-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathouds_button.dart
More file actions
535 lines (505 loc) · 19.8 KB
/
ouds_button.dart
File metadata and controls
535 lines (505 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Software Name: OUDS Flutter
// SPDX-FileCopyrightText: Copyright (c) Orange SA
// SPDX-License-Identifier: MIT
//
// This software is distributed under the MIT license,
// the text of which is available at https://opensource.org/license/MIT/
// or see the "LICENSE" file for more details.
//
// Software description: Flutter library of reusable graphical components
//
/// {@category Button}
library;
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:ouds_core/components/button/internal/ouds_button_border_modifier.dart';
import 'package:ouds_core/components/button/internal/ouds_button_control_state.dart';
import 'package:ouds_core/components/button/internal/ouds_button_icon_modifier.dart';
import 'package:ouds_core/components/button/internal/ouds_button_loading_modifier.dart';
import 'package:ouds_core/components/button/internal/ouds_button_style_modifier.dart';
import 'package:ouds_core/components/common/OudsBorder.dart';
import 'package:ouds_core/l10n/gen/ouds_localizations.dart';
import 'package:ouds_theme_contract/ouds_theme.dart';
/// The [OudsButtonAppearance] enum defines the visual importance of the button within the UI.
enum OudsButtonAppearance {
defaultAppearance,
strong,
brand,
minimal,
negative,
}
///
/// A circular loading indicator displayed in the button.
///
/// param [progress] The loading progress, where 0.0 represents no progress and 1.0 represents full progress.
/// Values outside of this range are coerced into the range.
/// Set this value to `null` to display a circular indeterminate progress indicator.
///
class Loader {
final double? progress;
Loader({this.progress});
}
///The [OudsButtonLayout] defines the layout of the button’s content.
///
/// This enum controls whether the button displays text, an icon, or both.
enum OudsButtonLayout {
textOnly,
iconAndText,
iconOnly;
}
/// [OUDS Button design guidelines](https://r.orange.fr/r/S-ouds-doc-button)
///
/// **Reference design version : 3.2.0**
///
/// Button is a UI element that triggers an action or event, and is used to initiate tasks or confirming an action.
/// Button appears in different layouts, styles and states to indicate hierarchy or emphasis.
///
/// This version of the button uses the *text only* layout which is the most used layout.
/// Other layouts are available for this component: *text + icon* and *icon only*.
///
/// Note that in the case it is placed in an [OudsColoredBox], its monochrome variant is automatically displayed.
/// Some tokens associated with these specific colors can be customized and are identified with the `Mono` suffix (for instance [OudsButtonTokens.colorBgDefaultEnabledMono]).
///
/// Parameters:
/// - [label]: Label displayed in the button which describes the button action. Use action verbs or phrases to tell the user what will happen next.
/// - [icon]: Icon displayed in the button. Use an icon to add additional affordance where the icon has a clear and well-established meaning.
/// - [onPressed]: Callback invoked when the button is clicked.
/// Controls the enabled state of the button when [loader] is equal to null.
/// When `false`, this button will not be clickable. Has no effect when [loader] is not null.
/// - [loader]: An optional loading progress indicator displayed in the button to indicate an ongoing operation.
/// - [appearance]: The button appearance based on its [OudsButtonAppearance].
/// A button with [OudsButtonAppearance.negative] appearance is not allowed as a direct or indirect child of an [OudsColoredBox] and will throw an [IllegalStateException].
/// To create the widget with an asset from a package, the [package] argument
/// must be provided. For instance, suppose a package called `my_icons` has
/// `icons/heart.svg` .
/// - [isFullWidth]: Flag to let button take all the screen width, set to *false* by default.
///
/// ### You can use [OudsButton] component in your project, customizing parameters as needed :
///
/// **Text only button :**
///
/// This is the default layout of the component.
///
///
/// ```dart
/// OudsButton(
/// isFullWidth: false,
/// label: 'Button',
/// appearance: OudsButtonAppearance.defaultAppearance,
/// onPressed: () {
/// // Handle button tap.
/// },
/// );
/// ```
/// ```dart
///
/// This is the Loading layout of the component.
///
/// OudsButton(
/// isFullWidth: false,
/// label: 'Button',
/// loader: Loader(progress: null),
/// appearance: OudsButtonAppearance.defaultAppearance
/// onPressed: () {
/// // Handle button tap.
/// },
/// );
/// ```
///
///
class OudsButton extends StatefulWidget {
final String? label;
final String? icon;
final VoidCallback? onPressed;
final Loader? loader;
final OudsButtonAppearance appearance;
final String? package;
final bool? isFullWidth;
const OudsButton({
super.key,
this.label,
this.icon,
this.onPressed,
this.loader,
required this.appearance,
this.package,
this.isFullWidth = false,
});
@override
State<OudsButton> createState() => _OudsButtonState();
/// Property that detects and returns the button layout based on the provided elements (text and/or icon)
OudsButtonLayout get layout => _detectLayout(label, icon);
static OudsButtonLayout _detectLayout(String? label, String? icon) {
if (label != null && icon != null) {
return OudsButtonLayout.iconAndText;
} else if (label != null) {
return OudsButtonLayout.textOnly;
} else if (icon != null) {
return OudsButtonLayout.iconOnly;
}
return OudsButtonLayout.textOnly;
}
}
class _OudsButtonState extends State<OudsButton> {
// Tracks hover and press states manually for custom SVG icon rendering.
//
// Flutter’s [ButtonStyle] uses [WidgetStateProperty] for styling based on
// states like hovered, focused, or pressed. However, this does not apply
// directly to SVGs via [colorFilter].
//
// To support dynamic color changes on SVG icons, we track interaction
// states manually using [MouseRegion] and [GestureDetector], enabling us to
// update the icon style accordingly.
bool _isHovered = false;
bool _isPressed = false;
// Tracks keyboard focus highlight to make focus visible when navigating with a keyboard.
bool _isFocused = false;
late FocusNode _focusNode;
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_focusNode.addListener(() {
setState(() {
_handleFocusChange(_focusNode.hasFocus);
});
});
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
void _handleFocusChange(bool focus) {
if (widget.onPressed == null) _isFocused = false; // Ignore focus changes if disabled
setState(() => _isFocused = focus);
}
void _handlePressed(VoidCallback? callback) {
setState(() {
_isPressed = true;
});
SchedulerBinding.instance.addPostFrameCallback((_) {
callback?.call();
if (mounted) {
setState(() {
_isPressed = false;
});
}
});
}
@override
Widget build(BuildContext context) {
// Determines the local visual state of the button (hovered, pressed, etc.)
// using internal flags managed via a [MouseRegion] and gesture listeners.
// This state is used to compute dynamic styling (e.g., background color).
final buttonStateDeterminer = OudsButtonControlStateDeterminer(
enabled: widget.onPressed != null,
isPressed: _isPressed,
isHovered: _isHovered,
isFocused: _isFocused,
isLoading: widget.loader != null,
);
final buttonState = buttonStateDeterminer.determineControlState();
final borderTokens = OudsTheme.of(context).borderTokens;
try {
if (widget.appearance == OudsButtonAppearance.negative && OudsTheme.isOnColoredSurfaceOf(context)) {
// Throw an IllegalStateException
throw StateError("An OudsButton with OudsButtonAppearance.negative appearance cannot be displayed as a child of an OudsColoredBox.");
}
} catch (e) {
debugPrint("Warning: ${e.toString()}");
}
final oudsTheme = OudsTheme.of(context);
// Get the button's radius so the focus border matches.
final buttonBorderRadius = OudsButtonBorderModifier.getBorderRadiusFocus(context);
return _isFocused
? Stack(
clipBehavior: Clip.none, // Allows the border to overflow slightly if necessary.
alignment: Alignment.center,
children: [
// The button itself. It defines the size of the Stack.
_buildLayout(context, buttonState),
// The focus border, drawn on top.
// IgnorePointer prevents this border from intercepting clicks.
Positioned.fill(
//the focus border should be outside
left: - oudsTheme.borderTokens.widthFocus,
right: - oudsTheme.borderTokens.widthFocus,
bottom: - oudsTheme.borderTokens.widthFocus,
top: - oudsTheme.borderTokens.widthFocus,
child: IgnorePointer(
child: Container(
decoration: BoxDecoration(
border: OudsBorder().borderAll(
color: oudsTheme.colorScheme(context).borderFocus,
width: oudsTheme.borderTokens.widthFocus / 2,
),
// The border radius should match the button's radius.
borderRadius: buttonBorderRadius,
),
child: Container(
decoration: BoxDecoration(
border: OudsBorder().borderAll(
color: OudsTheme.of(context).colorScheme(context).borderFocusInset,
width: borderTokens.widthFocusInset,
),
borderRadius: buttonBorderRadius
),
),
),
)
),
],
) : _buildLayout(
context,
buttonState,
);
}
Widget _buildLayout(
BuildContext context,
OudsButtonControlState buttonState,
) {
switch (widget.layout) {
case OudsButtonLayout.iconOnly:
return _buildButtonIconOnly(context, buttonState);
case OudsButtonLayout.iconAndText:
return _buildButtonIconAndText(context, buttonState);
case OudsButtonLayout.textOnly:
return _buildButtonTextOnly(context, buttonState);
}
}
Widget _buildButtonIconAndText(BuildContext context, OudsButtonControlState buttonState) {
final buttonToken = OudsTheme.of(context).componentsTokens(context).button;
switch (buttonState) {
case OudsButtonControlState.loading:
final buttonIconAndText = Semantics(
label: OudsLocalizations.of(context)?.core_common_loading_a11y,
enabled: false,
button: true,
child: ExcludeSemantics(
child: OutlinedButton(
onPressed: null,
style: OudsButtonStyleModifier.buildButtonStyle(context, appearance: widget.appearance, layout: widget.layout, buttonState: buttonState),
child: Stack(
alignment: Alignment.center,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
null,
size: buttonToken.sizeIcon,
),
SizedBox(
width: buttonToken.spaceColumnGapIcon,
),
Flexible(
fit: FlexFit.loose,
child: Text(
widget.label ?? "",
style: TextStyle(
color: Colors.transparent,
),
),
),
],
),
Padding(
padding: EdgeInsetsDirectional.only(start: buttonToken.spaceColumnGapIcon),
child: _buildLoadingIndicator(context, widget.loader?.progress),
),
],
),
),
),
);
return _wrapFullWidth(buttonIconAndText);
default:
final buttonIconAndText = MouseRegion(
onEnter: (_) => setState(() => _isHovered = true),
onExit: (_) => setState(() => _isHovered = false),
child: GestureDetector(
onTapDown: (_) => setState(() => _isPressed = true),
onTapUp: (_) => setState(() => _isPressed = false),
onTapCancel: () => setState(() => _isPressed = false),
child: Semantics(
label: widget.label ?? "",
button: true,
child: ExcludeSemantics(
child: OutlinedButton(
focusNode: _focusNode,
onPressed: widget.onPressed == null ? null : () => _handlePressed(widget.onPressed),
style: OudsButtonStyleModifier.buildButtonStyle(context, appearance: widget.appearance, layout: widget.layout, buttonState: buttonState),
child: Stack(
alignment: Alignment.center,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
_buildIcon(context, widget.icon!, widget.appearance, widget.layout, buttonState),
SizedBox(
width: buttonToken.spaceColumnGapIcon,
),
Flexible(
fit: FlexFit.loose,
child: Text(
widget.label ?? "",
textAlign: TextAlign.center,
style: OudsTheme.of(context).typographyTokens.typeLabelStrongLarge(context),
),
),
],
),
],
),
),
),
),
),
);
return _wrapFullWidth(buttonIconAndText);
}
}
Widget _buildButtonIconOnly(BuildContext context, OudsButtonControlState buttonState) {
final buttonToken = OudsTheme.of(context).componentsTokens(context).button;
switch (buttonState) {
case OudsButtonControlState.loading:
final buttonIconOnly = Semantics(
label: OudsLocalizations.of(context)?.core_common_loading_a11y,
enabled: false,
button: true,
child: IconButton(
onPressed: null,
style: OudsButtonStyleModifier.buildButtonStyle(context, appearance: widget.appearance, layout: widget.layout, buttonState: buttonState),
icon: Stack(
alignment: Alignment.center,
children: [
Opacity(
opacity: OudsTheme.of(context).opacityTokens.invisible,
child: _buildIcon(context, widget.icon!, widget.appearance, widget.layout, buttonState),
),
_buildLoadingIndicator(context, widget.loader?.progress),
],
),
),
);
return _wrapFullWidth(buttonIconOnly);
default:
final buttonIconOnly = MouseRegion(
onEnter: (_) => setState(() => _isHovered = true),
onExit: (_) => setState(() => _isHovered = false),
child: GestureDetector(
onTapDown: (_) => setState(() => _isPressed = true),
onTapUp: (_) => setState(() => _isPressed = false),
onTapCancel: () => setState(() => _isPressed = false),
child: Semantics(
label: OudsLocalizations.of(context)?.core_button_icon_only_a11y,
button: true,
child: ExcludeSemantics(
child: SizedBox(
width: buttonToken.sizeMinWidth,
child: IconButton(
focusNode: _focusNode,
style: OudsButtonStyleModifier.buildButtonStyle(context, appearance: widget.appearance, layout: widget.layout, buttonState: buttonState),
onPressed: widget.onPressed == null ? null : () => _handlePressed(widget.onPressed),
icon: _buildIcon(context, widget.icon!, widget.appearance, widget.layout, buttonState),
),
),
),
),
),
);
return _wrapFullWidth(buttonIconOnly);
}
}
Widget _buildButtonTextOnly(BuildContext context, OudsButtonControlState buttonState) {
switch (buttonState) {
case OudsButtonControlState.loading:
final buttonTextOnly = Semantics(
label: OudsLocalizations.of(context)?.core_common_loading_a11y,
enabled: false,
button: true,
child: ExcludeSemantics(
child: OutlinedButton(
onPressed: null,
style: OudsButtonStyleModifier.buildButtonStyle(context, appearance: widget.appearance, layout: widget.layout, buttonState: buttonState),
child: Stack(
alignment: Alignment.center,
children: [
Opacity(
opacity: OudsTheme.of(context).opacityTokens.invisible,
child: Text(
widget.label ?? "",
),
),
_buildLoadingIndicator(context, widget.loader?.progress),
],
),
),
),
);
return _wrapFullWidth(buttonTextOnly);
default:
final buttonTextOnly = OutlinedButton(
focusNode: _focusNode,
style: OudsButtonStyleModifier.buildButtonStyle(context, appearance: widget.appearance, layout: widget.layout, buttonState: buttonState),
onPressed: widget.onPressed == null ? null : () => _handlePressed(widget.onPressed),
child: Text(
widget.label ?? "",
textAlign: TextAlign.center,
style: OudsTheme.of(context).typographyTokens.typeLabelStrongLarge(context),
),
);
return _wrapFullWidth(buttonTextOnly);
}
}
Widget _buildLoadingIndicator(BuildContext context, double? progress) {
{
final clampedProgress = progress?.clamp(0.0, 1.0);
return SizedBox(
width: OudsTheme.of(context).componentsTokens(context).button.sizeLoader,
height: OudsTheme.of(context).componentsTokens(context).button.sizeLoader,
child: CircularProgressIndicator(
value: clampedProgress,
color: OudsButtonLoadingModifier.getColorToken(context, widget.appearance),
strokeWidth: 3,
),
);
}
}
/// Expands the button to fill the available horizontal space when [widget.isFullWidth] is true.
///
/// When `isFullWidth` is `true`, the returned widget wraps [child] in a [SizedBox] with
/// `width: double.infinity`, allowing the button to stretch to the maximum width allowed by
/// its parent constraints. When `isFullWidth` is `false` (default), [child] is returned as-is.
Widget _wrapFullWidth(Widget child) {
if (widget.isFullWidth == true) {
return SizedBox(
width: double.infinity,
child: child,
);
}
return child;
}
Widget _buildIcon(
BuildContext context,
String assetName,
final OudsButtonAppearance appearance,
final OudsButtonLayout layout,
final OudsButtonControlState buttonState,
) {
return SvgPicture.asset(
excludeFromSemantics: true,
package: widget.package,
assetName,
fit: BoxFit.contain,
width: OudsButtonIconModifier.getIconSize(context, layout),
height: OudsButtonIconModifier.getIconSize(context, layout),
colorFilter: ColorFilter.mode(
OudsButtonIconModifier.getIconColor(context, buttonState, appearance),
BlendMode.srcIn,
),
);
}
}