ink_well.dart 5 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 'dart:collection';

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

11
import 'debug.dart';
12 13
import 'material.dart';
import 'theme.dart';
14

15 16
class InkResponse extends StatefulComponent {
  InkResponse({
17 18 19
    Key key,
    this.child,
    this.onTap,
20
    this.onDoubleTap,
21
    this.onLongPress,
22 23
    this.onHighlightChanged,
    this.containedInWell: false,
24
    this.highlightShape: BoxShape.circle
25 26 27 28
  }) : super(key: key);

  final Widget child;
  final GestureTapCallback onTap;
29
  final GestureTapCallback onDoubleTap;
30
  final GestureLongPressCallback onLongPress;
31
  final ValueChanged<bool> onHighlightChanged;
32
  final bool containedInWell;
33
  final BoxShape highlightShape;
34

35
  _InkResponseState createState() => new _InkResponseState<InkResponse>();
36 37
}

38
class _InkResponseState<T extends InkResponse> extends State<T> {
39

40 41
  Set<InkSplash> _splashes;
  InkSplash _currentSplash;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  InkHighlight _lastHighlight;

  void updateHighlight(bool value) {
    if (value == (_lastHighlight != null && _lastHighlight.active))
      return;
    if (value) {
      if (_lastHighlight == null) {
        RenderBox referenceBox = context.findRenderObject();
        assert(Material.of(context) != null);
        _lastHighlight = Material.of(context).highlightAt(
          referenceBox: referenceBox,
          color: Theme.of(context).highlightColor,
          shape: config.highlightShape,
          onRemoved: () {
            assert(_lastHighlight != null);
            _lastHighlight = null;
          }
        );
      } else {
        _lastHighlight.activate();
      }
    } else {
      _lastHighlight.deactivate();
    }
66
    assert(value == (_lastHighlight != null && _lastHighlight.active));
67
    if (config.onHighlightChanged != null)
68
      config.onHighlightChanged(value);
69 70
  }

71

72 73 74 75 76 77 78
  void _handleTapDown(Point position) {
    RenderBox referenceBox = context.findRenderObject();
    assert(Material.of(context) != null);
    InkSplash splash;
    splash = Material.of(context).splashAt(
      referenceBox: referenceBox,
      position: referenceBox.globalToLocal(position),
79 80
      color: Theme.of(context).splashColor,
      containedInWell: config.containedInWell,
81 82 83 84 85 86 87 88 89
      onRemoved: () {
        if (_splashes != null) {
          assert(_splashes.contains(splash));
          _splashes.remove(splash);
          if (_currentSplash == splash)
            _currentSplash = null;
        } // else we're probably in deactivate()
      }
    );
90
    _splashes ??= new HashSet<InkSplash>();
91 92
    _splashes.add(splash);
    _currentSplash = splash;
93
    updateHighlight(true);
94 95
  }

96 97 98
  void _handleTap() {
    _currentSplash?.confirm();
    _currentSplash = null;
99
    updateHighlight(false);
100 101
    if (config.onTap != null)
      config.onTap();
102 103
  }

104 105 106
  void _handleTapCancel() {
    _currentSplash?.cancel();
    _currentSplash = null;
107
    updateHighlight(false);
108 109
  }

110 111 112 113 114
  void _handleDoubleTap() {
    _currentSplash?.confirm();
    _currentSplash = null;
    if (config.onDoubleTap != null)
      config.onDoubleTap();
115 116
  }

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  void _handleLongPress() {
    _currentSplash?.confirm();
    _currentSplash = null;
    if (config.onLongPress != null)
      config.onLongPress();
  }

  void deactivate() {
    if (_splashes != null) {
      Set<InkSplash> splashes = _splashes;
      _splashes = null;
      for (InkSplash splash in splashes)
        splash.dispose();
      _currentSplash = null;
    }
    assert(_currentSplash == null);
133 134
    _lastHighlight?.dispose();
    _lastHighlight = null;
135
    super.deactivate();
136 137
  }

138 139 140 141 142
  void dependenciesChanged(Type affectedWidgetType) {
    if (affectedWidgetType == Theme && _lastHighlight != null)
      _lastHighlight.color = Theme.of(context).highlightColor;
  }

143
  Widget build(BuildContext context) {
144
    assert(debugCheckHasMaterial(context));
145 146 147 148 149 150 151 152 153 154
    final bool enabled = config.onTap != null || config.onDoubleTap != null || config.onLongPress != null;
    return new GestureDetector(
      onTapDown: enabled ? _handleTapDown : null,
      onTap: enabled ? _handleTap : null,
      onTapCancel: enabled ? _handleTapCancel : null,
      onDoubleTap: config.onDoubleTap != null ? _handleDoubleTap : null,
      onLongPress: config.onLongPress != null ? _handleLongPress : null,
      behavior: HitTestBehavior.opaque,
      child: config.child
    );
155 156
  }

157
}
158

159 160 161 162 163 164 165
/// An area of a Material that responds to touch.
///
/// Must have an ancestor Material widget in which to cause ink reactions.
class InkWell extends InkResponse {
  InkWell({
    Key key,
    Widget child,
166
    GestureTapCallback onTap,
167
    GestureTapCallback onDoubleTap,
168
    GestureLongPressCallback onLongPress,
169
    ValueChanged<bool> onHighlightChanged
170 171 172 173 174
  }) : super(
    key: key,
    child: child,
    onTap: onTap,
    onDoubleTap: onDoubleTap,
175 176 177
    onLongPress: onLongPress,
    onHighlightChanged: onHighlightChanged,
    containedInWell: true,
178
    highlightShape: BoxShape.rectangle
179
  );
180
}