ink_highlight.dart 4.68 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

7
import 'ink_well.dart' show InteractiveInkFeature;
8 9
import 'material.dart';

10
const Duration _kDefaultHighlightFadeDuration = Duration(milliseconds: 200);
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

/// A visual emphasis on a part of a [Material] receiving user interaction.
///
/// This object is rarely created directly. Instead of creating an ink highlight
/// directly, consider using an [InkResponse] or [InkWell] widget, which uses
/// gestures (such as tap and long-press) to trigger ink highlights.
///
/// See also:
///
///  * [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 highlight is painted.
///  * [InkSplash], which is an ink feature that shows a reaction to user input
///    on a [Material].
27 28
///  * [Ink], a convenience widget for drawing images and other decorations on
///    Material widgets.
29
class InkHighlight extends InteractiveInkFeature {
30 31 32 33 34 35 36 37 38 39
  /// Begin a highlight animation.
  ///
  /// The [controller] argument is typically obtained via
  /// `Material.of(context)`.
  ///
  /// If a `rectCallback` is given, then it provides the highlight rectangle,
  /// otherwise, the highlight rectangle is coincident with the [referenceBox].
  ///
  /// When the highlight is removed, `onRemoved` will be called.
  InkHighlight({
40
    required super.controller,
41
    required super.referenceBox,
42
    required super.color,
43
    required TextDirection textDirection,
44
    BoxShape shape = BoxShape.rectangle,
45 46
    double? radius,
    BorderRadius? borderRadius,
47
    super.customBorder,
48
    RectCallback? rectCallback,
49
    super.onRemoved,
50
    Duration fadeDuration = _kDefaultHighlightFadeDuration,
51
  }) : _shape = shape,
52
       _radius = radius,
53
       _borderRadius = borderRadius ?? BorderRadius.zero,
54

55
       _textDirection = textDirection,
56
       _rectCallback = rectCallback {
57
    _alphaController = AnimationController(duration: fadeDuration, vsync: controller.vsync)
58 59 60
      ..addListener(controller.markNeedsPaint)
      ..addStatusListener(_handleAlphaStatusChanged)
      ..forward();
61
    _alpha = _alphaController.drive(IntTween(
62
      begin: 0,
63 64
      end: color.alpha,
    ));
65 66 67 68 69

    controller.addInkFeature(this);
  }

  final BoxShape _shape;
70
  final double? _radius;
71
  final BorderRadius _borderRadius;
72
  final RectCallback? _rectCallback;
73
  final TextDirection _textDirection;
74

75 76
  late Animation<int> _alpha;
  late AnimationController _alphaController;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  /// Whether this part of the material is being visually emphasized.
  bool get active => _active;
  bool _active = true;

  /// Start visually emphasizing this part of the material.
  void activate() {
    _active = true;
    _alphaController.forward();
  }

  /// Stop visually emphasizing this part of the material.
  void deactivate() {
    _active = false;
    _alphaController.reverse();
  }

  void _handleAlphaStatusChanged(AnimationStatus status) {
95
    if (status == AnimationStatus.dismissed && !_active) {
96
      dispose();
97
    }
98 99 100 101 102 103 104 105 106
  }

  @override
  void dispose() {
    _alphaController.dispose();
    super.dispose();
  }

  void _paintHighlight(Canvas canvas, Rect rect, Paint paint) {
107
    canvas.save();
108 109
    if (customBorder != null) {
      canvas.clipPath(customBorder!.getOuterPath(rect, textDirection: _textDirection));
110
    }
111 112
    switch (_shape) {
      case BoxShape.circle:
113
        canvas.drawCircle(rect.center, _radius ?? Material.defaultSplashRadius, paint);
114
      case BoxShape.rectangle:
115
        if (_borderRadius != BorderRadius.zero) {
116
          final RRect clipRRect = RRect.fromRectAndCorners(
117 118 119 120 121 122 123 124
            rect,
            topLeft: _borderRadius.topLeft, topRight: _borderRadius.topRight,
            bottomLeft: _borderRadius.bottomLeft, bottomRight: _borderRadius.bottomRight,
          );
          canvas.drawRRect(clipRRect, paint);
        } else {
          canvas.drawRect(rect, paint);
        }
125
    }
126
    canvas.restore();
127 128 129 130
  }

  @override
  void paintFeature(Canvas canvas, Matrix4 transform) {
131
    final Paint paint = Paint()..color = color.withAlpha(_alpha.value);
132
    final Offset? originOffset = MatrixUtils.getAsTranslation(transform);
133
    final Rect rect = _rectCallback != null ? _rectCallback() : Offset.zero & referenceBox.size;
134 135 136 137 138 139 140 141 142 143
    if (originOffset == null) {
      canvas.save();
      canvas.transform(transform.storage);
      _paintHighlight(canvas, rect, paint);
      canvas.restore();
    } else {
      _paintHighlight(canvas, rect.shift(originOffset), paint);
    }
  }
}