ink_splash.dart 7.92 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2017 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.

import 'dart:math' as math;

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

10
import 'ink_well.dart';
11 12
import 'material.dart';

13 14
const Duration _kUnconfirmedSplashDuration = Duration(seconds: 1);
const Duration _kSplashFadeDuration = Duration(milliseconds: 200);
15 16 17 18 19 20 21 22 23 24

const double _kSplashInitialSize = 0.0; // logical pixels
const double _kSplashConfirmedVelocity = 1.0; // logical pixels per millisecond

RectCallback _getClipCallback(RenderBox referenceBox, bool containedInkWell, RectCallback rectCallback) {
  if (rectCallback != null) {
    assert(containedInkWell);
    return rectCallback;
  }
  if (containedInkWell)
25
    return () => Offset.zero & referenceBox.size;
26 27 28
  return null;
}

29
double _getTargetRadius(RenderBox referenceBox, bool containedInkWell, RectCallback rectCallback, Offset position) {
30 31
  if (containedInkWell) {
    final Size size = rectCallback != null ? rectCallback().size : referenceBox.size;
32
    return _getSplashRadiusForPositionInSize(size, position);
33 34 35 36
  }
  return Material.defaultSplashRadius;
}

37
double _getSplashRadiusForPositionInSize(Size bounds, Offset position) {
38 39 40 41
  final double d1 = (position - bounds.topLeft(Offset.zero)).distance;
  final double d2 = (position - bounds.topRight(Offset.zero)).distance;
  final double d3 = (position - bounds.bottomLeft(Offset.zero)).distance;
  final double d4 = (position - bounds.bottomRight(Offset.zero)).distance;
42 43 44
  return math.max(math.max(d1, d2), math.max(d3, d4)).ceilToDouble();
}

45 46 47 48 49 50 51 52 53
class _InkSplashFactory extends InteractiveInkFeatureFactory {
  const _InkSplashFactory();

  @override
  InteractiveInkFeature create({
    @required MaterialInkController controller,
    @required RenderBox referenceBox,
    @required Offset position,
    @required Color color,
54
    @required TextDirection textDirection,
55
    bool containedInkWell = false,
56 57
    RectCallback rectCallback,
    BorderRadius borderRadius,
58
    ShapeBorder customBorder,
59 60 61
    double radius,
    VoidCallback onRemoved,
  }) {
62
    return InkSplash(
63 64 65 66 67 68 69
      controller: controller,
      referenceBox: referenceBox,
      position: position,
      color: color,
      containedInkWell: containedInkWell,
      rectCallback: rectCallback,
      borderRadius: borderRadius,
70
      customBorder: customBorder,
71 72
      radius: radius,
      onRemoved: onRemoved,
73
      textDirection: textDirection,
74 75 76 77
    );
  }
}

78 79
/// A visual reaction on a piece of [Material] to user input.
///
80 81 82
/// A circular ink feature whose origin starts at the input touch point
/// and whose radius expands from zero.
///
83 84 85 86 87 88
/// This object is rarely created directly. Instead of creating an ink splash
/// directly, consider using an [InkResponse] or [InkWell] widget, which uses
/// gestures (such as tap and long-press) to trigger ink splashes.
///
/// See also:
///
89 90
///  * [InkRipple], which is an ink splash feature that expands more
///    aggressively than this class does.
91 92 93 94 95 96 97
///  * [InkResponse], which uses gestures to trigger ink highlights and ink
///    splashes in the parent [Material].
///  * [InkWell], which is a rectangular [InkResponse] (the most common type of
///    ink response).
///  * [Material], which is the widget on which the ink splash is painted.
///  * [InkHighlight], which is an ink feature that emphasizes a part of a
///    [Material].
98
class InkSplash extends InteractiveInkFeature {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  /// Begin a splash, centered at position relative to [referenceBox].
  ///
  /// The [controller] argument is typically obtained via
  /// `Material.of(context)`.
  ///
  /// If `containedInkWell` is true, then the splash will be sized to fit
  /// the well rectangle, then clipped to it when drawn. The well
  /// rectangle is the box returned by `rectCallback`, if provided, or
  /// otherwise is the bounds of the [referenceBox].
  ///
  /// If `containedInkWell` is false, then `rectCallback` should be null.
  /// The ink splash is clipped only to the edges of the [Material].
  /// This is the default.
  ///
  /// When the splash is removed, `onRemoved` will be called.
  InkSplash({
    @required MaterialInkController controller,
    @required RenderBox referenceBox,
117
    @required TextDirection textDirection,
118
    Offset position,
119
    Color color,
120
    bool containedInkWell = false,
121
    RectCallback rectCallback,
Ian Hickson's avatar
Ian Hickson committed
122
    BorderRadius borderRadius,
123
    ShapeBorder customBorder,
124 125
    double radius,
    VoidCallback onRemoved,
126 127
  }) : assert(textDirection != null),
       _position = position,
Ian Hickson's avatar
Ian Hickson committed
128
       _borderRadius = borderRadius ?? BorderRadius.zero,
129
       _customBorder = customBorder,
130 131 132
       _targetRadius = radius ?? _getTargetRadius(referenceBox, containedInkWell, rectCallback, position),
       _clipCallback = _getClipCallback(referenceBox, containedInkWell, rectCallback),
       _repositionToReferenceBox = !containedInkWell,
133
       _textDirection = textDirection,
134
       super(controller: controller, referenceBox: referenceBox, color: color, onRemoved: onRemoved) {
135
    assert(_borderRadius != null);
136
    _radiusController = AnimationController(duration: _kUnconfirmedSplashDuration, vsync: controller.vsync)
137 138
      ..addListener(controller.markNeedsPaint)
      ..forward();
139
    _radius = _radiusController.drive(Tween<double>(
140
      begin: _kSplashInitialSize,
141 142
      end: _targetRadius,
    ));
143
    _alphaController = AnimationController(duration: _kSplashFadeDuration, vsync: controller.vsync)
144 145
      ..addListener(controller.markNeedsPaint)
      ..addStatusListener(_handleAlphaStatusChanged);
146
    _alpha = _alphaController.drive(IntTween(
147
      begin: color.alpha,
148 149
      end: 0,
    ));
150 151 152 153

    controller.addInkFeature(this);
  }

154
  final Offset _position;
155
  final BorderRadius _borderRadius;
156
  final ShapeBorder _customBorder;
157 158 159
  final double _targetRadius;
  final RectCallback _clipCallback;
  final bool _repositionToReferenceBox;
160
  final TextDirection _textDirection;
161 162 163 164 165 166 167

  Animation<double> _radius;
  AnimationController _radiusController;

  Animation<int> _alpha;
  AnimationController _alphaController;

168 169 170 171
  /// Used to specify this type of ink splash for an [InkWell], [InkResponse]
  /// or material [Theme].
  static const InteractiveInkFeatureFactory splashFactory = _InkSplashFactory();

172
  @override
173 174 175
  void confirm() {
    final int duration = (_targetRadius / _kSplashConfirmedVelocity).floor();
    _radiusController
176
      ..duration = Duration(milliseconds: duration)
177 178 179 180
      ..forward();
    _alphaController.forward();
  }

181
  @override
182
  void cancel() {
183
    _alphaController?.forward();
184 185 186 187 188 189 190 191 192 193 194
  }

  void _handleAlphaStatusChanged(AnimationStatus status) {
    if (status == AnimationStatus.completed)
      dispose();
  }

  @override
  void dispose() {
    _radiusController.dispose();
    _alphaController.dispose();
195
    _alphaController = null;
196 197 198 199 200
    super.dispose();
  }

  @override
  void paintFeature(Canvas canvas, Matrix4 transform) {
201
    final Paint paint = Paint()..color = color.withAlpha(_alpha.value);
202
    Offset center = _position;
203
    if (_repositionToReferenceBox)
204
      center = Offset.lerp(center, referenceBox.size.center(Offset.zero), _radiusController.value);
205
    final Offset originOffset = MatrixUtils.getAsTranslation(transform);
206
    canvas.save();
207 208 209
    if (originOffset == null) {
      canvas.transform(transform.storage);
    } else {
210 211 212 213 214
      canvas.translate(originOffset.dx, originOffset.dy);
    }
    if (_clipCallback != null) {
      final Rect rect = _clipCallback();
      if (_customBorder != null) {
215
        canvas.clipPath(_customBorder.getOuterPath(rect, textDirection: _textDirection));
216
      } else if (_borderRadius != BorderRadius.zero) {
217
        canvas.clipRRect(RRect.fromRectAndCorners(
218 219 220 221 222 223
          rect,
          topLeft: _borderRadius.topLeft, topRight: _borderRadius.topRight,
          bottomLeft: _borderRadius.bottomLeft, bottomRight: _borderRadius.bottomRight,
        ));
      } else {
        canvas.clipRect(rect);
224 225
      }
    }
226 227
    canvas.drawCircle(center, _radius.value, paint);
    canvas.restore();
228 229
  }
}