checkbox.dart 24 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 29 30 31 32 33 34 35 36 37
/// {@tool dartpad --template=stateful_widget_scaffold_center}
///
/// 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`.
///
/// ```dart
/// bool isChecked = false;
38 39
///
/// @override
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/// Widget build(BuildContext context) {
///   Color getColor(Set<MaterialState> states) {
///     const Set<MaterialState> interactiveStates = <MaterialState>{
///       MaterialState.pressed,
///       MaterialState.hovered,
///       MaterialState.focused,
///     };
///     if (states.any(interactiveStates.contains)) {
///       return Colors.blue;
///     }
///     return Colors.red;
///   }
///   return Checkbox(
///     checkColor: Colors.white,
///     fillColor: MaterialStateProperty.resolveWith(getColor),
///     value: isChecked,
56
///     onChanged: (bool? value) {
57
///       setState(() {
58
///         isChecked = value!;
59 60 61 62 63 64 65
///       });
///     },
///   );
/// }
/// ```
/// {@end-tool}
///
66
/// See also:
67
///
68 69 70
///  * [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].
71 72
///  * [Radio], for selecting among a set of explicit values.
///  * [Slider], for selecting a value in a range.
73 74
///  * <https://material.io/design/components/selection-controls.html#checkboxes>
///  * <https://material.io/design/components/lists.html#types>
75
class Checkbox extends StatefulWidget {
76
  /// Creates a material design checkbox.
77
  ///
78 79 80 81 82 83
  /// 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.
  ///
84 85
  /// The following arguments are required:
  ///
86
  /// * [value], which determines whether the checkbox is checked. The [value]
87
  ///   can only be null if [tristate] is true.
88 89
  /// * [onChanged], which is called when the value of the checkbox should
  ///   change. It can be set to null to disable the checkbox.
90
  ///
91
  /// The values of [tristate] and [autofocus] must not be null.
92
  const Checkbox({
93 94
    Key? key,
    required this.value,
95
    this.tristate = false,
96
    required this.onChanged,
97
    this.mouseCursor,
98
    this.activeColor,
99
    this.fillColor,
100
    this.checkColor,
101 102
    this.focusColor,
    this.hoverColor,
103
    this.overlayColor,
104
    this.splashRadius,
105
    this.materialTapTargetSize,
106
    this.visualDensity,
107 108
    this.focusNode,
    this.autofocus = false,
109 110
    this.shape,
    this.side,
111 112
  }) : assert(tristate != null),
       assert(tristate || value != null),
113
       assert(autofocus != null),
114
       super(key: key);
115

116
  /// Whether this checkbox is checked.
117 118
  ///
  /// This property must not be null.
119
  final bool? value;
120

121
  /// Called when the value of the checkbox should change.
122 123 124 125 126
  ///
  /// 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.
  ///
127 128 129 130 131 132
  /// 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.
133
  ///
134
  /// The callback provided to [onChanged] should update the state of the parent
135 136 137 138
  /// [StatefulWidget] using the [State.setState] method, so that the parent
  /// gets rebuilt; for example:
  ///
  /// ```dart
139
  /// Checkbox(
140 141 142 143 144 145
  ///   value: _throwShotAway,
  ///   onChanged: (bool newValue) {
  ///     setState(() {
  ///       _throwShotAway = newValue;
  ///     });
  ///   },
146
  /// )
147
  /// ```
148
  final ValueChanged<bool?>? onChanged;
149

150
  /// {@template flutter.material.checkbox.mouseCursor}
151 152 153 154 155 156 157 158 159 160
  /// 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].
161
  /// {@endtemplate}
162 163 164 165
  ///
  /// When [value] is null and [tristate] is true, [MaterialState.selected] is
  /// included as a state.
  ///
166 167 168 169 170 171 172 173
  /// 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>].
174
  final MouseCursor? mouseCursor;
175

176 177
  /// The color to use when this checkbox is checked.
  ///
178
  /// Defaults to [ThemeData.toggleableActiveColor].
179 180 181
  ///
  /// If [fillColor] returns a non-null color in the [MaterialState.selected]
  /// state, it will be used instead of this color.
182
  final Color? activeColor;
183

184 185
  /// {@template flutter.material.checkbox.fillColor}
  /// The color that fills the checkbox, in all [MaterialState]s.
186 187 188 189 190 191
  ///
  /// Resolves in the following states:
  ///  * [MaterialState.selected].
  ///  * [MaterialState.hovered].
  ///  * [MaterialState.focused].
  ///  * [MaterialState.disabled].
192 193 194 195 196 197 198 199
  /// {@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.
200 201
  final MaterialStateProperty<Color?>? fillColor;

202
  /// {@template flutter.material.checkbox.checkColor}
203
  /// The color to use for the check icon when this checkbox is checked.
204
  /// {@endtemplate}
205
  ///
206 207
  /// If null, then the value of [CheckboxThemeData.checkColor] is used. If
  /// that is also null, then Color(0xFFFFFFFF) is used.
208
  final Color? checkColor;
209

210 211 212 213
  /// If true the checkbox's [value] can be true, false, or null.
  ///
  /// Checkbox displays a dash when its value is null.
  ///
214 215 216 217
  /// 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).
218 219 220 221
  ///
  /// If tristate is false (the default), [value] must not be null.
  final bool tristate;

222
  /// {@template flutter.material.checkbox.materialTapTargetSize}
223
  /// Configures the minimum size of the tap target.
224
  /// {@endtemplate}
225
  ///
226 227 228
  /// If null, then the value of [CheckboxThemeData.materialTapTargetSize] is
  /// used. If that is also null, then the value of
  /// [ThemeData.materialTapTargetSize] is used.
229 230 231
  ///
  /// See also:
  ///
232
  ///  * [MaterialTapTargetSize], for a description of how this affects tap targets.
233
  final MaterialTapTargetSize? materialTapTargetSize;
234

235
  /// {@template flutter.material.checkbox.visualDensity}
236
  /// Defines how compact the checkbox's layout will be.
237
  /// {@endtemplate}
238 239 240
  ///
  /// {@macro flutter.material.themedata.visualDensity}
  ///
241 242 243
  /// If null, then the value of [CheckboxThemeData.visualDensity] is used. If
  /// that is also null, then the value of [ThemeData.visualDensity] is used.
  ///
244 245
  /// See also:
  ///
246 247
  ///  * [ThemeData.visualDensity], which specifies the [visualDensity] for all
  ///    widgets within a [Theme].
248
  final VisualDensity? visualDensity;
249

250
  /// The color for the checkbox's [Material] when it has the input focus.
251
  ///
252 253 254
  /// If [overlayColor] returns a non-null color in the [MaterialState.focused]
  /// state, it will be used instead.
  ///
255 256 257
  /// 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.
258
  final Color? focusColor;
259 260

  /// The color for the checkbox's [Material] when a pointer is hovering over it.
261
  ///
262 263 264
  /// If [overlayColor] returns a non-null color in the [MaterialState.hovered]
  /// state, it will be used instead.
  ///
265 266 267
  /// 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.
268
  final Color? hoverColor;
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  /// {@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;

289
  /// {@template flutter.material.checkbox.splashRadius}
290
  /// The splash radius of the circular [Material] ink response.
291
  /// {@endtemplate}
292
  ///
293 294
  /// If null, then the value of [CheckboxThemeData.splashRadius] is used. If
  /// that is also null, then [kRadialReactionRadius] is used.
295 296
  final double? splashRadius;

297
  /// {@macro flutter.widgets.Focus.focusNode}
298
  final FocusNode? focusNode;
299 300 301 302

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

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  /// {@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}
  /// The side of the checkbox's border.
  /// {@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;

320 321 322
  /// The width of a checkbox widget.
  static const double width = 18.0;

323
  @override
324
  State<Checkbox> createState() => _CheckboxState();
325 326
}

327 328 329
class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, ToggleableStateMixin {
  final _CheckboxPainter _painter = _CheckboxPainter();
  bool? _previousValue;
330 331 332 333

  @override
  void initState() {
    super.initState();
334
    _previousValue = widget.value;
335 336
  }

337 338 339 340 341 342
  @override
  void didUpdateWidget(Checkbox oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.value != widget.value) {
      _previousValue = oldWidget.value;
      animateToValue();
343 344 345
    }
  }

346 347 348 349
  @override
  void dispose() {
    _painter.dispose();
    super.dispose();
350 351
  }

352 353 354 355 356
  @override
  ValueChanged<bool?>? get onChanged => widget.onChanged;

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

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

  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;
    });
  }

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

    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);
    });

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

426
    final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
427 428 429 430 431
    final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
      ?? widget.focusColor
      ?? themeData.checkboxTheme.overlayColor?.resolve(focusedStates)
      ?? themeData.focusColor;

432
    final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
433 434 435
    final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
        ?? widget.hoverColor
        ?? themeData.checkboxTheme.overlayColor?.resolve(hoveredStates)
436 437
        ?? themeData.hoverColor;

438 439 440 441 442 443 444 445 446 447
    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);

448 449
    final Color effectiveCheckColor = widget.checkColor
      ?? themeData.checkboxTheme.checkColor?.resolve(states)
450 451
      ?? const Color(0xFFFFFFFF);

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
    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(
478
              borderRadius: BorderRadius.all(Radius.circular(1.0)),
479 480
          )
          ..side = widget.side ?? themeData.checkboxTheme.side,
481
      ),
482
    );
483 484 485
  }
}

486 487
const double _kEdgeSize = Checkbox.width;
const double _kStrokeWidth = 2.0;
488

489 490 491 492 493 494 495 496 497
class _CheckboxPainter extends ToggleablePainter {
  Color get checkColor => _checkColor!;
  Color? _checkColor;
  set checkColor(Color value) {
    if (_checkColor == value) {
      return;
    }
    _checkColor = value;
    notifyListeners();
498
  }
499

500 501 502 503 504 505 506 507 508
  bool? get value => _value;
  bool? _value;
  set value(bool? value) {
    if (_value == value) {
      return;
    }
    _value = value;
    notifyListeners();
  }
509

510 511 512 513 514 515 516 517 518
  bool? get previousValue => _previousValue;
  bool? _previousValue;
  set previousValue(bool? value) {
    if (_previousValue == value) {
      return;
    }
    _previousValue = value;
    notifyListeners();
  }
519

520 521 522 523
  OutlinedBorder get shape => _shape!;
  OutlinedBorder? _shape;
  set shape(OutlinedBorder value) {
    if (_shape == value) {
524
      return;
525 526 527
    }
    _shape = value;
    notifyListeners();
528
  }
529

530 531 532 533 534 535 536 537
  BorderSide? get side => _side;
  BorderSide? _side;
  set side(BorderSide? value) {
    if (_side == value) {
      return;
    }
    _side = value;
    notifyListeners();
538 539
  }

540 541 542 543
  // 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
544
  Rect _outerRectAt(Offset origin, double t) {
545 546
    final double inset = 1.0 - (t - 0.5).abs() * 2.0;
    final double size = _kEdgeSize - inset * _kStrokeWidth;
547
    final Rect rect = Rect.fromLTWH(origin.dx + inset, origin.dy + inset, size, size);
548
    return rect;
549
  }
550

551 552 553 554
  // 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.
555
    return t >= 0.25 ? activeColor : Color.lerp(inactiveColor, activeColor, t * 4.0)!;
556 557 558
  }

  // White stroke used to paint the check and dash.
559 560
  Paint _createStrokePaint() {
    return Paint()
561
      ..color = checkColor
562 563 564 565
      ..style = PaintingStyle.stroke
      ..strokeWidth = _kStrokeWidth;
  }

566
  void _drawBorder(Canvas canvas, Rect outer, double t, Paint paint) {
567
    assert(t >= 0.0 && t <= 0.5);
568
    OutlinedBorder resolvedShape = shape;
569
    if (side == null) {
570
      resolvedShape = resolvedShape.copyWith(side: BorderSide(width: 2, color: paint.color));
571
    }
572
    resolvedShape.copyWith(side: side).paint(canvas, outer);
573 574 575 576
  }

  void _drawCheck(Canvas canvas, Offset origin, double t, Paint paint) {
    assert(t >= 0.0 && t <= 1.0);
577 578
    // As t goes from 0.0 to 1.0, animate the two check mark strokes from the
    // short side to the long side.
579
    final Path path = Path();
580 581 582
    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);
583 584
    if (t < 0.5) {
      final double strokeT = t * 2.0;
585
      final Offset drawMid = Offset.lerp(start, mid, strokeT)!;
586 587 588 589
      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;
590
      final Offset drawEnd = Offset.lerp(mid, end, strokeT)!;
591 592 593 594
      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);
    }
595 596
    canvas.drawPath(path, paint);
  }
597

598 599 600 601
  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.
602 603 604
    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);
605 606
    final Offset drawStart = Offset.lerp(start, mid, 1.0 - t)!;
    final Offset drawEnd = Offset.lerp(mid, end, t)!;
607 608 609 610
    canvas.drawLine(origin + drawStart, origin + drawEnd, paint);
  }

  @override
611 612
  void paint(Canvas canvas, Size size) {
    paintRadialReaction(canvas: canvas, origin: size.center(Offset.zero));
613

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

621
    // Four cases: false to null, false to true, null to false, true to false
622
    if (previousValue == false || value == false) {
623
      final double t = value == false ? 1.0 - tNormalized : tNormalized;
624 625
      final Rect outer = _outerRectAt(origin, t);
      final Path emptyCheckboxPath = shape.copyWith(side: side).getOuterPath(outer);
626
      final Paint paint = Paint()..color = _colorAt(t);
627

628 629 630
      if (t <= 0.5) {
        _drawBorder(canvas, outer, t, paint);
      } else {
631
        canvas.drawPath(emptyCheckboxPath, paint);
632

633
        final double tShrink = (t - 0.5) * 2.0;
634
        if (previousValue == null || value == null)
635
          _drawDash(canvas, origin, tShrink, strokePaint);
636
        else
637
          _drawCheck(canvas, origin, tShrink, strokePaint);
638 639
      }
    } else { // Two cases: null to true, true to null
640
      final Rect outer = _outerRectAt(origin, 1.0);
641
      final Paint paint = Paint() ..color = _colorAt(1.0);
642
      canvas.drawPath(shape.copyWith(side: side).getOuterPath(outer), paint);
643

644 645
      if (tNormalized <= 0.5) {
        final double tShrink = 1.0 - tNormalized * 2.0;
646
        if (previousValue == true)
647
          _drawCheck(canvas, origin, tShrink, strokePaint);
648
        else
649
          _drawDash(canvas, origin, tShrink, strokePaint);
650 651 652
      } else {
        final double tExpand = (tNormalized - 0.5) * 2.0;
        if (value == true)
653
          _drawCheck(canvas, origin, tExpand, strokePaint);
654
        else
655
          _drawDash(canvas, origin, tExpand, strokePaint);
656
      }
657 658 659
    }
  }
}