toggleable.dart 6.39 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 6
import 'package:flutter/animation.dart';
import 'package:flutter/gestures.dart';
7
import 'package:flutter/rendering.dart';
8

9 10
import 'constants.dart';

11
const Duration _kToggleDuration = const Duration(milliseconds: 200);
Adam Barth's avatar
Adam Barth committed
12
final Tween<double> _kRadialReactionRadiusTween = new Tween<double>(begin: 0.0, end: kRadialReactionRadius);
13 14 15 16 17

// RenderToggleable is a base class for material style toggleable controls with
// toggle animations. It handles storing the current value, dispatching
// ValueChanged on a tap gesture and driving a changed animation. Subclasses are
// responsible for painting.
Hixie's avatar
Hixie committed
18
abstract class RenderToggleable extends RenderConstrainedBox implements SemanticActionHandler {
19 20 21
  RenderToggleable({
    bool value,
    Size size,
22 23
    Color activeColor,
    Color inactiveColor,
Adam Barth's avatar
Adam Barth committed
24
    ValueChanged<bool> onChanged
25
  }) : _value = value,
26 27
       _activeColor = activeColor,
       _inactiveColor = inactiveColor,
Hixie's avatar
Hixie committed
28
       _onChanged = onChanged,
29
       super(additionalConstraints: new BoxConstraints.tight(size)) {
30 31 32
    assert(value != null);
    assert(activeColor != null);
    assert(inactiveColor != null);
33
    _tap = new TapGestureRecognizer()
34 35 36 37
      ..onTapDown = _handleTapDown
      ..onTap = _handleTap
      ..onTapUp = _handleTapUp
      ..onTapCancel = _handleTapCancel;
38 39

    _positionController = new AnimationController(
Hixie's avatar
Hixie committed
40
      duration: _kToggleDuration,
41 42 43 44
      value: _value ? 1.0 : 0.0
    );
    _position = new CurvedAnimation(
      parent: _positionController
45 46
    )..addListener(markNeedsPaint)
     ..addStatusListener(_handlePositionStateChanged);
47 48

    _reactionController = new AnimationController(duration: kRadialReactionDuration);
Adam Barth's avatar
Adam Barth committed
49
    _reaction = new CurvedAnimation(
50 51
      parent: _reactionController,
      curve: Curves.ease
Adam Barth's avatar
Adam Barth committed
52
    )..addListener(markNeedsPaint);
53 54
  }

55 56 57
  bool get value => _value;
  bool _value;
  void set value(bool value) {
58
    assert(value != null);
59 60 61
    if (value == _value)
      return;
    _value = value;
Hixie's avatar
Hixie committed
62
    markNeedsSemanticsUpdate(onlyChanges: true, noGeometry: true);
63
    _position
64 65
      ..curve = Curves.easeIn
      ..reverseCurve = Curves.easeOut;
Adam Barth's avatar
Adam Barth committed
66 67 68 69
    if (value)
      _positionController.forward();
    else
      _positionController.reverse();
70 71
  }

72 73 74 75 76
  Color get activeColor => _activeColor;
  Color _activeColor;
  void set activeColor(Color value) {
    assert(value != null);
    if (value == _activeColor)
77
      return;
78 79 80 81 82 83 84 85 86 87 88
    _activeColor = value;
    markNeedsPaint();
  }

  Color get inactiveColor => _inactiveColor;
  Color _inactiveColor;
  void set inactiveColor(Color value) {
    assert(value != null);
    if (value == _inactiveColor)
      return;
    _inactiveColor = value;
89 90 91
    markNeedsPaint();
  }

Hixie's avatar
Hixie committed
92 93 94 95 96 97 98 99 100 101 102 103
  ValueChanged<bool> get onChanged => _onChanged;
  ValueChanged<bool> _onChanged;
  void set onChanged(ValueChanged<bool> value) {
    if (value == _onChanged)
      return;
    final bool wasInteractive = isInteractive;
    _onChanged = value;
    if (wasInteractive != isInteractive) {
      markNeedsPaint();
      markNeedsSemanticsUpdate(noGeometry: true);
    }
  }
104

Hixie's avatar
Hixie committed
105
  bool get isInteractive => onChanged != null;
106

107 108 109 110 111
  CurvedAnimation get position => _position;
  CurvedAnimation _position;

  AnimationController get positionController => _positionController;
  AnimationController _positionController;
Hixie's avatar
Hixie committed
112

113 114
  AnimationController get reactionController => _reactionController;
  AnimationController _reactionController;
115
  Animation<double> _reaction;
Hixie's avatar
Hixie committed
116

117
  TapGestureRecognizer _tap;
Adam Barth's avatar
Adam Barth committed
118
  Point _downPosition;
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  @override
  void attach(PipelineOwner owner) {
    super.attach(owner);
    if (_positionController != null) {
      if (value)
        _positionController.forward();
      else
        _positionController.reverse();
    }
    if (_reactionController != null && isInteractive) {
      switch (_reactionController.status) {
        case AnimationStatus.forward:
          _reactionController.forward();
          break;
        case AnimationStatus.reverse:
          _reactionController.reverse();
          break;
        case AnimationStatus.dismissed:
        case AnimationStatus.completed:
          // nothing to do
          break;
      }
    }
  }

  @override
  void detach() {
    _positionController?.stop();
    _reactionController?.stop();
    super.detach();
  }

152
  void _handlePositionStateChanged(AnimationStatus status) {
153
    if (isInteractive) {
154
      if (status == AnimationStatus.completed && !_value)
155
        onChanged(true);
156
      else if (status == AnimationStatus.dismissed && _value)
157 158
        onChanged(false);
    }
159 160
  }

161
  void _handleTapDown(Point globalPosition) {
Adam Barth's avatar
Adam Barth committed
162 163
    if (isInteractive) {
      _downPosition = globalToLocal(globalPosition);
164
      _reactionController.forward();
Adam Barth's avatar
Adam Barth committed
165
    }
166 167
  }

168
  void _handleTap() {
169
    if (isInteractive)
170
      onChanged(!_value);
171
  }
Adam Barth's avatar
Adam Barth committed
172

173
  void _handleTapUp(Point globalPosition) {
Adam Barth's avatar
Adam Barth committed
174
    _downPosition = null;
175
    if (isInteractive)
176
      _reactionController.reverse();
177 178 179
  }

  void _handleTapCancel() {
Adam Barth's avatar
Adam Barth committed
180
    _downPosition = null;
181
    if (isInteractive)
182
      _reactionController.reverse();
183 184
  }

185
  @override
Adam Barth's avatar
Adam Barth committed
186
  bool hitTestSelf(Point position) => true;
187

188
  @override
Ian Hickson's avatar
Ian Hickson committed
189 190
  void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
    if (event is PointerDownEvent && isInteractive)
191 192 193
      _tap.addPointer(event);
  }

Adam Barth's avatar
Adam Barth committed
194
  void paintRadialReaction(Canvas canvas, Offset offset, Point origin) {
195
    if (!_reaction.isDismissed) {
196 197
      // TODO(abarth): We should have a different reaction color when position is zero.
      Paint reactionPaint = new Paint()..color = activeColor.withAlpha(kRadialReactionAlpha);
Adam Barth's avatar
Adam Barth committed
198 199 200
      Point center = Point.lerp(_downPosition ?? origin, origin, _reaction.value);
      double radius = _kRadialReactionRadiusTween.evaluate(_reaction);
      canvas.drawCircle(center + offset, radius, reactionPaint);
201 202
    }
  }
Hixie's avatar
Hixie committed
203

204
  @override
Hixie's avatar
Hixie committed
205
  bool get hasSemantics => isInteractive;
206 207

  @override
Hixie's avatar
Hixie committed
208 209 210 211 212 213 214
  Iterable<SemanticAnnotator> getSemanticAnnotators() sync* {
    yield (SemanticsNode semantics) {
      semantics.hasCheckedState = true;
      semantics.isChecked = _value;
      semantics.canBeTapped = isInteractive;
    };
  }
215 216

  @override
Hixie's avatar
Hixie committed
217 218
  void handleSemanticTap() => _handleTap();

219
  @override
Hixie's avatar
Hixie committed
220
  void handleSemanticLongPress() { }
221 222

  @override
Hixie's avatar
Hixie committed
223
  void handleSemanticScrollLeft() { }
224 225

  @override
Hixie's avatar
Hixie committed
226
  void handleSemanticScrollRight() { }
227 228

  @override
Hixie's avatar
Hixie committed
229
  void handleSemanticScrollUp() { }
230 231

  @override
Hixie's avatar
Hixie committed
232
  void handleSemanticScrollDown() { }
233
}