modal_barrier.dart 2.19 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
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 'package:flutter/animation.dart';

Adam Barth's avatar
Adam Barth committed
7 8 9
import 'basic.dart';
import 'framework.dart';
import 'navigator.dart';
10 11
import 'transitions.dart';

12
/// Prevents the user from interacting with widgets behind itself.
Adam Barth's avatar
Adam Barth committed
13
class ModalBarrier extends StatelessComponent {
14 15
  ModalBarrier({
    Key key,
Hixie's avatar
Hixie committed
16
    this.color,
17
    this.dismissable: true
18 19
  }) : super(key: key);

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

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

  Widget build(BuildContext context) {
    return new Listener(
      onPointerDown: (_) {
29
        if (dismissable)
Hixie's avatar
Hixie committed
30
          Navigator.pop(context);
Adam Barth's avatar
Adam Barth committed
31
      },
Hixie's avatar
Hixie committed
32
      behavior: HitTestBehavior.opaque,
33 34
      child: new ConstrainedBox(
        constraints: const BoxConstraints.expand(),
Hixie's avatar
Hixie committed
35
        child: color == null ? null : new DecoratedBox(
Adam Barth's avatar
Adam Barth committed
36 37 38 39
          decoration: new BoxDecoration(
            backgroundColor: color
          )
        )
40 41 42 43 44
      )
    );
  }
}

45
/// Prevents the user from interacting with widgets behind itself.
46 47
class AnimatedModalBarrier extends StatelessComponent {
  AnimatedModalBarrier({
48 49
    Key key,
    this.color,
50 51
    this.performance,
    this.dismissable: true
52 53
  }) : super(key: key);

54 55 56
  /// If non-null, fill the barrier with this color.
  ///
  /// The barrier will animate this color according to the given [performance].
57
  final AnimatedColorValue color;
58 59

  /// The performance to use when animating the given [color].
60
  final PerformanceView performance;
61 62

  /// Whether touching the barrier will pop the current route off the [Navigator].
63
  final bool dismissable;
64 65 66 67 68 69 70 71

  Widget build(BuildContext context) {
    return new BuilderTransition(
      performance: performance,
      variables: <AnimatedColorValue>[color],
      builder: (BuildContext context) {
        return new IgnorePointer(
          ignoring: performance.status == PerformanceStatus.reverse,
72 73 74 75
          child: new ModalBarrier(
            color: color.value,
            dismissable: dismissable
          )
76 77 78 79 80
        );
      }
    );
  }
}