modal_barrier.dart 2.01 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5
// 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 'basic.dart';
6
import 'container.dart';
Adam Barth's avatar
Adam Barth committed
7
import 'framework.dart';
Hixie's avatar
Hixie committed
8
import 'gesture_detector.dart';
Adam Barth's avatar
Adam Barth committed
9
import 'navigator.dart';
10 11
import 'transitions.dart';

12
/// A widget that prevents the user from interacting with widgets behind itself.
13
class ModalBarrier extends StatelessWidget {
14
  /// Creates a widget that blocks user interaction.
15
  const ModalBarrier({
16
    Key key,
Hixie's avatar
Hixie committed
17
    this.color,
18
    this.dismissible: true
19 20
  }) : super(key: key);

21
  /// If non-null, fill the barrier with this color.
22
  final Color color;
23 24

  /// Whether touching the barrier will pop the current route off the [Navigator].
25
  final bool dismissible;
Adam Barth's avatar
Adam Barth committed
26

27
  @override
Adam Barth's avatar
Adam Barth committed
28
  Widget build(BuildContext context) {
Hixie's avatar
Hixie committed
29 30 31
    return new Semantics(
      container: true,
      child: new GestureDetector(
32
        onTapDown: (TapDownDetails details) {
33
          if (dismissible)
Hixie's avatar
Hixie committed
34 35 36 37 38 39 40 41 42
            Navigator.pop(context);
        },
        behavior: HitTestBehavior.opaque,
        child: new ConstrainedBox(
          constraints: const BoxConstraints.expand(),
          child: color == null ? null : new DecoratedBox(
            decoration: new BoxDecoration(
              backgroundColor: color
            )
Adam Barth's avatar
Adam Barth committed
43 44
          )
        )
45 46 47 48 49
      )
    );
  }
}

50
/// A widget that prevents the user from interacting with widgets behind itself.
51
class AnimatedModalBarrier extends AnimatedWidget {
52
  /// Creates a widget that blocks user interaction.
53
  const AnimatedModalBarrier({
54
    Key key,
55
    Animation<Color> color,
56
    this.dismissible: true
57
  }) : super(key: key, listenable: color);
58

59
  /// If non-null, fill the barrier with this color.
60
  Animation<Color> get color => listenable;
61 62

  /// Whether touching the barrier will pop the current route off the [Navigator].
63
  final bool dismissible;
64

65
  @override
66
  Widget build(BuildContext context) {
67
    return new ModalBarrier(
68
      color: color?.value,
69
      dismissible: dismissible
70 71 72
    );
  }
}