ink_splash.dart 7.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// 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/widgets.dart';

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

12 13
const Duration _kUnconfirmedSplashDuration = Duration(seconds: 1);
const Duration _kSplashFadeDuration = Duration(milliseconds: 200);
14 15 16 17

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

18
RectCallback? _getClipCallback(RenderBox referenceBox, bool containedInkWell, RectCallback? rectCallback) {
19 20 21 22 23
  if (rectCallback != null) {
    assert(containedInkWell);
    return rectCallback;
  }
  if (containedInkWell)
24
    return () => Offset.zero & referenceBox.size;
25 26 27
  return null;
}

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

36
double _getSplashRadiusForPositionInSize(Size bounds, Offset position) {
37 38 39 40
  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;
41 42 43
  return math.max(math.max(d1, d2), math.max(d3, d4)).ceilToDouble();
}

44 45 46 47 48
class _InkSplashFactory extends InteractiveInkFeatureFactory {
  const _InkSplashFactory();

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

77 78
/// A visual reaction on a piece of [Material] to user input.
///
79 80 81
/// A circular ink feature whose origin starts at the input touch point
/// and whose radius expands from zero.
///
82 83 84 85 86 87
/// 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:
///
88 89
///  * [InkRipple], which is an ink splash feature that expands more
///    aggressively than this class does.
90 91 92 93 94 95 96
///  * [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].
97 98
///  * [Ink], a convenience widget for drawing images and other decorations on
///    Material widgets.
99
class InkSplash extends InteractiveInkFeature {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  /// 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({
116
    required MaterialInkController controller,
117
    required super.referenceBox,
118 119 120
    required TextDirection textDirection,
    Offset? position,
    required Color color,
121
    bool containedInkWell = false,
122 123 124 125
    RectCallback? rectCallback,
    BorderRadius? borderRadius,
    ShapeBorder? customBorder,
    double? radius,
126
    super.onRemoved,
127 128
  }) : assert(textDirection != null),
       _position = position,
Ian Hickson's avatar
Ian Hickson committed
129
       _borderRadius = borderRadius ?? BorderRadius.zero,
130
       _customBorder = customBorder,
131
       _targetRadius = radius ?? _getTargetRadius(referenceBox, containedInkWell, rectCallback, position!),
132 133
       _clipCallback = _getClipCallback(referenceBox, containedInkWell, rectCallback),
       _repositionToReferenceBox = !containedInkWell,
134
       _textDirection = textDirection,
135
       super(controller: controller, color: color) {
136
    assert(_borderRadius != null);
137
    _radiusController = AnimationController(duration: _kUnconfirmedSplashDuration, vsync: controller.vsync)
138 139
      ..addListener(controller.markNeedsPaint)
      ..forward();
140
    _radius = _radiusController.drive(Tween<double>(
141
      begin: _kSplashInitialSize,
142 143
      end: _targetRadius,
    ));
144
    _alphaController = AnimationController(duration: _kSplashFadeDuration, vsync: controller.vsync)
145 146
      ..addListener(controller.markNeedsPaint)
      ..addStatusListener(_handleAlphaStatusChanged);
147
    _alpha = _alphaController!.drive(IntTween(
148
      begin: color.alpha,
149 150
      end: 0,
    ));
151 152 153 154

    controller.addInkFeature(this);
  }

155
  final Offset? _position;
156
  final BorderRadius _borderRadius;
157
  final ShapeBorder? _customBorder;
158
  final double _targetRadius;
159
  final RectCallback? _clipCallback;
160
  final bool _repositionToReferenceBox;
161
  final TextDirection _textDirection;
162

163 164
  late Animation<double> _radius;
  late AnimationController _radiusController;
165

166 167
  late Animation<int> _alpha;
  AnimationController? _alphaController;
168

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

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

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

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

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

  @override
  void paintFeature(Canvas canvas, Matrix4 transform) {
202
    final Paint paint = Paint()..color = color.withAlpha(_alpha.value);
203
    Offset? center = _position;
204
    if (_repositionToReferenceBox)
205
      center = Offset.lerp(center, referenceBox.size.center(Offset.zero), _radiusController.value);
206 207 208 209
    paintInkCircle(
      canvas: canvas,
      transform: transform,
      paint: paint,
210
      center: center!,
211 212 213 214 215 216
      textDirection: _textDirection,
      radius: _radius.value,
      customBorder: _customBorder,
      borderRadius: _borderRadius,
      clipCallback: _clipCallback,
    );
217 218
  }
}