checkbox.dart 6.77 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:math' as math;
6

7
import 'package:flutter/foundation.dart';
8 9
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
10

11
import 'constants.dart';
12
import 'debug.dart';
13
import 'theme.dart';
14
import 'toggleable.dart';
15

16 17 18
/// A material design checkbox
///
/// The checkbox itself does not maintain any state. Instead, when the state of
19 20 21
/// 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
22 23
/// the checkbox.
///
24 25 26
/// Requires one of its ancestors to be a [Material] widget.
///
/// See also:
27
///
28 29 30
///  * [Radio]
///  * [Switch]
///  * [Slider]
31 32
///  * <https://material.google.com/components/selection-controls.html#selection-controls-checkbox>
///  * <https://material.google.com/components/lists-controls.html#lists-controls-types-of-list-controls>
33
class Checkbox extends StatefulWidget {
34
  /// Creates a material design checkbox.
35
  ///
36 37 38 39 40 41 42 43 44
  /// 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.
  ///
  /// * [value] determines whether the checkbox is checked.
  /// * [onChanged] is called when the value of the checkbox should change.
  Checkbox({
45
    Key key,
46 47 48
    @required this.value,
    @required this.onChanged,
    this.activeColor
49
  }) : super(key: key);
50

51
  /// Whether this checkbox is checked.
52
  final bool value;
53

54
  /// Called when the value of the checkbox should change.
55 56 57 58 59 60
  ///
  /// 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.
  ///
  /// If null, the checkbox will be displayed as disabled.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  ///
  /// The callback provided to onChanged should update the state of the parent
  /// [StatefulWidget] using the [State.setState] method, so that the parent
  /// gets rebuilt; for example:
  ///
  /// ```dart
  /// new Checkbox(
  ///   value: _throwShotAway,
  ///   onChanged: (bool newValue) {
  ///     setState(() {
  ///       _throwShotAway = newValue;
  ///     });
  ///   },
  /// ),
  /// ```
Hixie's avatar
Hixie committed
76
  final ValueChanged<bool> onChanged;
77

78 79 80 81 82
  /// The color to use when this checkbox is checked.
  ///
  /// Defaults to accent color of the current [Theme].
  final Color activeColor;

83 84 85
  /// The width of a checkbox widget.
  static const double width = 18.0;

86 87 88 89 90
  @override
  _CheckboxState createState() => new _CheckboxState();
}

class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin {
91
  @override
92
  Widget build(BuildContext context) {
93
    assert(debugCheckHasMaterial(context));
94
    ThemeData themeData = Theme.of(context);
95
    return new _CheckboxRenderObjectWidget(
96 97 98 99 100
      value: config.value,
      activeColor: config.activeColor ?? themeData.accentColor,
      inactiveColor: config.onChanged != null ? themeData.unselectedWidgetColor : themeData.disabledColor,
      onChanged: config.onChanged,
      vsync: this,
101
    );
102 103 104
  }
}

105 106
class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
  _CheckboxRenderObjectWidget({
107
    Key key,
108 109 110 111 112
    @required this.value,
    @required this.activeColor,
    @required this.inactiveColor,
    @required this.onChanged,
    @required this.vsync,
113
  }) : super(key: key) {
114 115 116
    assert(value != null);
    assert(activeColor != null);
    assert(inactiveColor != null);
117
    assert(vsync != null);
118
  }
119 120

  final bool value;
121 122
  final Color activeColor;
  final Color inactiveColor;
123
  final ValueChanged<bool> onChanged;
124
  final TickerProvider vsync;
125

126
  @override
127
  _RenderCheckbox createRenderObject(BuildContext context) => new _RenderCheckbox(
128
    value: value,
129 130
    activeColor: activeColor,
    inactiveColor: inactiveColor,
131 132
    onChanged: onChanged,
    vsync: vsync,
133
  );
134

135
  @override
136
  void updateRenderObject(BuildContext context, _RenderCheckbox renderObject) {
137 138 139 140
    renderObject
      ..value = value
      ..activeColor = activeColor
      ..inactiveColor = inactiveColor
141 142
      ..onChanged = onChanged
      ..vsync = vsync;
143 144 145
  }
}

146
const double _kMidpoint = 0.5;
147
const double _kEdgeSize = Checkbox.width;
148
const Radius _kEdgeRadius = const Radius.circular(1.0);
149 150
const double _kStrokeWidth = 2.0;

151
class _RenderCheckbox extends RenderToggleable {
152 153
  _RenderCheckbox({
    bool value,
154 155
    Color activeColor,
    Color inactiveColor,
156 157
    ValueChanged<bool> onChanged,
    @required TickerProvider vsync,
158 159 160 161 162
  }): super(
    value: value,
    activeColor: activeColor,
    inactiveColor: inactiveColor,
    onChanged: onChanged,
163 164
    size: const Size(2 * kRadialReactionRadius, 2 * kRadialReactionRadius),
    vsync: vsync,
165
  );
166

167
  @override
168
  void paint(PaintingContext context, Offset offset) {
169

170 171
    final Canvas canvas = context.canvas;

172 173 174 175
    final double offsetX = offset.dx + (size.width - _kEdgeSize) / 2.0;
    final double offsetY = offset.dy + (size.height - _kEdgeSize) / 2.0;

    paintRadialReaction(canvas, offset, size.center(Point.origin));
176

177 178 179 180 181 182
    double t = position.value;

    Color borderColor = inactiveColor;
    if (onChanged != null)
      borderColor = t >= 0.25 ? activeColor : Color.lerp(inactiveColor, activeColor, t * 4.0);

183
    Paint paint = new Paint()
184
      ..color = borderColor;
185

186
    double inset = 1.0 - (t - 0.5).abs() * 2.0;
187
    double rectSize = _kEdgeSize - inset * _kStrokeWidth;
188
    Rect rect = new Rect.fromLTWH(offsetX + inset, offsetY + inset, rectSize, rectSize);
189

190
    RRect outer = new RRect.fromRectAndRadius(rect, _kEdgeRadius);
191 192 193 194 195 196 197
    if (t <= 0.5) {
      // Outline
      RRect inner = outer.deflate(math.min(rectSize / 2.0, _kStrokeWidth + rectSize * t));
      canvas.drawDRRect(outer, inner, paint);
    } else {
      // Background
      canvas.drawRRect(outer, paint);
198 199

      // White inner check
200
      double value = (t - 0.5) * 2.0;
201 202
      paint
        ..color = const Color(0xFFFFFFFF)
203 204
        ..style = PaintingStyle.stroke
        ..strokeWidth = _kStrokeWidth;
205
      Path path = new Path();
206 207 208
      Point start = const Point(_kEdgeSize * 0.15, _kEdgeSize * 0.45);
      Point mid = const Point(_kEdgeSize * 0.4, _kEdgeSize * 0.7);
      Point end = const Point(_kEdgeSize * 0.85, _kEdgeSize * 0.25);
209 210
      Point drawStart = Point.lerp(start, mid, 1.0 - value);
      Point drawEnd = Point.lerp(mid, end, value);
211 212 213
      path.moveTo(offsetX + drawStart.x, offsetY + drawStart.y);
      path.lineTo(offsetX + mid.x, offsetY + mid.y);
      path.lineTo(offsetX + drawEnd.x, offsetY + drawEnd.y);
214 215 216 217
      canvas.drawPath(path, paint);
    }
  }
}