-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathgradient_button.dart
More file actions
166 lines (154 loc) · 4.88 KB
/
gradient_button.dart
File metadata and controls
166 lines (154 loc) · 4.88 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
import 'package:flutter/material.dart';
class GradientButton extends StatelessWidget {
const GradientButton({
Key? key,
this.colors,
required this.onPressed,
required this.child,
this.padding,
this.borderRadius = const BorderRadius.all(Radius.circular(2)),
this.textColor,
this.splashColor,
this.disabledColor,
this.disabledTextColor,
this.onHighlightChanged,
}) : super(key: key);
// 渐变色数组
final List<Color>? colors;
final Color? textColor;
final Color? splashColor;
final Color? disabledTextColor;
final Color? disabledColor;
final EdgeInsetsGeometry? padding;
final Widget child;
final BorderRadius? borderRadius;
final GestureTapCallback? onPressed;
final ValueChanged<bool>? onHighlightChanged;
@override
Widget build(BuildContext context) {
ThemeData theme = Theme.of(context);
//确保colors数组不空
List<Color> _colors =
colors ?? [theme.primaryColor, theme.primaryColorDark];
final radius = borderRadius;
bool disabled = onPressed == null;
return DecoratedBox(
decoration: BoxDecoration(
gradient: disabled ? null : LinearGradient(colors: _colors),
color: disabled ? disabledColor ?? theme.disabledColor : null,
borderRadius: radius,
),
child: Material(
type: MaterialType.transparency,
borderRadius: radius,
clipBehavior: Clip.hardEdge,
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 88.0, minHeight: 36.0),
child: InkWell(
splashColor: splashColor ?? _colors.last,
highlightColor: Colors.transparent,
onHighlightChanged: onHighlightChanged,
onTap: onPressed,
child: Padding(
padding: padding ?? theme.buttonTheme.padding,
child: DefaultTextStyle(
style: const TextStyle(fontWeight: FontWeight.bold),
child: Center(
child: DefaultTextStyle(
style: theme.textTheme.labelLarge!.copyWith(
color: disabled
? disabledTextColor ?? Colors.black38
: textColor ?? Colors.white,
),
child: child,
),
widthFactor: 1,
heightFactor: 1,
),
),
),
),
),
),
);
}
}
class ElevatedGradientButton extends StatefulWidget {
const ElevatedGradientButton({
Key? key,
this.colors,
this.onPressed,
this.padding,
this.borderRadius = const BorderRadius.all(Radius.circular(2)),
this.textColor,
this.splashColor,
this.disabledColor,
this.disabledTextColor,
this.onHighlightChanged,
this.shadowColor,
required this.child,
}) : super(key: key);
// 渐变色数组
final List<Color>? colors;
final Color? textColor;
final Color? splashColor;
final Color? disabledTextColor;
final Color? disabledColor;
final Color? shadowColor;
final EdgeInsetsGeometry? padding;
final Widget child;
final BorderRadius? borderRadius;
final GestureTapCallback? onPressed;
final ValueChanged<bool>? onHighlightChanged;
@override
_ElevatedGradientButtonState createState() => _ElevatedGradientButtonState();
}
class _ElevatedGradientButtonState extends State<ElevatedGradientButton> {
bool _tapDown = false;
@override
Widget build(BuildContext context) {
bool disabled = widget.onPressed == null;
return AnimatedContainer(
duration: const Duration(milliseconds: 100),
decoration: BoxDecoration(
borderRadius: widget.borderRadius,
boxShadow: disabled
? null
: [
_tapDown
? BoxShadow(
offset: const Offset(2, 6),
spreadRadius: -2,
blurRadius: 9,
color: widget.shadowColor ?? Colors.black54,
)
: BoxShadow(
offset: const Offset(0, 2),
spreadRadius: -2,
blurRadius: 3,
color: widget.shadowColor ?? Colors.black87,
)
],
),
child: GradientButton(
colors: widget.colors,
onPressed: widget.onPressed,
padding: widget.padding,
borderRadius: widget.borderRadius,
textColor: widget.textColor,
splashColor: widget.splashColor,
disabledColor: widget.disabledColor,
disabledTextColor: widget.disabledTextColor,
child: widget.child,
onHighlightChanged: (v) {
setState(() {
_tapDown = v;
});
if (widget.onHighlightChanged != null) {
widget.onHighlightChanged!(v);
}
},
),
);
}
}