tooltip.dart 8.93 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4 5 6 7
// 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.

import 'dart:async';
import 'dart:math' as math;

8
import 'package:flutter/gestures.dart';
Hixie's avatar
Hixie committed
9 10
import 'package:flutter/widgets.dart';

11 12
import 'theme.dart';
import 'theme_data.dart';
13 14 15

const double _kScreenEdgeMargin = 10.0;
const Duration _kFadeDuration = const Duration(milliseconds: 200);
16
const Duration _kShowDuration = const Duration(milliseconds: 1500);
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/// A material design tooltip.
///
/// Tooltips provide text labels that help explain the function of a button or
/// other user interface action. Wrap the button in a [Tooltip] widget to
/// show a label when the widget long pressed (or when the user takes some
/// other appropriate action).
///
/// Many widgets, such as [IconButton], [FloatingActionButton], and
/// [PopupMenuButton] have a `tooltip` property that, when non-null, causes the
/// widget to include a [Tooltip] in its build.
///
/// Tooltips improve the accessibility of visual widgets by proving a textual
/// representation of the widget, which, for example, can be vocalized by a
/// screen reader.
///
/// See also:
///
35
///  * <https://material.google.com/components/tooltips.html>
36
class Tooltip extends StatefulWidget {
37 38 39 40 41 42
  /// Creates a tooltip.
  ///
  /// By default, tooltips prefer to appear below the [child] widget when the
  /// user long presses on the widget.
  ///
  /// The [message] argument cannot be null.
Hixie's avatar
Hixie committed
43 44 45
  Tooltip({
    Key key,
    this.message,
46 47 48
    this.height: 32.0,
    this.padding: const EdgeInsets.symmetric(horizontal: 16.0),
    this.verticalOffset: 24.0,
Hixie's avatar
Hixie committed
49
    this.preferBelow: true,
50
    this.child,
Hixie's avatar
Hixie committed
51 52 53 54 55 56
  }) : super(key: key) {
    assert(message != null);
    assert(height != null);
    assert(padding != null);
    assert(verticalOffset != null);
    assert(preferBelow != null);
57
    assert(child != null);
Hixie's avatar
Hixie committed
58 59
  }

60
  /// The text to display in the tooltip.
Hixie's avatar
Hixie committed
61
  final String message;
62

63
  /// The amount of vertical space the tooltip should occupy (inside its padding).
Hixie's avatar
Hixie committed
64
  final double height;
65

66 67 68
  /// The amount of space by which to inset the child.
  ///
  /// Defaults to 16.0 logical pixels in each direction.
69
  final EdgeInsets padding;
70

71
  /// The amount of vertical distance between the widget and the displayed tooltip.
Hixie's avatar
Hixie committed
72
  final double verticalOffset;
73

74 75 76 77 78
  /// Whether the tooltip defaults to being displayed below the widget.
  ///
  /// Defaults to true. If there is insufficient space to display the tooltip in
  /// the preferred direction, the tooltip will be displayed in the opposite
  /// direction.
Hixie's avatar
Hixie committed
79
  final bool preferBelow;
80 81

  /// The widget below this widget in the tree.
Hixie's avatar
Hixie committed
82 83
  final Widget child;

84
  @override
Hixie's avatar
Hixie committed
85
  _TooltipState createState() => new _TooltipState();
Hixie's avatar
Hixie committed
86

87
  @override
Hixie's avatar
Hixie committed
88 89 90 91 92 93
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('"$message"');
    description.add('vertical offset: $verticalOffset');
    description.add('position: ${preferBelow ? "below" : "above"}');
  }
Hixie's avatar
Hixie committed
94 95
}

96
class _TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
97
  AnimationController _controller;
Hixie's avatar
Hixie committed
98 99 100
  OverlayEntry _entry;
  Timer _timer;

101
  @override
Hixie's avatar
Hixie committed
102 103
  void initState() {
    super.initState();
104
    _controller = new AnimationController(duration: _kFadeDuration, vsync: this)
105 106 107 108
      ..addStatusListener(_handleStatusChanged);
  }

  void _handleStatusChanged(AnimationStatus status) {
109 110
    if (status == AnimationStatus.dismissed)
      _removeEntry();
Hixie's avatar
Hixie committed
111 112
  }

113
  void ensureTooltipVisible() {
114 115 116 117
    if (_entry != null) {
      _timer?.cancel();
      _timer = null;
      _controller.forward();
118
      return;  // Already visible.
119
    }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    final RenderBox box = context.findRenderObject();
    final Point target = box.localToGlobal(box.size.center(Point.origin));
    // We create this widget outside of the overlay entry's builder to prevent
    // updated values from happening to leak into the overlay when the overlay
    // rebuilds.
    final Widget overlay = new _TooltipOverlay(
      message: config.message,
      height: config.height,
      padding: config.padding,
      animation: new CurvedAnimation(
        parent: _controller,
        curve: Curves.fastOutSlowIn
      ),
      target: target,
      verticalOffset: config.verticalOffset,
      preferBelow: config.preferBelow
    );
    _entry = new OverlayEntry(builder: (BuildContext context) => overlay);
138 139 140
    Overlay.of(context, debugRequiredFor: config).insert(_entry);
    GestureBinding.instance.pointerRouter.addGlobalRoute(_handlePointerEvent);
    _controller.forward();
Hixie's avatar
Hixie committed
141 142
  }

143 144
  void _removeEntry() {
    assert(_entry != null);
Hixie's avatar
Hixie committed
145
    _timer?.cancel();
146 147 148 149
    _timer = null;
    _entry.remove();
    _entry = null;
    GestureBinding.instance.pointerRouter.removeGlobalRoute(_handlePointerEvent);
Hixie's avatar
Hixie committed
150 151
  }

152
  void _handlePointerEvent(PointerEvent event) {
Hixie's avatar
Hixie committed
153
    assert(_entry != null);
154
    if (event is PointerUpEvent || event is PointerCancelEvent)
155
      _timer ??= new Timer(_kShowDuration, _controller.reverse);
156 157
    else if (event is PointerDownEvent)
      _controller.reverse();
Hixie's avatar
Hixie committed
158 159
  }

160
  @override
Hixie's avatar
Hixie committed
161 162
  void deactivate() {
    if (_entry != null)
163
      _controller.reverse();
Hixie's avatar
Hixie committed
164 165 166
    super.deactivate();
  }

167 168
  @override
  void dispose() {
169 170
    if (_entry != null)
      _removeEntry();
171
    _controller.dispose();
172 173 174
    super.dispose();
  }

175
  @override
Hixie's avatar
Hixie committed
176
  Widget build(BuildContext context) {
177
    assert(Overlay.of(context, debugRequiredFor: config) != null);
Hixie's avatar
Hixie committed
178 179
    return new GestureDetector(
      behavior: HitTestBehavior.opaque,
180
      onLongPress: ensureTooltipVisible,
Hixie's avatar
Hixie committed
181 182 183
      excludeFromSemantics: true,
      child: new Semantics(
        label: config.message,
184
        child: config.child,
Hixie's avatar
Hixie committed
185
      )
Hixie's avatar
Hixie committed
186 187 188 189
    );
  }
}

190
class _TooltipPositionDelegate extends SingleChildLayoutDelegate {
Hixie's avatar
Hixie committed
191 192 193 194 195
  _TooltipPositionDelegate({
    this.target,
    this.verticalOffset,
    this.preferBelow
  });
196

Hixie's avatar
Hixie committed
197 198 199 200
  final Point target;
  final double verticalOffset;
  final bool preferBelow;

201
  @override
Hixie's avatar
Hixie committed
202 203
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) => constraints.loosen();

204
  @override
Hixie's avatar
Hixie committed
205 206
  Offset getPositionForChild(Size size, Size childSize) {
    // VERTICAL DIRECTION
207 208
    final bool fitsBelow = target.y + verticalOffset + childSize.height <= size.height - _kScreenEdgeMargin;
    final bool fitsAbove = target.y - verticalOffset - childSize.height >= _kScreenEdgeMargin;
Hixie's avatar
Hixie committed
209 210 211
    final bool tooltipBelow = preferBelow ? fitsBelow || !fitsAbove : !(fitsAbove || !fitsBelow);
    double y;
    if (tooltipBelow)
212
      y = math.min(target.y + verticalOffset, size.height - _kScreenEdgeMargin);
Hixie's avatar
Hixie committed
213
    else
214
      y = math.max(target.y - verticalOffset - childSize.height, _kScreenEdgeMargin);
Hixie's avatar
Hixie committed
215
    // HORIZONTAL DIRECTION
216
    final double normalizedTargetX = target.x.clamp(_kScreenEdgeMargin, size.width - _kScreenEdgeMargin);
Hixie's avatar
Hixie committed
217
    double x;
218 219 220 221
    if (normalizedTargetX < _kScreenEdgeMargin + childSize.width / 2.0) {
      x = _kScreenEdgeMargin;
    } else if (normalizedTargetX > size.width - _kScreenEdgeMargin - childSize.width / 2.0) {
      x = size.width - _kScreenEdgeMargin - childSize.width;
Hixie's avatar
Hixie committed
222
    } else {
223
      x = normalizedTargetX - childSize.width / 2.0;
Hixie's avatar
Hixie committed
224 225 226 227
    }
    return new Offset(x, y);
  }

228
  @override
Hixie's avatar
Hixie committed
229
  bool shouldRelayout(_TooltipPositionDelegate oldDelegate) {
230 231 232
    return target != oldDelegate.target
        || verticalOffset != oldDelegate.verticalOffset
        || preferBelow != oldDelegate.preferBelow;
Hixie's avatar
Hixie committed
233 234 235
  }
}

236
class _TooltipOverlay extends StatelessWidget {
Hixie's avatar
Hixie committed
237 238 239 240 241
  _TooltipOverlay({
    Key key,
    this.message,
    this.height,
    this.padding,
242
    this.animation,
Hixie's avatar
Hixie committed
243 244 245 246 247 248 249
    this.target,
    this.verticalOffset,
    this.preferBelow
  }) : super(key: key);

  final String message;
  final double height;
250
  final EdgeInsets padding;
251
  final Animation<double> animation;
Hixie's avatar
Hixie committed
252 253 254 255
  final Point target;
  final double verticalOffset;
  final bool preferBelow;

256
  @override
Hixie's avatar
Hixie committed
257
  Widget build(BuildContext context) {
258 259
    final ThemeData theme = Theme.of(context);
    final ThemeData darkTheme = new ThemeData(
260
      brightness: Brightness.dark,
261 262
      textTheme: theme.brightness == Brightness.dark ? theme.textTheme : theme.primaryTextTheme,
      platform: theme.platform,
263
    );
264
    return new Positioned.fill(
Hixie's avatar
Hixie committed
265
      child: new IgnorePointer(
266
        child: new CustomSingleChildLayout(
Hixie's avatar
Hixie committed
267 268 269 270 271
          delegate: new _TooltipPositionDelegate(
            target: target,
            verticalOffset: verticalOffset,
            preferBelow: preferBelow
          ),
272 273
          child: new FadeTransition(
            opacity: animation,
Hixie's avatar
Hixie committed
274
            child: new Opacity(
275
              opacity: 0.9,
Hixie's avatar
Hixie committed
276 277
              child: new Container(
                decoration: new BoxDecoration(
278
                  backgroundColor: darkTheme.backgroundColor,
279
                  borderRadius: new BorderRadius.circular(2.0)
Hixie's avatar
Hixie committed
280 281 282 283 284
                ),
                height: height,
                padding: padding,
                child: new Center(
                  widthFactor: 1.0,
285
                  child: new Text(message, style: darkTheme.textTheme.body1)
Hixie's avatar
Hixie committed
286 287 288 289 290 291 292 293 294
                )
              )
            )
          )
        )
      )
    );
  }
}