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

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/gestures.dart';
7

Adam Barth's avatar
Adam Barth committed
8
import 'basic.dart';
9
import 'container.dart';
10
import 'debug.dart';
Adam Barth's avatar
Adam Barth committed
11
import 'framework.dart';
Hixie's avatar
Hixie committed
12
import 'gesture_detector.dart';
Adam Barth's avatar
Adam Barth committed
13
import 'navigator.dart';
14 15
import 'transitions.dart';

16
/// A widget that prevents the user from interacting with widgets behind itself.
17 18 19 20 21 22 23 24 25 26 27 28 29
///
/// The modal barrier is the scrim that is rendered behind each route, which
/// generally prevents the user from interacting with the route below the
/// current route, and normally partially obscures such routes.
///
/// For example, when a dialog is on the screen, the page below the dialog is
/// usually darkened by the modal barrier.
///
/// See also:
///
///  * [ModalRoute], which indirectly uses this widget.
///  * [AnimatedModalBarrier], which is similar but takes an animated [color]
///    instead of a single color value.
30
class ModalBarrier extends StatelessWidget {
31
  /// Creates a widget that blocks user interaction.
32
  const ModalBarrier({
33
    Key key,
Hixie's avatar
Hixie committed
34
    this.color,
35
    this.dismissible = true,
36
    this.semanticsLabel,
37
    this.barrierSemanticsDismissible = true,
38 39
  }) : super(key: key);

40
  /// If non-null, fill the barrier with this color.
41 42 43 44 45
  ///
  /// See also:
  ///
  ///  * [ModalRoute.barrierColor], which controls this property for the
  ///    [ModalBarrier] built by [ModalRoute] pages.
46
  final Color color;
47 48

  /// Whether touching the barrier will pop the current route off the [Navigator].
49 50 51 52 53
  ///
  /// See also:
  ///
  ///  * [ModalRoute.barrierDismissible], which controls this property for the
  ///    [ModalBarrier] built by [ModalRoute] pages.
54
  final bool dismissible;
Adam Barth's avatar
Adam Barth committed
55

56 57 58
  /// Whether the modal barrier semantics are included in the semantics tree.
  ///
  /// See also:
59
  ///
60 61 62 63
  ///  * [ModalRoute.semanticsDismissible], which controls this property for
  ///    the [ModalBarrier] built by [ModalRoute] pages.
  final bool barrierSemanticsDismissible;

64
  /// Semantics label used for the barrier if it is [dismissible].
65 66 67 68 69 70 71 72 73 74
  ///
  /// The semantics label is read out by accessibility tools (e.g. TalkBack
  /// on Android and VoiceOver on iOS) when the barrier is focused.
  ///
  /// See also:
  ///
  ///  * [ModalRoute.barrierLabel], which controls this property for the
  ///    [ModalBarrier] built by [ModalRoute] pages.
  final String semanticsLabel;

75
  @override
Adam Barth's avatar
Adam Barth committed
76
  Widget build(BuildContext context) {
77
    assert(!dismissible || semanticsLabel == null || debugCheckHasDirectionality(context));
78 79 80 81 82 83 84
    bool platformSupportsDismissingBarrier;
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
        platformSupportsDismissingBarrier = false;
        break;
      case TargetPlatform.iOS:
85
      case TargetPlatform.macOS:
86 87 88 89 90
        platformSupportsDismissingBarrier = true;
        break;
    }
    assert(platformSupportsDismissingBarrier != null);
    final bool semanticsDismissible = dismissible && platformSupportsDismissingBarrier;
91
    final bool modalBarrierSemanticsDismissible = barrierSemanticsDismissible ?? semanticsDismissible;
92 93
    return BlockSemantics(
      child: ExcludeSemantics(
94 95 96
        // On Android, the back button is used to dismiss a modal. On iOS, some
        // modal barriers are not dismissible in accessibility mode.
        excluding: !semanticsDismissible || !modalBarrierSemanticsDismissible,
97
        child: _ModalBarrierGestureDetector(
98
          onDismiss: () {
99
            if (dismissible)
100
              Navigator.maybePop(context);
101
          },
102
          child: Semantics(
103 104
            label: semanticsDismissible ? semanticsLabel : null,
            textDirection: semanticsDismissible && semanticsLabel != null ? Directionality.of(context) : null,
105 106 107 108 109 110 111 112
            child: MouseRegion(
              opaque: true,
              child: ConstrainedBox(
                constraints: const BoxConstraints.expand(),
                child: color == null ? null : DecoratedBox(
                  decoration: BoxDecoration(
                    color: color,
                  ),
113 114 115 116 117 118
                ),
              ),
            ),
          ),
        ),
      ),
119 120 121 122
    );
  }
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/// A widget that prevents the user from interacting with widgets behind itself,
/// and can be configured with an animated color value.
///
/// The modal barrier is the scrim that is rendered behind each route, which
/// generally prevents the user from interacting with the route below the
/// current route, and normally partially obscures such routes.
///
/// For example, when a dialog is on the screen, the page below the dialog is
/// usually darkened by the modal barrier.
///
/// This widget is similar to [ModalBarrier] except that it takes an animated
/// [color] instead of a single color.
///
/// See also:
///
///  * [ModalRoute], which uses this widget.
139
class AnimatedModalBarrier extends AnimatedWidget {
140
  /// Creates a widget that blocks user interaction.
141
  const AnimatedModalBarrier({
142
    Key key,
143
    Animation<Color> color,
144
    this.dismissible = true,
145
    this.semanticsLabel,
146
    this.barrierSemanticsDismissible,
147
  }) : super(key: key, listenable: color);
148

149
  /// If non-null, fill the barrier with this color.
150 151 152 153 154
  ///
  /// See also:
  ///
  ///  * [ModalRoute.barrierColor], which controls this property for the
  ///    [AnimatedModalBarrier] built by [ModalRoute] pages.
155
  Animation<Color> get color => listenable as Animation<Color>;
156 157

  /// Whether touching the barrier will pop the current route off the [Navigator].
158 159 160 161 162
  ///
  /// See also:
  ///
  ///  * [ModalRoute.barrierDismissible], which controls this property for the
  ///    [AnimatedModalBarrier] built by [ModalRoute] pages.
163
  final bool dismissible;
164

165
  /// Semantics label used for the barrier if it is [dismissible].
166 167 168 169 170 171 172 173 174
  ///
  /// The semantics label is read out by accessibility tools (e.g. TalkBack
  /// on Android and VoiceOver on iOS) when the barrier is focused.
  /// See also:
  ///
  ///  * [ModalRoute.barrierLabel], which controls this property for the
  ///    [ModalBarrier] built by [ModalRoute] pages.
  final String semanticsLabel;

175 176 177
  /// Whether the modal barrier semantics are included in the semantics tree.
  ///
  /// See also:
178
  ///
179 180 181 182
  ///  * [ModalRoute.semanticsDismissible], which controls this property for
  ///    the [ModalBarrier] built by [ModalRoute] pages.
  final bool barrierSemanticsDismissible;

183
  @override
184
  Widget build(BuildContext context) {
185
    return ModalBarrier(
186
      color: color?.value,
187
      dismissible: dismissible,
188
      semanticsLabel: semanticsLabel,
189
      barrierSemanticsDismissible: barrierSemanticsDismissible,
190 191 192
    );
  }
}
193 194 195 196 197

// Recognizes tap down by any pointer button.
//
// It is similar to [TapGestureRecognizer.onTapDown], but accepts any single
// button, which means the gesture also takes parts in gesture arenas.
198 199 200
class _AnyTapGestureRecognizer extends BaseTapGestureRecognizer {
  _AnyTapGestureRecognizer({ Object debugOwner })
    : super(debugOwner: debugOwner);
201

202
  VoidCallback onAnyTapUp;
203

204
  @protected
205
  @override
206
  bool isPointerAllowed(PointerDownEvent event) {
207
    if (onAnyTapUp == null)
208 209
      return false;
    return super.isPointerAllowed(event);
210 211
  }

212
  @protected
213
  @override
214
  void handleTapDown({PointerDownEvent down}) {
215
    // Do nothing.
216 217
  }

218
  @protected
219
  @override
220
  void handleTapUp({PointerDownEvent down, PointerUpEvent up}) {
221 222
    if (onAnyTapUp != null)
      onAnyTapUp();
223 224
  }

225
  @protected
226
  @override
227 228
  void handleTapCancel({PointerDownEvent down, PointerCancelEvent cancel, String reason}) {
    // Do nothing.
229 230 231 232 233 234 235
  }

  @override
  String get debugDescription => 'any tap';
}

class _ModalBarrierSemanticsDelegate extends SemanticsGestureDelegate {
236
  const _ModalBarrierSemanticsDelegate({this.onDismiss});
237

238
  final VoidCallback onDismiss;
239 240 241

  @override
  void assignSemantics(RenderSemanticsGestureHandler renderObject) {
242
    renderObject.onTap = onDismiss;
243 244 245 246 247
  }
}


class _AnyTapGestureRecognizerFactory extends GestureRecognizerFactory<_AnyTapGestureRecognizer> {
248
  const _AnyTapGestureRecognizerFactory({this.onAnyTapUp});
249

250
  final VoidCallback onAnyTapUp;
251 252 253 254 255 256

  @override
  _AnyTapGestureRecognizer constructor() => _AnyTapGestureRecognizer();

  @override
  void initializer(_AnyTapGestureRecognizer instance) {
257
    instance.onAnyTapUp = onAnyTapUp;
258 259 260 261 262 263 264 265 266
  }
}

// A GestureDetector used by ModalBarrier. It only has one callback,
// [onAnyTapDown], which recognizes tap down unconditionally.
class _ModalBarrierGestureDetector extends StatelessWidget {
  const _ModalBarrierGestureDetector({
    Key key,
    @required this.child,
267
    @required this.onDismiss,
268
  }) : assert(child != null),
269
       assert(onDismiss != null),
270 271 272 273 274 275
       super(key: key);

  /// The widget below this widget in the tree.
  /// See [RawGestureDetector.child].
  final Widget child;

276 277 278
  /// Immediately called when an event that should dismiss the modal barrier
  /// has happened.
  final VoidCallback onDismiss;
279 280 281 282

  @override
  Widget build(BuildContext context) {
    final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{
283
      _AnyTapGestureRecognizer: _AnyTapGestureRecognizerFactory(onAnyTapUp: onDismiss),
284 285 286 287 288
    };

    return RawGestureDetector(
      gestures: gestures,
      behavior: HitTestBehavior.opaque,
289
      semantics: _ModalBarrierSemanticsDelegate(onDismiss: onDismiss),
290 291 292 293
      child: child,
    );
  }
}