checkbox.dart 6.25 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:ui' as ui;
6

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

10 11
import 'theme.dart';

12
export 'package:flutter/rendering.dart' show ValueChanged;
13 14

const double _kMidpoint = 0.5;
15 16
const Color _kLightUncheckedColor = const Color(0x8A000000);
const Color _kDarkUncheckedColor = const Color(0xB2FFFFFF);
17
const double _kEdgeSize = 18.0;
18
const double _kEdgeRadius = 1.0;
19
const double _kStrokeWidth = 2.0;
20 21 22 23 24 25 26 27 28 29

/// A material design checkbox
///
/// The checkbox itself does not maintain any state. Instead, when the state of
/// the checkbox changes, the component calls the `onChange` callback. Most
/// components that use a checkbox will listen for the `onChange` callback and
/// rebuild the checkbox with a new `value` to update the visual appearance of
/// the checkbox.
///
/// <https://www.google.com/design/spec/components/lists-controls.html#lists-controls-types-of-list-controls>
30
class Checkbox extends StatelessComponent {
31 32 33 34
  /// Constructs a checkbox
  ///
  /// * `value` determines whether the checkbox is checked.
  /// * `onChanged` is called whenever the state of the checkbox should change.
35
  const Checkbox({Key key, this.value, this.onChanged}) : super(key: key);
36

37 38
  final bool value;
  final ValueChanged onChanged;
39

40 41
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
42 43 44 45
    Color uncheckedColor = themeData.brightness == ThemeBrightness.light
        ? _kLightUncheckedColor
        : _kDarkUncheckedColor;
    return new _CheckboxWrapper(
46 47 48 49 50
      value: value,
      onChanged: onChanged,
      uncheckedColor: uncheckedColor,
      accentColor: themeData.accentColor
    );
51 52 53 54 55 56
  }
}

// This wrapper class exists only because Switch needs to be a Component in
// order to get an accent color from a Theme but Components do not know how to
// host RenderObjects.
57 58 59 60 61 62 63
class _CheckboxWrapper extends LeafRenderObjectWidget {
  _CheckboxWrapper({
    Key key,
    this.value,
    this.onChanged,
    this.uncheckedColor,
    this.accentColor
64
  }) : super(key: key) {
65 66 67
    assert(uncheckedColor != null);
    assert(accentColor != null);
  }
68 69 70 71 72 73

  final bool value;
  final ValueChanged onChanged;
  final Color uncheckedColor;
  final Color accentColor;

74 75 76 77 78 79
  _RenderCheckbox createRenderObject() => new _RenderCheckbox(
    value: value,
    accentColor: accentColor,
    uncheckedColor: uncheckedColor,
    onChanged: onChanged
  );
80

81
  void updateRenderObject(_RenderCheckbox renderObject, _CheckboxWrapper oldWidget) {
82 83 84 85
    renderObject.value = value;
    renderObject.onChanged = onChanged;
    renderObject.uncheckedColor = uncheckedColor;
    renderObject.accentColor = accentColor;
86 87 88 89
  }
}

class _RenderCheckbox extends RenderToggleable {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  _RenderCheckbox({
    bool value,
    Color uncheckedColor,
    Color accentColor,
    ValueChanged onChanged
  }): _uncheckedColor = uncheckedColor,
      _accentColor = accentColor,
      super(
        value: value,
        onChanged: onChanged,
        size: new Size(_kEdgeSize, _kEdgeSize)
      ) {
    assert(uncheckedColor != null);
    assert(accentColor != null);
  }
105 106 107 108 109

  Color _uncheckedColor;
  Color get uncheckedColor => _uncheckedColor;

  void set uncheckedColor(Color value) {
110 111 112
    assert(value != null);
    if (value == _uncheckedColor)
      return;
113 114 115 116 117 118
    _uncheckedColor = value;
    markNeedsPaint();
  }

  Color _accentColor;
  void set accentColor(Color value) {
119 120 121
    assert(value != null);
    if (value == _accentColor)
      return;
122 123 124
    _accentColor = value;
    markNeedsPaint();
  }
125

126 127
  void paint(PaintingContext context, Offset offset) {
    final PaintingCanvas canvas = context.canvas;
128
    // Choose a color between grey and the theme color
129
    Paint paint = new Paint()
130
      ..strokeWidth = _kStrokeWidth
131
      ..color = uncheckedColor;
132

133 134
    // The rrect contracts slightly during the transition animation from checked states.
    // Because we have a stroke size of 2, we should have a minimum 1.0 inset.
135
    double inset = 2.0 - (position - _kMidpoint).abs() * 2.0;
136
    double rectSize = _kEdgeSize - inset * _kStrokeWidth;
137
    Rect rect = new Rect.fromLTWH(offset.dx + inset, offset.dy + inset, rectSize, rectSize);
138 139
    // Create an inner rectangle to cover inside of rectangle. This is needed to avoid
    // painting artefacts caused by overlayed paintings.
140
    Rect innerRect = rect.deflate(1.0);
141
    ui.RRect rrect = new ui.RRect()
142
      ..setRectXY(rect, _kEdgeRadius, _kEdgeRadius);
143 144

    // Outline of the empty rrect
145
    paint.style = ui.PaintingStyle.stroke;
146 147 148
    canvas.drawRRect(rrect, paint);

    // Radial gradient that changes size
149
    if (position > 0) {
150 151 152
      paint
        ..style = ui.PaintingStyle.fill
        ..shader = new ui.Gradient.radial(
153
          new Point(_kEdgeSize / 2.0, _kEdgeSize / 2.0),
Hixie's avatar
Hixie committed
154
          _kEdgeSize * (_kMidpoint - position) * 8.0, <Color>[
155
        const Color(0x00000000),
156
        uncheckedColor
157
      ]);
158
      canvas.drawRect(innerRect, paint);
159 160
    }

161 162
    if (position > _kMidpoint) {
      double t = (position - _kMidpoint) / (1.0 - _kMidpoint);
163

164
      // First draw a rounded rect outline then fill inner rectangle with accent color.
165 166 167
      paint
        ..color = new Color.fromARGB((t * 255).floor(), _accentColor.red, _accentColor.green, _accentColor.blue)
        ..style = ui.PaintingStyle.stroke;
168
      canvas.drawRRect(rrect, paint);
169
      paint.style = ui.PaintingStyle.fill;
170
      canvas.drawRect(innerRect, paint);
171 172

      // White inner check
173 174 175 176 177 178 179
      paint
        ..color = const Color(0xFFFFFFFF)
        ..style = ui.PaintingStyle.stroke;
      Path path = new Path();
      Point start = new Point(_kEdgeSize * 0.15, _kEdgeSize * 0.45);
      Point mid = new Point(_kEdgeSize * 0.4, _kEdgeSize * 0.7);
      Point end = new Point(_kEdgeSize * 0.85, _kEdgeSize * 0.25);
180 181
      Point lerp(Point p1, Point p2, double t) =>
          new Point(p1.x * (1.0 - t) + p2.x * t, p1.y * (1.0 - t) + p2.y * t);
182 183
      Point drawStart = lerp(start, mid, 1.0 - t);
      Point drawEnd = lerp(mid, end, t);
184 185 186
      path.moveTo(offset.dx + drawStart.x, offset.dy + drawStart.y);
      path.lineTo(offset.dx + mid.x, offset.dy + mid.y);
      path.lineTo(offset.dx + drawEnd.x, offset.dy + drawEnd.y);
187 188 189 190
      canvas.drawPath(path, paint);
    }
  }
}