-
-
Notifications
You must be signed in to change notification settings - Fork 647
Expand file tree
/
Copy pathgf_button.dart
More file actions
753 lines (671 loc) · 22.8 KB
/
gf_button.dart
File metadata and controls
753 lines (671 loc) · 22.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:getwidget/getwidget.dart';
class GFButton extends StatefulWidget {
/// Create buttons of all types. check out [GFIconButton] for icon buttons, and [GFBadge] for badges
const GFButton({
Key? key,
required this.onPressed,
this.onHighlightChanged,
this.textStyle,
this.boxShadow,
this.buttonBoxShadow,
this.focusColor,
this.hoverColor,
this.highlightColor,
this.splashColor,
this.elevation = 0.0,
this.focusElevation = 4.0,
this.hoverElevation = 4.0,
this.highlightElevation = 1.0,
this.disabledElevation = 0.0,
this.padding = const EdgeInsets.symmetric(horizontal: 8),
this.constraints,
this.borderShape,
this.animationDuration = kThemeChangeDuration,
this.clipBehavior = Clip.none,
this.focusNode,
this.autofocus = false,
MaterialTapTargetSize? materialTapTargetSize,
this.child,
this.type = GFButtonType.solid,
this.shape = GFButtonShape.standard,
this.color = GFColors.PRIMARY,
this.textColor,
this.position = GFPosition.start,
this.size = GFSize.MEDIUM,
this.borderSide,
this.text,
this.icon,
this.blockButton,
this.fullWidthButton,
this.colorScheme,
this.enableFeedback,
this.onLongPress,
this.disabledColor,
this.disabledTextColor,
}) : materialTapTargetSize =
materialTapTargetSize ?? MaterialTapTargetSize.padded,
// assert(elevation != null && elevation >= 0.0),
assert(focusElevation >= 0.0),
assert(hoverElevation >= 0.0),
assert(highlightElevation >= 0.0),
assert(disabledElevation >= 0.0),
super(key: key);
/// Called when the button is tapped or otherwise activated.
final VoidCallback? onPressed;
/// Called by the underlying [InkWell] widget's InkWell.onHighlightChanged callback.
final ValueChanged<bool>? onHighlightChanged;
/// Defines the default text style, with [Material.textStyle], for the button's [child].
final TextStyle? textStyle;
/// The border side for the button's [Material].
final BorderSide? borderSide;
/// The box shadow for the button's [Material], if GFButtonType is solid
final BoxShadow? boxShadow;
/// Pass [GFColors] or [Color]. The color for the button's [Material] when it has the input focus.
final Color? focusColor;
/// Pass [GFColors] or [Color]. The color for the button's [Material] when a pointer is hovering over it.
final Color? hoverColor;
/// Pass [GFColors] or [Color]. The highlight color for the button's [InkWell].
final Color? highlightColor;
/// Pass [GFColors] or [Color]. The splash color for the button's [InkWell].
final Color? splashColor;
/// The elevation for the button's [Material] when the button is [enabled] but not pressed.
final double elevation;
/// The elevation for the button's [Material] when the button is [enabled] and a pointer is hovering over it.
final double hoverElevation;
/// The elevation for the button's [Material] when the button is [enabled] and has the input focus.
final double focusElevation;
/// The elevation for the button's [Material] when the button is [enabled] and pressed.
final double highlightElevation;
/// The elevation for the button's [Material] when the button is not [enabled].
final double disabledElevation;
/// The internal padding for the button's [child].
final EdgeInsetsGeometry padding;
/// Defines the button's size.
final BoxConstraints? constraints;
/// The shape of the button's [Material].
final ShapeBorder? borderShape;
/// Defines the duration of animated changes for [shape] and [elevation].
final Duration animationDuration;
/// Typically the button's label.
final Widget? child;
/// Whether the button is enabled or disabled.
bool get enabled => onPressed != null;
/// Configures the minimum size of the tap target.
final MaterialTapTargetSize materialTapTargetSize;
/// {@macro flutter.widgets.Focus.focusNode}
final FocusNode? focusNode;
/// {@macro flutter.widgets.Focus.autofocus}
final bool autofocus;
/// {@macro flutter.widgets.Clip}
final Clip clipBehavior;
/// Button type of [GFButtonType] i.e, solid, outline, outline2x, transparent
final GFButtonType type;
/// Button type of [GFButtonShape] i.e, standard, pills, square, shadow, icons
final GFButtonShape shape;
/// Pass [GFColors] or [Color]
final Color color;
/// The fill color of the button when the button is disabled.
///
/// The default value of this color is the theme's disabled color,
/// [ThemeData.disabledColor].
///
/// See also:
///
/// * [color] - the fill color of the button when the button is [enabled].
final Color? disabledColor;
/// Pass [GFColors] or [Color]
final Color? textColor;
/// The color to use for this button's text when the button is disabled.
///
/// The button's [Material.textStyle] will be the current theme's button
///
/// The default value is the theme's disabled color,
/// [ThemeData.disabledColor].
///
/// If [textColor] is a [MaterialStateProperty<Color>], [disabledTextColor]
/// will be ignored.
///
/// See also:
///
/// * [textColor] - The color to use for this button's text when the button is [enabled].
final Color? disabledTextColor;
/// size of [double] or [GFSize] i.e, 1.2, small, medium, large etc.
final double size;
/// text of type [String] is alternative to child. text will get priority over child
final String? text;
/// icon of type [Widget]
final Widget? icon;
/// icon type of [GFPosition] i.e, start, end
final GFPosition position;
/// on true state blockButton gives block size button
final bool? blockButton;
/// on true state full width Button gives full width button
final bool? fullWidthButton;
/// on true state default box shadow appears around button, if GFButtonType is solid
final bool? buttonBoxShadow;
/// A set of thirteen colors that can be used to derive the button theme's
/// colors.
///
/// This property was added much later than the theme's set of highly
/// specific colors, like [ThemeData.highlightColor],
/// [ThemeData.splashColor] etc.
///
/// The colors for new button classes can be defined exclusively in terms
/// of [colorScheme]. When it's possible, the existing buttons will
/// (continue to) gradually migrate to it.
final ColorScheme? colorScheme;
/// Whether detected gestures should provide acoustic and/or haptic feedback.
///
/// For example, on Android a tap will produce a clicking sound and a
/// long-press will produce a short vibration, when feedback is enabled.
///
/// See also:
///
/// * [Feedback] for providing platform-specific feedback to certain actions.
final bool? enableFeedback;
/// Called when the button is long-pressed.
///
/// If this callback and [onPressed] are null, then the button will be disabled.
///
/// See also:
///
/// * [enabled], which is true if the button is enabled.
final VoidCallback? onLongPress;
@override
_GFButtonState createState() => _GFButtonState();
}
class _GFButtonState extends State<GFButton> {
late Color color;
Color? textColor;
Color? disabledColor;
Color? disabledTextColor;
Widget? child;
Widget? icon;
Function? onPressed;
late GFButtonType type;
late GFButtonShape shape;
late double size;
late GFPosition position;
late BoxShadow boxShadow;
final Set<WidgetState> _states = <WidgetState>{};
@override
void initState() {
color = widget.color;
textColor = widget.textColor;
child = widget.text != null ? Text(widget.text!) : widget.child;
icon = widget.icon;
onPressed = widget.onPressed;
type = widget.type;
shape = widget.shape;
size = widget.size;
position = widget.position;
disabledColor = widget.disabledColor;
disabledTextColor = widget.disabledTextColor;
_updateState(
WidgetState.disabled,
!widget.enabled,
);
super.initState();
}
bool get _hovered => _states.contains(WidgetState.hovered);
bool get _focused => _states.contains(WidgetState.focused);
bool get _pressed => _states.contains(WidgetState.pressed);
bool get _disabled => _states.contains(WidgetState.disabled);
double? buttonWidth() {
double? buttonWidth = 0;
if (widget.blockButton == true) {
buttonWidth = MediaQuery.of(context).size.width * 0.88;
} else if (widget.fullWidthButton == true) {
buttonWidth = MediaQuery.of(context).size.width;
} else {
buttonWidth = null;
}
return buttonWidth;
}
void _updateState(WidgetState state, bool value) {
value ? _states.add(state) : _states.remove(state);
}
void _handleHighlightChanged(bool value) {
if (_pressed != value) {
setState(() {
_updateState(WidgetState.pressed, value);
if (widget.onHighlightChanged != null) {
widget.onHighlightChanged!(value);
}
});
}
}
void _handleHoveredChanged(bool value) {
if (_hovered != value) {
setState(() {
_updateState(WidgetState.hovered, value);
});
}
}
void _handleFocusedChanged(bool value) {
if (_focused != value) {
setState(() {
_updateState(WidgetState.focused, value);
});
}
}
@override
void didUpdateWidget(GFButton oldWidget) {
_updateState(WidgetState.disabled, !widget.enabled);
// If the button is disabled while a press gesture is currently ongoing,
// InkWell makes a call to handleHighlightChanged. This causes an exception
// because it calls setState in the middle of a build. To preempt this, we
// manually update pressed to false when this situation occurs.
if (_disabled && _pressed) {
_handleHighlightChanged(false);
}
color = widget.color;
textColor = widget.textColor;
child = widget.text != null ? Text(widget.text!) : widget.child;
icon = widget.icon;
onPressed = widget.onPressed;
type = widget.type;
shape = widget.shape;
size = widget.size;
position = widget.position;
disabledColor = widget.disabledColor;
disabledTextColor = widget.disabledTextColor;
_updateState(
WidgetState.disabled,
!widget.enabled,
);
super.didUpdateWidget(oldWidget);
}
double get _effectiveElevation {
// These conditionals are in order of precedence, so be careful about
// reorganizing them.
if (_disabled) {
return widget.disabledElevation;
}
if (_pressed) {
return widget.highlightElevation;
}
if (_hovered) {
return widget.hoverElevation;
}
if (_focused) {
return widget.focusElevation;
}
return widget.elevation;
}
@override
Widget build(BuildContext context) {
ShapeBorder shapeBorderType;
Color getBorderColor() {
if (widget.enabled) {
final Color fillColor = color;
return fillColor;
} else {
if (disabledColor != null) {
return disabledColor!;
} else {
return color.withValues(alpha: 0.48);
}
}
}
Color getDisabledFillColor() {
if (widget.type == GFButtonType.transparent ||
widget.type == GFButtonType.outline ||
widget.type == GFButtonType.outline2x) {
return Colors.transparent;
}
if (disabledColor != null) {
return disabledColor!;
} else {
return color.withValues(alpha: 0.48);
}
}
Color getColor() {
if (widget.type == GFButtonType.transparent ||
widget.type == GFButtonType.outline ||
widget.type == GFButtonType.outline2x) {
return Colors.transparent;
}
final Color fillColor = color;
return fillColor;
}
Color getDisabledTextColor() {
if (disabledTextColor != null) {
return disabledTextColor!;
} else if (widget.type == GFButtonType.outline ||
widget.type == GFButtonType.outline2x ||
widget.type == GFButtonType.transparent) {
return color;
} else {
return GFColors.DARK;
}
}
Color getTextColor() {
if (widget.type == GFButtonType.outline ||
widget.type == GFButtonType.outline2x ||
widget.type == GFButtonType.transparent) {
return widget.enabled
? textColor == null
? color == GFColors.TRANSPARENT
? GFColors.DARK
: color
: textColor!
: getDisabledTextColor();
}
if (textColor == null) {
if (color == GFColors.TRANSPARENT) {
return GFColors.DARK;
} else {
return GFColors.WHITE;
}
} else {
return textColor!;
}
}
final Color? effectiveTextColor =
WidgetStateProperty.resolveAs<Color?>(widget.textStyle?.color, _states);
final Color themeColor =
Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.12);
final BorderSide outlineBorder = BorderSide(
color: widget.borderSide == null
? getBorderColor()
: widget.borderSide!.color,
width: (widget.borderSide?.width == null
? widget.type == GFButtonType.outline2x
? 2.0
: 1.0
: widget.borderSide?.width)!,
);
Size minSize;
switch (widget.materialTapTargetSize) {
case MaterialTapTargetSize.padded:
minSize = const Size(48, 48);
break;
case MaterialTapTargetSize.shrinkWrap:
minSize = Size.zero;
break;
default:
minSize = Size.zero;
break;
}
final BorderSide shapeBorder = widget.type == GFButtonType.outline ||
widget.type == GFButtonType.outline2x
? outlineBorder
: widget.borderSide ??
BorderSide(
color: getBorderColor(),
width: 0,
);
if (shape == GFButtonShape.pills) {
shapeBorderType = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
50,
),
side: shapeBorder,
);
} else if (shape == GFButtonShape.square) {
shapeBorderType = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(0),
side: shapeBorder,
);
} else if (shape == GFButtonShape.standard) {
shapeBorderType = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(3),
side: shapeBorder,
);
} else {
shapeBorderType = RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50),
side: shapeBorder,
);
}
BoxDecoration? getBoxShadow() {
if (widget.type != GFButtonType.transparent) {
if (widget.boxShadow == null && widget.buttonBoxShadow != true) {
return null;
} else {
return BoxDecoration(
color: widget.type == GFButtonType.transparent ||
widget.type == GFButtonType.outline ||
widget.type == GFButtonType.outline2x
? Colors.transparent
: color,
borderRadius: widget.shape == GFButtonShape.pills
? BorderRadius.circular(50)
: widget.shape == GFButtonShape.standard
? BorderRadius.circular(5)
: BorderRadius.zero,
boxShadow: [
widget.boxShadow == null && widget.buttonBoxShadow == true
? BoxShadow(
color: themeColor,
blurRadius: 1.5,
spreadRadius: 2,
offset: Offset.zero,
)
: widget.boxShadow != null
? widget.boxShadow!
: BoxShadow(
color: Theme.of(context).canvasColor,
blurRadius: 0,
spreadRadius: 0,
offset: Offset.zero,
),
],
);
}
}
return null;
}
TextStyle getTextStyle() {
if (widget.size == GFSize.SMALL) {
return TextStyle(
color: widget.enabled ? getTextColor() : getDisabledTextColor(),
fontSize: 12,
);
} else if (widget.size == GFSize.MEDIUM) {
return TextStyle(
color: widget.enabled ? getTextColor() : getDisabledTextColor(),
fontSize: 13,
fontWeight: FontWeight.w400,
);
} else if (widget.size == GFSize.LARGE) {
return TextStyle(
color: widget.enabled ? getTextColor() : getDisabledTextColor(),
fontSize: 14,
fontWeight: FontWeight.w500,
);
}
return TextStyle(
color: widget.enabled ? getTextColor() : getDisabledTextColor(),
fontSize: 13,
fontWeight: FontWeight.w400,
);
}
final Widget result = Container(
constraints: icon == null
? const BoxConstraints(minWidth: 80)
: const BoxConstraints(minWidth: 90),
decoration: widget.type == GFButtonType.solid ? getBoxShadow() : null,
child: Material(
elevation: _effectiveElevation,
textStyle: widget.textStyle == null ? getTextStyle() : widget.textStyle,
shape: widget.type == GFButtonType.transparent
? null
: widget.borderShape ?? shapeBorderType,
color: widget.enabled ? getColor() : getDisabledFillColor(),
type: MaterialType.button,
animationDuration: widget.animationDuration,
clipBehavior: widget.clipBehavior,
child: InkWell(
focusNode: widget.focusNode,
canRequestFocus: widget.enabled,
onFocusChange: _handleFocusedChanged,
autofocus: widget.autofocus,
onHighlightChanged: _handleHighlightChanged,
onHover: _handleHoveredChanged,
onTap: widget.onPressed,
onLongPress: widget.onLongPress,
enableFeedback: widget.enableFeedback ?? false,
splashColor: widget.splashColor,
highlightColor: widget.highlightColor,
focusColor: widget.focusColor,
hoverColor: widget.hoverColor,
customBorder: widget.type == GFButtonType.transparent
? null
: widget.borderShape ?? shapeBorderType,
child: IconTheme.merge(
data: IconThemeData(color: effectiveTextColor),
child: Container(
height: size,
width: buttonWidth(),
padding: widget.padding,
child: Center(
widthFactor: 1,
heightFactor: 1,
child: icon != null &&
child != null &&
(position == GFPosition.start)
? Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
icon!,
const SizedBox(width: 8),
child!
],
)
: icon != null &&
child != null &&
(position == GFPosition.end)
? Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
child!,
const SizedBox(width: 8),
icon!
],
)
: child,
),
),
),
),
),
);
return Semantics(
container: true,
button: true,
enabled: widget.enabled,
child: _InputPadding(
minSize: minSize,
child: Focus(
focusNode: widget.focusNode,
onFocusChange: _handleFocusedChanged,
autofocus: widget.autofocus,
child: result,
),
),
);
}
}
/// A widget to pad the area around a [MaterialButton]'s inner [Material].
///
/// Redirect taps that occur in the padded area around the child to the center
/// of the child. This increases the size of the button and the button's
/// "tap target", but not its material or its ink splashes.
class _InputPadding extends SingleChildRenderObjectWidget {
const _InputPadding({
Key? key,
Widget? child,
this.minSize,
}) : super(
key: key,
child: child,
);
final Size? minSize;
@override
RenderObject createRenderObject(BuildContext context) =>
_RenderInputPadding(minSize);
@override
void updateRenderObject(
BuildContext context, covariant _RenderInputPadding renderObject) {
renderObject.minSize = minSize;
}
}
class _RenderInputPadding extends RenderShiftedBox {
_RenderInputPadding(this._minSize, [RenderBox? child]) : super(child);
Size? get minSize => _minSize;
Size? _minSize;
set minSize(Size? value) {
_minSize = value;
markNeedsLayout();
}
@override
double computeMinIntrinsicWidth(double height) {
if (child != null && minSize != null) {
return math.max(child!.getMinIntrinsicWidth(height), minSize!.width);
}
return 0;
}
@override
double computeMinIntrinsicHeight(double width) {
if (child != null && minSize != null) {
return math.max(child!.getMinIntrinsicHeight(width), minSize!.height);
}
return 0;
}
@override
double computeMaxIntrinsicWidth(double height) {
if (child != null && minSize != null) {
return math.max(child!.getMaxIntrinsicWidth(height), minSize!.width);
}
return 0;
}
@override
double computeMaxIntrinsicHeight(double width) {
if (child != null && minSize != null) {
return math.max(child!.getMaxIntrinsicHeight(width), minSize!.height);
}
return 0;
}
@override
void performLayout() {
if (child != null && minSize != null) {
child!.layout(constraints, parentUsesSize: true);
// ignore: avoid_as
final BoxParentData childParentData = child!.parentData as BoxParentData;
final double height = math.max(child!.size.width, minSize!.width);
final double width = math.max(child!.size.height, minSize!.height);
size = constraints.constrain(Size(height, width));
childParentData.offset =
// ignore: avoid_as
Alignment.center.alongOffset(size - child!.size as Offset);
} else {
size = Size.zero;
}
}
@override
bool hitTest(BoxHitTestResult result, {required Offset position}) {
if (super.hitTest(result, position: position)) {
return true;
}
if (child != null) {
final Offset center = child!.size.center(Offset.zero);
return result.addWithRawTransform(
transform: MatrixUtils.forceToPoint(center),
position: center,
hitTest: (BoxHitTestResult result, Offset position) {
assert(position == center);
return child!.hitTest(
result,
position: center,
);
},
);
}
throw Exception('child property cannot be null');
}
}