snack_bar.dart 7.23 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
import 'package:flutter/widgets.dart';
6
import 'package:meta/meta.dart';
7

8
import 'button.dart';
9
import 'flat_button.dart';
10
import 'material.dart';
11
import 'scaffold.dart';
12
import 'theme_data.dart';
13
import 'theme.dart';
14
import 'typography.dart';
Matt Perry's avatar
Matt Perry committed
15

Hixie's avatar
Hixie committed
16
// https://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-specs
17
const double _kSideMargins = 24.0;
Hixie's avatar
Hixie committed
18
const double _kSingleLineVerticalPadding = 14.0;
19 20
const double _kMultiLineVerticalTopPadding = 24.0;
const double _kMultiLineVerticalSpaceBetweenTextAndButtons = 10.0;
21
const Color _kSnackBackground = const Color(0xFF323232);
Matt Perry's avatar
Matt Perry committed
22

Hixie's avatar
Hixie committed
23 24 25 26 27 28
// TODO(ianh): We should check if the given text and actions are going to fit on
// one line or not, and if they are, use the single-line layout, and if not, use
// the multiline layout. See link above.

// TODO(ianh): Implement the Tablet version of snackbar if we're "on a tablet".

29
const Duration _kSnackBarTransitionDuration = const Duration(milliseconds: 250);
30 31
const Duration _kSnackBarShortDisplayDuration = const Duration(milliseconds: 1500);
const Duration _kSnackBarMediumDisplayDuration = const Duration(milliseconds: 2750);
32
const Curve _snackBarHeightCurve = Curves.fastOutSlowIn;
33
const Curve _snackBarFadeCurve = const Interval(0.72, 1.0, curve: Curves.fastOutSlowIn);
Hixie's avatar
Hixie committed
34

35 36 37 38 39
/// A button for a [SnackBar], known as an "action".
///
/// Snack bar actions are always enabled. If you want to disable a snack bar
/// action, simply don't include it in the snack bar.
///
40 41
/// Snack bar actions can only be pressed once. Subsequent presses are ignored.
///
42 43 44 45
/// See also:
///
///  * [SnackBar]
///  * <https://www.google.com/design/spec/components/snackbars-toasts.html>
46
class SnackBarAction extends StatefulWidget {
47 48 49
  /// Creates an action for a [SnackBar].
  ///
  /// The [label] and [onPressed] arguments must be non-null.
50 51 52 53 54
  SnackBarAction({
    Key key,
    this.label,
    @required this.onPressed
  }) : super(key: key) {
55
    assert(label != null);
56
    assert(onPressed != null);
57 58
  }

59
  /// The button label.
60
  final String label;
61

62
  /// The callback to be called when the button is pressed. Must not be null.
63
  ///
64
  /// This callback will be called at most once each time this action is
65
  /// displayed in a [SnackBar].
66
  final VoidCallback onPressed;
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  @override
  _SnackBarActionState createState() => new _SnackBarActionState();
}

class _SnackBarActionState extends State<SnackBarAction> {
  bool _haveTriggeredAction = false;

  void _handlePressed() {
    if (_haveTriggeredAction)
      return;
    setState(() {
      _haveTriggeredAction = true;
    });
    config.onPressed();
  }

84
  @override
Hixie's avatar
Hixie committed
85
  Widget build(BuildContext context) {
86
    return new Container(
87
      margin: const EdgeInsets.only(left: _kSideMargins),
88
      child: new FlatButton(
89
        onPressed: _haveTriggeredAction ? null : _handlePressed,
90
        textTheme: ButtonTextTheme.accent,
91
        child: new Text(config.label)
92 93 94 95
      )
    );
  }
}
96

97 98 99 100 101 102
/// A lightweight message with an optional action which briefly displays at the
/// bottom of the screen.
///
/// Displayed with the Scaffold.of().showSnackBar() API.
///
/// See also:
103
///
104 105
///  * [Scaffold.of] and [ScaffoldState.showSnackBar]
///  * [SnackBarAction]
106
///  * <https://www.google.com/design/spec/components/snackbars-toasts.html>
107
class SnackBar extends StatelessWidget {
108 109 110
  /// Creates a snack bar.
  ///
  /// The [content] argument must be non-null.
Hixie's avatar
Hixie committed
111
  SnackBar({
112 113
    Key key,
    this.content,
114
    this.action,
115
    this.duration: _kSnackBarShortDisplayDuration,
116
    this.animation
117
  }) : super(key: key) {
118 119 120
    assert(content != null);
  }

121 122 123
  /// The primary content of the snack bar.
  ///
  /// Typically a [Text] widget.
124
  final Widget content;
125 126 127 128 129

  /// (optional) An action that the user can take based on the snack bar.
  ///
  /// For example, the snack bar might let the user undo the operation that
  /// prompted the snackbar. Snack bars can have at most one action.
130
  final SnackBarAction action;
131 132

  /// The amount of time the snack bar should be displayed.
Hixie's avatar
Hixie committed
133
  final Duration duration;
134 135

  /// The animation driving the entrance and exit of the snack bar.
136
  final Animation<double> animation;
137

138
  @override
139
  Widget build(BuildContext context) {
140
    assert(animation != null);
Hixie's avatar
Hixie committed
141
    List<Widget> children = <Widget>[
142 143
      new Flexible(
        child: new Container(
144
          margin: const EdgeInsets.symmetric(vertical: _kSingleLineVerticalPadding),
145
          child: new DefaultTextStyle(
146
            style: Typography.white.subhead,
147
            child: content
148 149 150
          )
        )
      )
151
    ];
152 153
    if (action != null)
      children.add(action);
154
    CurvedAnimation heightAnimation = new CurvedAnimation(parent: animation, curve: _snackBarHeightCurve);
155
    CurvedAnimation fadeAnimation = new CurvedAnimation(parent: animation, curve: _snackBarFadeCurve, reverseCurve: const Step(0.0));
156
    ThemeData theme = Theme.of(context);
157 158 159
    return new ClipRect(
      child: new AnimatedBuilder(
        animation: heightAnimation,
160
        builder: (BuildContext context, Widget child) {
161
          return new Align(
162
            alignment: FractionalOffset.topLeft,
163 164 165
            heightFactor: heightAnimation.value,
            child: child
          );
166
        },
Hixie's avatar
Hixie committed
167 168
        child: new Semantics(
          container: true,
169 170 171
          child: new Dismissable(
            key: new Key('dismissable'),
            direction: DismissDirection.down,
172
            resizeDuration: null,
173 174 175 176 177 178 179 180 181 182
            onDismissed: (DismissDirection direction) {
              Scaffold.of(context).removeCurrentSnackBar();
            },
            child: new Material(
              elevation: 6,
              color: _kSnackBackground,
              child: new Container(
                margin: const EdgeInsets.symmetric(horizontal: _kSideMargins),
                child: new Theme(
                  data: new ThemeData(
183
                    brightness: Brightness.dark,
184 185 186 187 188 189 190 191 192 193
                    accentColor: theme.accentColor,
                    accentColorBrightness: theme.accentColorBrightness,
                    textTheme: Typography.white
                  ),
                  child: new FadeTransition(
                    opacity: fadeAnimation,
                    child: new Row(
                      children: children,
                      crossAxisAlignment: CrossAxisAlignment.center
                    )
Hixie's avatar
Hixie committed
194
                  )
195 196 197 198 199
                )
              )
            )
          )
        )
200 201
      )
    );
202
  }
203

Hixie's avatar
Hixie committed
204
  // API for Scaffold.addSnackBar():
205

206
  /// Creates an animation controller useful for driving a snack bar's entrance and exit animation.
207 208
  static AnimationController createAnimationController() {
    return new AnimationController(
209
      duration: _kSnackBarTransitionDuration,
Hixie's avatar
Hixie committed
210 211 212
      debugLabel: 'SnackBar'
    );
  }
213

214 215 216 217
  /// Creates a copy of this snack bar but with the animation replaced with the given animation.
  ///
  /// If the original snack bar lacks a key, the newly created snack bar will
  /// use the given fallback key.
218
  SnackBar withAnimation(Animation<double> newAnimation, { Key fallbackKey }) {
Hixie's avatar
Hixie committed
219
    return new SnackBar(
220
      key: key ?? fallbackKey,
Hixie's avatar
Hixie committed
221
      content: content,
222
      action: action,
Hixie's avatar
Hixie committed
223
      duration: duration,
224
      animation: newAnimation
Hixie's avatar
Hixie committed
225 226
    );
  }
227
}