|
@@ -0,0 +1,371 @@
|
|
1
|
+import 'dart:math' as math;
|
|
2
|
+
|
|
3
|
+import 'package:flutter/material.dart' hide Checkbox;
|
|
4
|
+import 'package:flutter/rendering.dart';
|
|
5
|
+import 'package:flutter/widgets.dart';
|
|
6
|
+
|
|
7
|
+/// A material design checkbox.
|
|
8
|
+///
|
|
9
|
+/// The checkbox itself does not maintain any state. Instead, when the state of
|
|
10
|
+/// the checkbox changes, the widget calls the [onChanged] callback. Most
|
|
11
|
+/// widgets that use a checkbox will listen for the [onChanged] callback and
|
|
12
|
+/// rebuild the checkbox with a new [value] to update the visual appearance of
|
|
13
|
+/// the checkbox.
|
|
14
|
+///
|
|
15
|
+/// The checkbox can optionally display three values - true, false, and null -
|
|
16
|
+/// if [tristate] is true. When [value] is null a dash is displayed. By default
|
|
17
|
+/// [tristate] is false and the checkbox's [value] must be true or false.
|
|
18
|
+///
|
|
19
|
+/// Requires one of its ancestors to be a [Material] widget.
|
|
20
|
+///
|
|
21
|
+/// See also:
|
|
22
|
+///
|
|
23
|
+/// * [CheckboxListTile], which combines this widget with a [ListTile] so that
|
|
24
|
+/// you can give the checkbox a label.
|
|
25
|
+/// * [Switch], a widget with semantics similar to [Checkbox].
|
|
26
|
+/// * [Radio], for selecting among a set of explicit values.
|
|
27
|
+/// * [Slider], for selecting a value in a range.
|
|
28
|
+/// * <https://material.google.com/components/selection-controls.html#selection-controls-checkbox>
|
|
29
|
+/// * <https://material.google.com/components/lists-controls.html#lists-controls-types-of-list-controls>
|
|
30
|
+class Checkbox extends StatefulWidget {
|
|
31
|
+ /// Creates a material design checkbox.
|
|
32
|
+ ///
|
|
33
|
+ /// The checkbox itself does not maintain any state. Instead, when the state of
|
|
34
|
+ /// the checkbox changes, the widget calls the [onChanged] callback. Most
|
|
35
|
+ /// widgets that use a checkbox will listen for the [onChanged] callback and
|
|
36
|
+ /// rebuild the checkbox with a new [value] to update the visual appearance of
|
|
37
|
+ /// the checkbox.
|
|
38
|
+ ///
|
|
39
|
+ /// The following arguments are required:
|
|
40
|
+ ///
|
|
41
|
+ /// * [value], which determines whether the checkbox is checked. The [value]
|
|
42
|
+ /// can only be null if [tristate] is true.
|
|
43
|
+ /// * [onChanged], which is called when the value of the checkbox should
|
|
44
|
+ /// change. It can be set to null to disable the checkbox.
|
|
45
|
+ ///
|
|
46
|
+ /// The value of [tristate] must not be null.
|
|
47
|
+ const Checkbox({
|
|
48
|
+ Key key,
|
|
49
|
+ @required this.value,
|
|
50
|
+ this.tristate = false,
|
|
51
|
+ @required this.onChanged,
|
|
52
|
+ this.activeColor,
|
|
53
|
+ this.checkColor,
|
|
54
|
+ this.materialTapTargetSize,
|
|
55
|
+ }) : assert(tristate != null),
|
|
56
|
+ assert(tristate || value != null),
|
|
57
|
+ super(key: key);
|
|
58
|
+
|
|
59
|
+ /// Whether this checkbox is checked.
|
|
60
|
+ ///
|
|
61
|
+ /// This property must not be null.
|
|
62
|
+ final bool value;
|
|
63
|
+
|
|
64
|
+ /// Called when the value of the checkbox should change.
|
|
65
|
+ ///
|
|
66
|
+ /// The checkbox passes the new value to the callback but does not actually
|
|
67
|
+ /// change state until the parent widget rebuilds the checkbox with the new
|
|
68
|
+ /// value.
|
|
69
|
+ ///
|
|
70
|
+ /// If this callback is null, the checkbox will be displayed as disabled
|
|
71
|
+ /// and will not respond to input gestures.
|
|
72
|
+ ///
|
|
73
|
+ /// When the checkbox is tapped, if [tristate] is false (the default) then
|
|
74
|
+ /// the [onChanged] callback will be applied to `!value`. If [tristate] is
|
|
75
|
+ /// true this callback cycle from false to true to null.
|
|
76
|
+ ///
|
|
77
|
+ /// The callback provided to [onChanged] should update the state of the parent
|
|
78
|
+ /// [StatefulWidget] using the [State.setState] method, so that the parent
|
|
79
|
+ /// gets rebuilt; for example:
|
|
80
|
+ ///
|
|
81
|
+ /// ```dart
|
|
82
|
+ /// Checkbox(
|
|
83
|
+ /// value: _throwShotAway,
|
|
84
|
+ /// onChanged: (bool newValue) {
|
|
85
|
+ /// setState(() {
|
|
86
|
+ /// _throwShotAway = newValue;
|
|
87
|
+ /// });
|
|
88
|
+ /// },
|
|
89
|
+ /// )
|
|
90
|
+ /// ```
|
|
91
|
+ final ValueChanged<bool> onChanged;
|
|
92
|
+
|
|
93
|
+ /// The color to use when this checkbox is checked.
|
|
94
|
+ ///
|
|
95
|
+ /// Defaults to [ThemeData.toggleableActiveColor].
|
|
96
|
+ final Color activeColor;
|
|
97
|
+
|
|
98
|
+ final Color checkColor;
|
|
99
|
+
|
|
100
|
+ /// If true the checkbox's [value] can be true, false, or null.
|
|
101
|
+ ///
|
|
102
|
+ /// Checkbox displays a dash when its value is null.
|
|
103
|
+ ///
|
|
104
|
+ /// When a tri-state checkbox is tapped its [onChanged] callback will be
|
|
105
|
+ /// applied to true if the current value is null or false, false otherwise.
|
|
106
|
+ /// Typically tri-state checkboxes are disabled (the onChanged callback is
|
|
107
|
+ /// null) so they don't respond to taps.
|
|
108
|
+ ///
|
|
109
|
+ /// If tristate is false (the default), [value] must not be null.
|
|
110
|
+ final bool tristate;
|
|
111
|
+
|
|
112
|
+ /// Configures the minimum size of the tap target.
|
|
113
|
+ ///
|
|
114
|
+ /// Defaults to [ThemeData.materialTapTargetSize].
|
|
115
|
+ ///
|
|
116
|
+ /// See also:
|
|
117
|
+ ///
|
|
118
|
+ /// * [MaterialTapTargetSize], for a description of how this affects tap targets.
|
|
119
|
+ final MaterialTapTargetSize materialTapTargetSize;
|
|
120
|
+
|
|
121
|
+ /// The width of a checkbox widget.
|
|
122
|
+ static const double width = 18.0;
|
|
123
|
+
|
|
124
|
+ @override
|
|
125
|
+ _CheckboxState createState() => _CheckboxState();
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin {
|
|
129
|
+ @override
|
|
130
|
+ Widget build(BuildContext context) {
|
|
131
|
+ assert(debugCheckHasMaterial(context));
|
|
132
|
+ final ThemeData themeData = Theme.of(context);
|
|
133
|
+ Size size;
|
|
134
|
+ switch (widget.materialTapTargetSize ?? themeData.materialTapTargetSize) {
|
|
135
|
+ case MaterialTapTargetSize.padded:
|
|
136
|
+ size = const Size(2 * kRadialReactionRadius + 8.0, 2 * kRadialReactionRadius + 8.0);
|
|
137
|
+ break;
|
|
138
|
+ case MaterialTapTargetSize.shrinkWrap:
|
|
139
|
+ size = const Size(2 * kRadialReactionRadius, 2 * kRadialReactionRadius);
|
|
140
|
+ break;
|
|
141
|
+ }
|
|
142
|
+ final BoxConstraints additionalConstraints = BoxConstraints.tight(size);
|
|
143
|
+ return _CheckboxRenderObjectWidget(
|
|
144
|
+ value: widget.value,
|
|
145
|
+ tristate: widget.tristate,
|
|
146
|
+ activeColor: widget.activeColor ?? themeData.toggleableActiveColor,
|
|
147
|
+ checkColor: widget.checkColor ?? themeData.primaryColor,
|
|
148
|
+ inactiveColor: widget.onChanged != null ? themeData.unselectedWidgetColor : themeData.disabledColor,
|
|
149
|
+ onChanged: widget.onChanged,
|
|
150
|
+ additionalConstraints: additionalConstraints,
|
|
151
|
+ vsync: this,
|
|
152
|
+ );
|
|
153
|
+ }
|
|
154
|
+}
|
|
155
|
+
|
|
156
|
+class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
|
|
157
|
+ const _CheckboxRenderObjectWidget({
|
|
158
|
+ Key key,
|
|
159
|
+ @required this.value,
|
|
160
|
+ @required this.tristate,
|
|
161
|
+ @required this.activeColor,
|
|
162
|
+ @required this.inactiveColor,
|
|
163
|
+ @required this.checkColor,
|
|
164
|
+ @required this.onChanged,
|
|
165
|
+ @required this.vsync,
|
|
166
|
+ @required this.additionalConstraints,
|
|
167
|
+ }) : assert(tristate != null),
|
|
168
|
+ assert(tristate || value != null),
|
|
169
|
+ assert(activeColor != null),
|
|
170
|
+ assert(inactiveColor != null),
|
|
171
|
+ assert(vsync != null),
|
|
172
|
+ super(key: key);
|
|
173
|
+
|
|
174
|
+ final bool value;
|
|
175
|
+ final bool tristate;
|
|
176
|
+ final Color activeColor;
|
|
177
|
+ final Color checkColor;
|
|
178
|
+ final Color inactiveColor;
|
|
179
|
+ final ValueChanged<bool> onChanged;
|
|
180
|
+ final TickerProvider vsync;
|
|
181
|
+ final BoxConstraints additionalConstraints;
|
|
182
|
+
|
|
183
|
+ @override
|
|
184
|
+ _RenderCheckbox createRenderObject(BuildContext context) => _RenderCheckbox(
|
|
185
|
+ value: value,
|
|
186
|
+ tristate: tristate,
|
|
187
|
+ activeColor: activeColor,
|
|
188
|
+ inactiveColor: inactiveColor,
|
|
189
|
+ onChanged: onChanged,
|
|
190
|
+ vsync: vsync,
|
|
191
|
+ additionalConstraints: additionalConstraints,
|
|
192
|
+ );
|
|
193
|
+
|
|
194
|
+ @override
|
|
195
|
+ void updateRenderObject(BuildContext context, _RenderCheckbox renderObject) {
|
|
196
|
+ renderObject
|
|
197
|
+ ..value = value
|
|
198
|
+ ..tristate = tristate
|
|
199
|
+ ..activeColor = activeColor
|
|
200
|
+ ..inactiveColor = inactiveColor
|
|
201
|
+ ..checkColor = checkColor
|
|
202
|
+ ..onChanged = onChanged
|
|
203
|
+ ..additionalConstraints = additionalConstraints
|
|
204
|
+ ..vsync = vsync;
|
|
205
|
+ }
|
|
206
|
+}
|
|
207
|
+
|
|
208
|
+const double _kEdgeSize = Checkbox.width;
|
|
209
|
+const Radius _kEdgeRadius = Radius.circular(1.0);
|
|
210
|
+const double _kStrokeWidth = 2.0;
|
|
211
|
+
|
|
212
|
+class _RenderCheckbox extends RenderToggleable {
|
|
213
|
+ _RenderCheckbox({
|
|
214
|
+ bool value,
|
|
215
|
+ bool tristate,
|
|
216
|
+ Color activeColor,
|
|
217
|
+ Color inactiveColor,
|
|
218
|
+ BoxConstraints additionalConstraints,
|
|
219
|
+ ValueChanged<bool> onChanged,
|
|
220
|
+ @required TickerProvider vsync,
|
|
221
|
+ }): _oldValue = value,
|
|
222
|
+ super(
|
|
223
|
+ value: value,
|
|
224
|
+ tristate: tristate,
|
|
225
|
+ activeColor: activeColor,
|
|
226
|
+ inactiveColor: inactiveColor,
|
|
227
|
+ onChanged: onChanged,
|
|
228
|
+ additionalConstraints: additionalConstraints,
|
|
229
|
+ vsync: vsync,
|
|
230
|
+ );
|
|
231
|
+
|
|
232
|
+ bool _oldValue;
|
|
233
|
+
|
|
234
|
+ Color checkColor;
|
|
235
|
+
|
|
236
|
+ @override
|
|
237
|
+ set value(bool newValue) {
|
|
238
|
+ if (newValue == value)
|
|
239
|
+ return;
|
|
240
|
+ _oldValue = value;
|
|
241
|
+ super.value = newValue;
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ @override
|
|
245
|
+ void describeSemanticsConfiguration(SemanticsConfiguration config) {
|
|
246
|
+ super.describeSemanticsConfiguration(config);
|
|
247
|
+ config.isChecked = value == true;
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+ // The square outer bounds of the checkbox at t, with the specified origin.
|
|
251
|
+ // At t == 0.0, the outer rect's size is _kEdgeSize (Checkbox.width)
|
|
252
|
+ // At t == 0.5, .. is _kEdgeSize - _kStrokeWidth
|
|
253
|
+ // At t == 1.0, .. is _kEdgeSize
|
|
254
|
+ RRect _outerRectAt(Offset origin, double t) {
|
|
255
|
+ final double inset = 1.0 - (t - 0.5).abs() * 2.0;
|
|
256
|
+ final double size = _kEdgeSize - inset * _kStrokeWidth;
|
|
257
|
+ final Rect rect = Rect.fromLTWH(origin.dx + inset, origin.dy + inset, size, size);
|
|
258
|
+ return RRect.fromRectAndRadius(rect, _kEdgeRadius);
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ // The checkbox's border color if value == false, or its fill color when
|
|
262
|
+ // value == true or null.
|
|
263
|
+ Color _colorAt(double t) {
|
|
264
|
+ // As t goes from 0.0 to 0.25, animate from the inactiveColor to activeColor.
|
|
265
|
+ return onChanged == null
|
|
266
|
+ ? inactiveColor
|
|
267
|
+ : (t >= 0.25 ? activeColor : Color.lerp(inactiveColor, activeColor, t * 4.0));
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ // White stroke used to paint the check and dash.
|
|
271
|
+ void _initStrokePaint(Paint paint) {
|
|
272
|
+ paint
|
|
273
|
+ ..color = checkColor
|
|
274
|
+ ..style = PaintingStyle.stroke
|
|
275
|
+ ..strokeWidth = _kStrokeWidth;
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ void _drawBorder(Canvas canvas, RRect outer, double t, Paint paint) {
|
|
279
|
+ assert(t >= 0.0 && t <= 0.5);
|
|
280
|
+ final double size = outer.width;
|
|
281
|
+ // As t goes from 0.0 to 1.0, gradually fill the outer RRect.
|
|
282
|
+ final RRect inner = outer.deflate(math.min(size / 2.0, _kStrokeWidth + size * t));
|
|
283
|
+ canvas.drawDRRect(outer, inner, paint);
|
|
284
|
+ }
|
|
285
|
+
|
|
286
|
+ void _drawCheck(Canvas canvas, Offset origin, double t, Paint paint) {
|
|
287
|
+ assert(t >= 0.0 && t <= 1.0);
|
|
288
|
+ // As t goes from 0.0 to 1.0, animate the two check mark strokes from the
|
|
289
|
+ // short side to the long side.
|
|
290
|
+ final Path path = Path();
|
|
291
|
+ const Offset start = Offset(_kEdgeSize * 0.15, _kEdgeSize * 0.45);
|
|
292
|
+ const Offset mid = Offset(_kEdgeSize * 0.4, _kEdgeSize * 0.7);
|
|
293
|
+ const Offset end = Offset(_kEdgeSize * 0.85, _kEdgeSize * 0.25);
|
|
294
|
+ if (t < 0.5) {
|
|
295
|
+ final double strokeT = t * 2.0;
|
|
296
|
+ final Offset drawMid = Offset.lerp(start, mid, strokeT);
|
|
297
|
+ path.moveTo(origin.dx + start.dx, origin.dy + start.dy);
|
|
298
|
+ path.lineTo(origin.dx + drawMid.dx, origin.dy + drawMid.dy);
|
|
299
|
+ } else {
|
|
300
|
+ final double strokeT = (t - 0.5) * 2.0;
|
|
301
|
+ final Offset drawEnd = Offset.lerp(mid, end, strokeT);
|
|
302
|
+ path.moveTo(origin.dx + start.dx, origin.dy + start.dy);
|
|
303
|
+ path.lineTo(origin.dx + mid.dx, origin.dy + mid.dy);
|
|
304
|
+ path.lineTo(origin.dx + drawEnd.dx, origin.dy + drawEnd.dy);
|
|
305
|
+ }
|
|
306
|
+ canvas.drawPath(path, paint);
|
|
307
|
+ }
|
|
308
|
+
|
|
309
|
+ void _drawDash(Canvas canvas, Offset origin, double t, Paint paint) {
|
|
310
|
+ assert(t >= 0.0 && t <= 1.0);
|
|
311
|
+ // As t goes from 0.0 to 1.0, animate the horizontal line from the
|
|
312
|
+ // mid point outwards.
|
|
313
|
+ const Offset start = Offset(_kEdgeSize * 0.2, _kEdgeSize * 0.5);
|
|
314
|
+ const Offset mid = Offset(_kEdgeSize * 0.5, _kEdgeSize * 0.5);
|
|
315
|
+ const Offset end = Offset(_kEdgeSize * 0.8, _kEdgeSize * 0.5);
|
|
316
|
+ final Offset drawStart = Offset.lerp(start, mid, 1.0 - t);
|
|
317
|
+ final Offset drawEnd = Offset.lerp(mid, end, t);
|
|
318
|
+ canvas.drawLine(origin + drawStart, origin + drawEnd, paint);
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ @override
|
|
322
|
+ void paint(PaintingContext context, Offset offset) {
|
|
323
|
+ final Canvas canvas = context.canvas;
|
|
324
|
+ paintRadialReaction(canvas, offset, size.center(Offset.zero));
|
|
325
|
+
|
|
326
|
+ final Offset origin = offset + (size / 2.0 - const Size.square(_kEdgeSize) / 2.0);
|
|
327
|
+ final AnimationStatus status = position.status;
|
|
328
|
+ final double tNormalized = status == AnimationStatus.forward || status == AnimationStatus.completed
|
|
329
|
+ ? position.value
|
|
330
|
+ : 1.0 - position.value;
|
|
331
|
+
|
|
332
|
+ // Four cases: false to null, false to true, null to false, true to false
|
|
333
|
+ if (_oldValue == false || value == false) {
|
|
334
|
+ final double t = value == false ? 1.0 - tNormalized : tNormalized;
|
|
335
|
+ final RRect outer = _outerRectAt(origin, t);
|
|
336
|
+ final Paint paint = Paint()..color = _colorAt(t);
|
|
337
|
+
|
|
338
|
+ if (t <= 0.5) {
|
|
339
|
+ _drawBorder(canvas, outer, t, paint);
|
|
340
|
+ } else {
|
|
341
|
+ canvas.drawRRect(outer, paint);
|
|
342
|
+
|
|
343
|
+ _initStrokePaint(paint);
|
|
344
|
+ final double tShrink = (t - 0.5) * 2.0;
|
|
345
|
+ if (_oldValue == null)
|
|
346
|
+ _drawDash(canvas, origin, tShrink, paint);
|
|
347
|
+ else
|
|
348
|
+ _drawCheck(canvas, origin, tShrink, paint);
|
|
349
|
+ }
|
|
350
|
+ } else { // Two cases: null to true, true to null
|
|
351
|
+ final RRect outer = _outerRectAt(origin, 1.0);
|
|
352
|
+ final Paint paint = Paint() ..color = _colorAt(1.0);
|
|
353
|
+ canvas.drawRRect(outer, paint);
|
|
354
|
+
|
|
355
|
+ _initStrokePaint(paint);
|
|
356
|
+ if (tNormalized <= 0.5) {
|
|
357
|
+ final double tShrink = 1.0 - tNormalized * 2.0;
|
|
358
|
+ if (_oldValue == true)
|
|
359
|
+ _drawCheck(canvas, origin, tShrink, paint);
|
|
360
|
+ else
|
|
361
|
+ _drawDash(canvas, origin, tShrink, paint);
|
|
362
|
+ } else {
|
|
363
|
+ final double tExpand = (tNormalized - 0.5) * 2.0;
|
|
364
|
+ if (value == true)
|
|
365
|
+ _drawCheck(canvas, origin, tExpand, paint);
|
|
366
|
+ else
|
|
367
|
+ _drawDash(canvas, origin, tExpand, paint);
|
|
368
|
+ }
|
|
369
|
+ }
|
|
370
|
+ }
|
|
371
|
+}
|