checkbox.dart 24.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/widgets.dart';
6

7
import 'constants.dart';
8
import 'debug.dart';
9
import 'material_state.dart';
10
import 'theme.dart';
11
import 'theme_data.dart';
12
import 'toggleable.dart';
13

14
/// A material design checkbox.
15 16
///
/// The checkbox itself does not maintain any state. Instead, when the state of
17 18 19
/// the checkbox changes, the widget calls the [onChanged] callback. Most
/// widgets that use a checkbox will listen for the [onChanged] callback and
/// rebuild the checkbox with a new [value] to update the visual appearance of
20 21
/// the checkbox.
///
22 23 24 25
/// The checkbox can optionally display three values - true, false, and null -
/// if [tristate] is true. When [value] is null a dash is displayed. By default
/// [tristate] is false and the checkbox's [value] must be true or false.
///
26 27
/// Requires one of its ancestors to be a [Material] widget.
///
28
/// {@tool dartpad}
29 30 31 32 33 34
/// This example shows how you can override the default theme of
/// of a [Checkbox] with a [MaterialStateProperty].
/// In this example, the checkbox's color will be `Colors.blue` when the [Checkbox]
/// is being pressed, hovered, or focused. Otherwise, the checkbox's color will
/// be `Colors.red`.
///
35
/// ** See code in examples/api/lib/material/checkbox/checkbox.0.dart **
36 37
/// {@end-tool}
///
38
/// See also:
39
///
40 41 42
///  * [CheckboxListTile], which combines this widget with a [ListTile] so that
///    you can give the checkbox a label.
///  * [Switch], a widget with semantics similar to [Checkbox].
43 44
///  * [Radio], for selecting among a set of explicit values.
///  * [Slider], for selecting a value in a range.
45 46
///  * <https://material.io/design/components/selection-controls.html#checkboxes>
///  * <https://material.io/design/components/lists.html#types>
47
class Checkbox extends StatefulWidget {
48
  /// Creates a material design checkbox.
49
  ///
50 51 52 53 54 55
  /// The checkbox itself does not maintain any state. Instead, when the state of
  /// the checkbox changes, the widget calls the [onChanged] callback. Most
  /// widgets that use a checkbox will listen for the [onChanged] callback and
  /// rebuild the checkbox with a new [value] to update the visual appearance of
  /// the checkbox.
  ///
56 57
  /// The following arguments are required:
  ///
58
  /// * [value], which determines whether the checkbox is checked. The [value]
59
  ///   can only be null if [tristate] is true.
60 61
  /// * [onChanged], which is called when the value of the checkbox should
  ///   change. It can be set to null to disable the checkbox.
62
  ///
63
  /// The values of [tristate] and [autofocus] must not be null.
64
  const Checkbox({
65 66
    Key? key,
    required this.value,
67
    this.tristate = false,
68
    required this.onChanged,
69
    this.mouseCursor,
70
    this.activeColor,
71
    this.fillColor,
72
    this.checkColor,
73 74
    this.focusColor,
    this.hoverColor,
75
    this.overlayColor,
76
    this.splashRadius,
77
    this.materialTapTargetSize,
78
    this.visualDensity,
79 80
    this.focusNode,
    this.autofocus = false,
81 82
    this.shape,
    this.side,
83 84
  }) : assert(tristate != null),
       assert(tristate || value != null),
85
       assert(autofocus != null),
86
       super(key: key);
87

88
  /// Whether this checkbox is checked.
89 90
  ///
  /// This property must not be null.
91
  final bool? value;
92

93
  /// Called when the value of the checkbox should change.
94 95 96 97 98
  ///
  /// The checkbox passes the new value to the callback but does not actually
  /// change state until the parent widget rebuilds the checkbox with the new
  /// value.
  ///
99 100 101 102 103 104
  /// If this callback is null, the checkbox will be displayed as disabled
  /// and will not respond to input gestures.
  ///
  /// When the checkbox is tapped, if [tristate] is false (the default) then
  /// the [onChanged] callback will be applied to `!value`. If [tristate] is
  /// true this callback cycle from false to true to null.
105
  ///
106
  /// The callback provided to [onChanged] should update the state of the parent
107 108 109 110
  /// [StatefulWidget] using the [State.setState] method, so that the parent
  /// gets rebuilt; for example:
  ///
  /// ```dart
111
  /// Checkbox(
112
  ///   value: _throwShotAway,
Abhishek Ghaskata's avatar
Abhishek Ghaskata committed
113
  ///   onChanged: (bool? newValue) {
114
  ///     setState(() {
Abhishek Ghaskata's avatar
Abhishek Ghaskata committed
115
  ///       _throwShotAway = newValue!;
116 117
  ///     });
  ///   },
118
  /// )
119
  /// ```
120
  final ValueChanged<bool?>? onChanged;
121

122
  /// {@template flutter.material.checkbox.mouseCursor}
123 124 125 126 127 128 129 130 131 132
  /// The cursor for a mouse pointer when it enters or is hovering over the
  /// widget.
  ///
  /// If [mouseCursor] is a [MaterialStateProperty<MouseCursor>],
  /// [MaterialStateProperty.resolve] is used for the following [MaterialState]s:
  ///
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  ///  * [MaterialState.disabled].
133
  /// {@endtemplate}
134 135 136 137
  ///
  /// When [value] is null and [tristate] is true, [MaterialState.selected] is
  /// included as a state.
  ///
138 139 140 141 142 143 144 145
  /// If null, then the value of [CheckboxThemeData.mouseCursor] is used. If
  /// that is also null, then [MaterialStateMouseCursor.clickable] is used.
  ///
  /// See also:
  ///
  ///  * [MaterialStateMouseCursor], a [MouseCursor] that implements
  ///    `MaterialStateProperty` which is used in APIs that need to accept
  ///    either a [MouseCursor] or a [MaterialStateProperty<MouseCursor>].
146
  final MouseCursor? mouseCursor;
147

148 149
  /// The color to use when this checkbox is checked.
  ///
150
  /// Defaults to [ThemeData.toggleableActiveColor].
151 152 153
  ///
  /// If [fillColor] returns a non-null color in the [MaterialState.selected]
  /// state, it will be used instead of this color.
154
  final Color? activeColor;
155

156 157
  /// {@template flutter.material.checkbox.fillColor}
  /// The color that fills the checkbox, in all [MaterialState]s.
158 159 160 161 162 163
  ///
  /// Resolves in the following states:
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  ///  * [MaterialState.disabled].
164 165 166 167 168 169 170 171
  /// {@endtemplate}
  ///
  /// If null, then the value of [activeColor] is used in the selected
  /// state. If that is also null, the value of [CheckboxThemeData.fillColor]
  /// is used. If that is also null, then [ThemeData.disabledColor] is used in
  /// the disabled state, [ThemeData.toggleableActiveColor] is used in the
  /// selected state, and [ThemeData.unselectedWidgetColor] is used in the
  /// default state.
172 173
  final MaterialStateProperty<Color?>? fillColor;

174
  /// {@template flutter.material.checkbox.checkColor}
175
  /// The color to use for the check icon when this checkbox is checked.
176
  /// {@endtemplate}
177
  ///
178 179
  /// If null, then the value of [CheckboxThemeData.checkColor] is used. If
  /// that is also null, then Color(0xFFFFFFFF) is used.
180
  final Color? checkColor;
181

182 183 184 185
  /// If true the checkbox's [value] can be true, false, or null.
  ///
  /// Checkbox displays a dash when its value is null.
  ///
186 187 188 189
  /// When a tri-state checkbox ([tristate] is true) is tapped, its [onChanged]
  /// callback will be applied to true if the current value is false, to null if
  /// value is true, and to false if value is null (i.e. it cycles through false
  /// => true => null => false when tapped).
190 191 192 193
  ///
  /// If tristate is false (the default), [value] must not be null.
  final bool tristate;

194
  /// {@template flutter.material.checkbox.materialTapTargetSize}
195
  /// Configures the minimum size of the tap target.
196
  /// {@endtemplate}
197
  ///
198 199 200
  /// If null, then the value of [CheckboxThemeData.materialTapTargetSize] is
  /// used. If that is also null, then the value of
  /// [ThemeData.materialTapTargetSize] is used.
201 202 203
  ///
  /// See also:
  ///
204
  ///  * [MaterialTapTargetSize], for a description of how this affects tap targets.
205
  final MaterialTapTargetSize? materialTapTargetSize;
206

207
  /// {@template flutter.material.checkbox.visualDensity}
208
  /// Defines how compact the checkbox's layout will be.
209
  /// {@endtemplate}
210 211 212
  ///
  /// {@macro flutter.material.themedata.visualDensity}
  ///
213 214 215
  /// If null, then the value of [CheckboxThemeData.visualDensity] is used. If
  /// that is also null, then the value of [ThemeData.visualDensity] is used.
  ///
216 217
  /// See also:
  ///
218 219
  ///  * [ThemeData.visualDensity], which specifies the [visualDensity] for all
  ///    widgets within a [Theme].
220
  final VisualDensity? visualDensity;
221

222
  /// The color for the checkbox's [Material] when it has the input focus.
223
  ///
224 225 226
  /// If [overlayColor] returns a non-null color in the [MaterialState.focused]
  /// state, it will be used instead.
  ///
227 228 229
  /// If null, then the value of [CheckboxThemeData.overlayColor] is used in the
  /// focused state. If that is also null, then the value of
  /// [ThemeData.focusColor] is used.
230
  final Color? focusColor;
231 232

  /// The color for the checkbox's [Material] when a pointer is hovering over it.
233
  ///
234 235 236
  /// If [overlayColor] returns a non-null color in the [MaterialState.hovered]
  /// state, it will be used instead.
  ///
237 238 239
  /// If null, then the value of [CheckboxThemeData.overlayColor] is used in the
  /// hovered state. If that is also null, then the value of
  /// [ThemeData.hoverColor] is used.
240
  final Color? hoverColor;
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  /// {@template flutter.material.checkbox.overlayColor}
  /// The color for the checkbox's [Material].
  ///
  /// Resolves in the following states:
  ///  * [MaterialState.pressed].
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  /// {@endtemplate}
  ///
  /// If null, then the value of [activeColor] with alpha
  /// [kRadialReactionAlpha], [focusColor] and [hoverColor] is used in the
  /// pressed, focused and hovered state. If that is also null,
  /// the value of [CheckboxThemeData.overlayColor] is used. If that is
  /// also null, then the value of [ThemeData.toggleableActiveColor] with alpha
  /// [kRadialReactionAlpha], [ThemeData.focusColor] and [ThemeData.hoverColor]
  /// is used in the pressed, focused and hovered state.
  final MaterialStateProperty<Color?>? overlayColor;

261
  /// {@template flutter.material.checkbox.splashRadius}
262
  /// The splash radius of the circular [Material] ink response.
263
  /// {@endtemplate}
264
  ///
265 266
  /// If null, then the value of [CheckboxThemeData.splashRadius] is used. If
  /// that is also null, then [kRadialReactionRadius] is used.
267 268
  final double? splashRadius;

269
  /// {@macro flutter.widgets.Focus.focusNode}
270
  final FocusNode? focusNode;
271 272 273 274

  /// {@macro flutter.widgets.Focus.autofocus}
  final bool autofocus;

275 276 277 278 279 280 281 282 283 284
  /// {@template flutter.material.checkbox.shape}
  /// The shape of the checkbox's [Material].
  /// {@endtemplate}
  ///
  /// If this property is null then [CheckboxThemeData.shape] of [ThemeData.checkboxTheme]
  /// is used. If that's null then the shape will be a [RoundedRectangleBorder]
  /// with a circular corner radius of 1.0.
  final OutlinedBorder? shape;

  /// {@template flutter.material.checkbox.side}
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  /// The color and width of the checkbox's border.
  ///
  /// This property can be a [MaterialStateBorderSide] that can
  /// specify different border color and widths depending on the
  /// checkbox's state.
  ///
  /// Resolves in the following states:
  ///  * [MaterialState.pressed].
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  ///  * [MaterialState.disabled].
  ///
  /// If this property is not a [MaterialStateBorderSide] and it is
  /// non-null, then it is only rendered when the checkbox's value is
  /// false. The difference in interpretation is for backwards
  /// compatibility.
302 303 304 305 306 307
  /// {@endtemplate}
  ///
  /// If this property is null then [CheckboxThemeData.side] of [ThemeData.checkboxTheme]
  /// is used. If that's null then the side will be width 2.
  final BorderSide? side;

308 309 310
  /// The width of a checkbox widget.
  static const double width = 18.0;

311
  @override
312
  State<Checkbox> createState() => _CheckboxState();
313 314
}

315 316 317
class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, ToggleableStateMixin {
  final _CheckboxPainter _painter = _CheckboxPainter();
  bool? _previousValue;
318 319 320 321

  @override
  void initState() {
    super.initState();
322
    _previousValue = widget.value;
323 324
  }

325 326 327 328 329 330
  @override
  void didUpdateWidget(Checkbox oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.value != widget.value) {
      _previousValue = oldWidget.value;
      animateToValue();
331 332 333
    }
  }

334 335 336 337
  @override
  void dispose() {
    _painter.dispose();
    super.dispose();
338 339
  }

340 341 342 343 344
  @override
  ValueChanged<bool?>? get onChanged => widget.onChanged;

  @override
  bool get tristate => widget.tristate;
345

346 347
  @override
  bool? get value => widget.value;
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

  MaterialStateProperty<Color?> get _widgetFillColor {
    return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        return null;
      }
      if (states.contains(MaterialState.selected)) {
        return widget.activeColor;
      }
      return null;
    });
  }

  MaterialStateProperty<Color> get _defaultFillColor {
    final ThemeData themeData = Theme.of(context);
    return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        return themeData.disabledColor;
      }
      if (states.contains(MaterialState.selected)) {
        return themeData.toggleableActiveColor;
      }
      return themeData.unselectedWidgetColor;
    });
  }

374 375 376 377 378 379 380 381
  BorderSide? _resolveSide(BorderSide? side) {
    if (side is MaterialStateBorderSide)
      return MaterialStateProperty.resolveAs<BorderSide?>(side, states);
    if (!states.contains(MaterialState.selected))
      return side;
    return null;
  }

382
  @override
383
  Widget build(BuildContext context) {
384
    assert(debugCheckHasMaterial(context));
385
    final ThemeData themeData = Theme.of(context);
386 387 388 389 390 391
    final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize
      ?? themeData.checkboxTheme.materialTapTargetSize
      ?? themeData.materialTapTargetSize;
    final VisualDensity effectiveVisualDensity = widget.visualDensity
      ?? themeData.checkboxTheme.visualDensity
      ?? themeData.visualDensity;
392
    Size size;
393
    switch (effectiveMaterialTapTargetSize) {
394
      case MaterialTapTargetSize.padded:
395
        size = const Size(kMinInteractiveDimension, kMinInteractiveDimension);
396 397
        break;
      case MaterialTapTargetSize.shrinkWrap:
398
        size = const Size(kMinInteractiveDimension - 8.0, kMinInteractiveDimension - 8.0);
399 400
        break;
    }
401
    size += effectiveVisualDensity.baseSizeAdjustment;
402 403 404 405 406 407 408

    final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) {
      return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
        ?? themeData.checkboxTheme.mouseCursor?.resolve(states)
        ?? MaterialStateMouseCursor.clickable.resolve(states);
    });

409 410
    // Colors need to be resolved in selected and non selected states separately
    // so that they can be lerped between.
411 412
    final Set<MaterialState> activeStates = states..add(MaterialState.selected);
    final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected);
413 414
    final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates)
      ?? _widgetFillColor.resolve(activeStates)
415
      ?? themeData.checkboxTheme.fillColor?.resolve(activeStates)
416 417 418
      ?? _defaultFillColor.resolve(activeStates);
    final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates)
      ?? _widgetFillColor.resolve(inactiveStates)
419
      ?? themeData.checkboxTheme.fillColor?.resolve(inactiveStates)
420
      ?? _defaultFillColor.resolve(inactiveStates);
421

422
    final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
423 424 425 426 427
    final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
      ?? widget.focusColor
      ?? themeData.checkboxTheme.overlayColor?.resolve(focusedStates)
      ?? themeData.focusColor;

428
    final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
429 430 431
    final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
        ?? widget.hoverColor
        ?? themeData.checkboxTheme.overlayColor?.resolve(hoveredStates)
432 433
        ?? themeData.hoverColor;

434 435 436 437 438 439 440 441 442 443
    final Set<MaterialState> activePressedStates = activeStates..add(MaterialState.pressed);
    final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates)
        ?? themeData.checkboxTheme.overlayColor?.resolve(activePressedStates)
        ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);

    final Set<MaterialState> inactivePressedStates = inactiveStates..add(MaterialState.pressed);
    final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates)
        ?? themeData.checkboxTheme.overlayColor?.resolve(inactivePressedStates)
        ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);

444 445
    final Color effectiveCheckColor = widget.checkColor
      ?? themeData.checkboxTheme.checkColor?.resolve(states)
446 447
      ?? const Color(0xFFFFFFFF);

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
    return Semantics(
      checked: widget.value == true,
      child: buildToggleable(
        mouseCursor: effectiveMouseCursor,
        focusNode: widget.focusNode,
        autofocus: widget.autofocus,
        size: size,
        painter: _painter
          ..position = position
          ..reaction = reaction
          ..reactionFocusFade = reactionFocusFade
          ..reactionHoverFade = reactionHoverFade
          ..inactiveReactionColor = effectiveInactivePressedOverlayColor
          ..reactionColor = effectiveActivePressedOverlayColor
          ..hoverColor = effectiveHoverOverlayColor
          ..focusColor = effectiveFocusOverlayColor
          ..splashRadius = widget.splashRadius ?? themeData.checkboxTheme.splashRadius ?? kRadialReactionRadius
          ..downPosition = downPosition
          ..isFocused = states.contains(MaterialState.focused)
          ..isHovered = states.contains(MaterialState.hovered)
          ..activeColor = effectiveActiveColor
          ..inactiveColor = effectiveInactiveColor
          ..checkColor = effectiveCheckColor
          ..value = value
          ..previousValue = _previousValue
          ..shape = widget.shape ?? themeData.checkboxTheme.shape ?? const RoundedRectangleBorder(
474
              borderRadius: BorderRadius.all(Radius.circular(1.0)),
475
          )
476
          ..side = _resolveSide(widget.side) ?? _resolveSide(themeData.checkboxTheme.side),
477
      ),
478
    );
479 480 481
  }
}

482 483
const double _kEdgeSize = Checkbox.width;
const double _kStrokeWidth = 2.0;
484

485 486 487 488 489 490 491 492 493
class _CheckboxPainter extends ToggleablePainter {
  Color get checkColor => _checkColor!;
  Color? _checkColor;
  set checkColor(Color value) {
    if (_checkColor == value) {
      return;
    }
    _checkColor = value;
    notifyListeners();
494
  }
495

496 497 498 499 500 501 502 503 504
  bool? get value => _value;
  bool? _value;
  set value(bool? value) {
    if (_value == value) {
      return;
    }
    _value = value;
    notifyListeners();
  }
505

506 507 508 509 510 511 512 513 514
  bool? get previousValue => _previousValue;
  bool? _previousValue;
  set previousValue(bool? value) {
    if (_previousValue == value) {
      return;
    }
    _previousValue = value;
    notifyListeners();
  }
515

516 517 518 519
  OutlinedBorder get shape => _shape!;
  OutlinedBorder? _shape;
  set shape(OutlinedBorder value) {
    if (_shape == value) {
520
      return;
521 522 523
    }
    _shape = value;
    notifyListeners();
524
  }
525

526 527 528 529 530 531 532 533
  BorderSide? get side => _side;
  BorderSide? _side;
  set side(BorderSide? value) {
    if (_side == value) {
      return;
    }
    _side = value;
    notifyListeners();
534 535
  }

536 537 538 539
  // The square outer bounds of the checkbox at t, with the specified origin.
  // At t == 0.0, the outer rect's size is _kEdgeSize (Checkbox.width)
  // At t == 0.5, .. is _kEdgeSize - _kStrokeWidth
  // At t == 1.0, .. is _kEdgeSize
540
  Rect _outerRectAt(Offset origin, double t) {
541 542
    final double inset = 1.0 - (t - 0.5).abs() * 2.0;
    final double size = _kEdgeSize - inset * _kStrokeWidth;
543
    final Rect rect = Rect.fromLTWH(origin.dx + inset, origin.dy + inset, size, size);
544
    return rect;
545
  }
546

547 548 549 550
  // The checkbox's border color if value == false, or its fill color when
  // value == true or null.
  Color _colorAt(double t) {
    // As t goes from 0.0 to 0.25, animate from the inactiveColor to activeColor.
551
    return t >= 0.25 ? activeColor : Color.lerp(inactiveColor, activeColor, t * 4.0)!;
552 553 554
  }

  // White stroke used to paint the check and dash.
555 556
  Paint _createStrokePaint() {
    return Paint()
557
      ..color = checkColor
558 559 560 561
      ..style = PaintingStyle.stroke
      ..strokeWidth = _kStrokeWidth;
  }

562 563 564 565 566 567
  void _drawBox(Canvas canvas, Rect outer, Paint paint, BorderSide? side, bool fill) {
    if (fill) {
      canvas.drawPath(shape.getOuterPath(outer), paint);
    }
    if (side != null) {
      shape.copyWith(side: side).paint(canvas, outer);
568
    }
569 570 571 572
  }

  void _drawCheck(Canvas canvas, Offset origin, double t, Paint paint) {
    assert(t >= 0.0 && t <= 1.0);
573 574
    // As t goes from 0.0 to 1.0, animate the two check mark strokes from the
    // short side to the long side.
575
    final Path path = Path();
576 577 578
    const Offset start = Offset(_kEdgeSize * 0.15, _kEdgeSize * 0.45);
    const Offset mid = Offset(_kEdgeSize * 0.4, _kEdgeSize * 0.7);
    const Offset end = Offset(_kEdgeSize * 0.85, _kEdgeSize * 0.25);
579 580
    if (t < 0.5) {
      final double strokeT = t * 2.0;
581
      final Offset drawMid = Offset.lerp(start, mid, strokeT)!;
582 583 584 585
      path.moveTo(origin.dx + start.dx, origin.dy + start.dy);
      path.lineTo(origin.dx + drawMid.dx, origin.dy + drawMid.dy);
    } else {
      final double strokeT = (t - 0.5) * 2.0;
586
      final Offset drawEnd = Offset.lerp(mid, end, strokeT)!;
587 588 589 590
      path.moveTo(origin.dx + start.dx, origin.dy + start.dy);
      path.lineTo(origin.dx + mid.dx, origin.dy + mid.dy);
      path.lineTo(origin.dx + drawEnd.dx, origin.dy + drawEnd.dy);
    }
591 592
    canvas.drawPath(path, paint);
  }
593

594 595 596 597
  void _drawDash(Canvas canvas, Offset origin, double t, Paint paint) {
    assert(t >= 0.0 && t <= 1.0);
    // As t goes from 0.0 to 1.0, animate the horizontal line from the
    // mid point outwards.
598 599 600
    const Offset start = Offset(_kEdgeSize * 0.2, _kEdgeSize * 0.5);
    const Offset mid = Offset(_kEdgeSize * 0.5, _kEdgeSize * 0.5);
    const Offset end = Offset(_kEdgeSize * 0.8, _kEdgeSize * 0.5);
601 602
    final Offset drawStart = Offset.lerp(start, mid, 1.0 - t)!;
    final Offset drawEnd = Offset.lerp(mid, end, t)!;
603 604 605 606
    canvas.drawLine(origin + drawStart, origin + drawEnd, paint);
  }

  @override
607 608
  void paint(Canvas canvas, Size size) {
    paintRadialReaction(canvas: canvas, origin: size.center(Offset.zero));
609

610
    final Paint strokePaint = _createStrokePaint();
611
    final Offset origin = size / 2.0 - const Size.square(_kEdgeSize) / 2.0 as Offset;
612 613 614 615
    final AnimationStatus status = position.status;
    final double tNormalized = status == AnimationStatus.forward || status == AnimationStatus.completed
      ? position.value
      : 1.0 - position.value;
616

617
    // Four cases: false to null, false to true, null to false, true to false
618
    if (previousValue == false || value == false) {
619
      final double t = value == false ? 1.0 - tNormalized : tNormalized;
620
      final Rect outer = _outerRectAt(origin, t);
621
      final Paint paint = Paint()..color = _colorAt(t);
622

623
      if (t <= 0.5) {
624 625
        final BorderSide border = side ?? BorderSide(width: 2, color: paint.color);
        _drawBox(canvas, outer, paint, border, false); // only paint the border
626
      } else {
627
        _drawBox(canvas, outer, paint, side, true);
628
        final double tShrink = (t - 0.5) * 2.0;
629
        if (previousValue == null || value == null)
630
          _drawDash(canvas, origin, tShrink, strokePaint);
631
        else
632
          _drawCheck(canvas, origin, tShrink, strokePaint);
633 634
      }
    } else { // Two cases: null to true, true to null
635
      final Rect outer = _outerRectAt(origin, 1.0);
636
      final Paint paint = Paint() ..color = _colorAt(1.0);
637

638
      _drawBox(canvas, outer, paint, side, true);
639 640
      if (tNormalized <= 0.5) {
        final double tShrink = 1.0 - tNormalized * 2.0;
641
        if (previousValue == true)
642
          _drawCheck(canvas, origin, tShrink, strokePaint);
643
        else
644
          _drawDash(canvas, origin, tShrink, strokePaint);
645 646 647
      } else {
        final double tExpand = (tNormalized - 0.5) * 2.0;
        if (value == true)
648
          _drawCheck(canvas, origin, tExpand, strokePaint);
649
        else
650
          _drawDash(canvas, origin, tExpand, strokePaint);
651
      }
652 653 654
    }
  }
}