snack_bar.dart 5.25 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

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

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

Hixie's avatar
Hixie committed
21 22 23 24 25 26
// 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".

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

33 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.
///
/// See also:
///  * https://www.google.com/design/spec/components/snackbars-toasts.html
40
class SnackBarAction extends StatelessWidget {
41
  SnackBarAction({Key key, this.label, this.onPressed }) : super(key: key) {
42
    assert(label != null);
43
    assert(onPressed != null);
44 45
  }

46
  /// The button label.
47
  final String label;
48 49

  /// The callback to be invoked when the button is pressed. Must be non-null.
50
  final VoidCallback onPressed;
51

Hixie's avatar
Hixie committed
52
  Widget build(BuildContext context) {
53
    return new Container(
54
      margin: const EdgeInsets.only(left: _kSideMargins),
55 56 57
      child: new FlatButton(
        onPressed: onPressed,
        textTheme: ButtonColor.accent,
58 59 60 61 62
        child: new Text(label)
      )
    );
  }
}
63

64 65 66 67 68 69 70 71 72
/// 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:
///  * [Scaffold.of] and [ScaffoldState.showSnackBar]
///  * [SnackBarAction]
///  * https://www.google.com/design/spec/components/snackbars-toasts.html
73
class SnackBar extends StatelessWidget {
Hixie's avatar
Hixie committed
74
  SnackBar({
75 76
    Key key,
    this.content,
77
    this.action,
Hixie's avatar
Hixie committed
78
    this.duration: kSnackBarShortDisplayDuration,
79
    this.animation
80
  }) : super(key: key) {
81 82 83
    assert(content != null);
  }

84
  final Widget content;
85
  final SnackBarAction action;
Hixie's avatar
Hixie committed
86
  final Duration duration;
87
  final Animation<double> animation;
88

89
  Widget build(BuildContext context) {
90
    assert(animation != null);
Hixie's avatar
Hixie committed
91
    List<Widget> children = <Widget>[
92 93
      new Flexible(
        child: new Container(
94
          margin: const EdgeInsets.symmetric(vertical: _kSingleLineVerticalPadding),
95
          child: new DefaultTextStyle(
96
            style: Typography.white.subhead,
97
            child: content
98 99 100
          )
        )
      )
101
    ];
102 103
    if (action != null)
      children.add(action);
104 105
    CurvedAnimation heightAnimation = new CurvedAnimation(parent: animation, curve: _snackBarHeightCurve);
    CurvedAnimation fadeAnimation = new CurvedAnimation(parent: animation, curve: _snackBarFadeCurve);
106
    ThemeData theme = Theme.of(context);
107 108 109
    return new ClipRect(
      child: new AnimatedBuilder(
        animation: heightAnimation,
110
        builder: (BuildContext context, Widget child) {
111 112 113 114 115
          return new Align(
            alignment: const FractionalOffset(0.0, 0.0),
            heightFactor: heightAnimation.value,
            child: child
          );
116
        },
Hixie's avatar
Hixie committed
117 118 119 120 121 122
        child: new Semantics(
          container: true,
          child: new Material(
            elevation: 6,
            color: _kSnackBackground,
            child: new Container(
123
              margin: const EdgeInsets.symmetric(horizontal: _kSideMargins),
Hixie's avatar
Hixie committed
124 125 126 127 128
              child: new Theme(
                data: new ThemeData(
                  brightness: ThemeBrightness.dark,
                  accentColor: theme.accentColor,
                  accentColorBrightness: theme.accentColorBrightness,
129
                  textTheme: Typography.white
Hixie's avatar
Hixie committed
130 131 132 133 134
                ),
                child: new FadeTransition(
                  opacity: fadeAnimation,
                  child: new Row(
                    children: children,
135
                    crossAxisAlignment: CrossAxisAlignment.center
Hixie's avatar
Hixie committed
136
                  )
137 138 139 140 141
                )
              )
            )
          )
        )
142 143
      )
    );
144
  }
145

Hixie's avatar
Hixie committed
146
  // API for Scaffold.addSnackBar():
147

148 149
  static AnimationController createAnimationController() {
    return new AnimationController(
150
      duration: _kSnackBarTransitionDuration,
Hixie's avatar
Hixie committed
151 152 153
      debugLabel: 'SnackBar'
    );
  }
154

155
  SnackBar withAnimation(Animation<double> newAnimation, { Key fallbackKey }) {
Hixie's avatar
Hixie committed
156
    return new SnackBar(
157
      key: key ?? fallbackKey,
Hixie's avatar
Hixie committed
158
      content: content,
159
      action: action,
Hixie's avatar
Hixie committed
160
      duration: duration,
161
      animation: newAnimation
Hixie's avatar
Hixie committed
162 163
    );
  }
164
}