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

6
import 'package:sky/animation.dart';
7
import 'package:sky/painting.dart';
8
import 'package:sky/material.dart';
9
import 'package:sky/src/widgets/animated_component.dart';
10 11 12 13 14 15
import 'package:sky/src/widgets/basic.dart';
import 'package:sky/src/widgets/framework.dart';
import 'package:sky/src/widgets/gesture_detector.dart';
import 'package:sky/src/widgets/material.dart';
import 'package:sky/src/widgets/theme.dart';
import 'package:sky/src/widgets/transitions.dart';
Matt Perry's avatar
Matt Perry committed
16

17
typedef void SnackBarDismissedCallback();
Matt Perry's avatar
Matt Perry committed
18 19

const Duration _kSlideInDuration = const Duration(milliseconds: 200);
20 21 22 23
const double kSnackHeight = 52.0;
const double kSideMargins = 24.0;
const double kVerticalPadding = 14.0;
const Color kSnackBackground = const Color(0xFF323232);
Matt Perry's avatar
Matt Perry committed
24

25
class SnackBarAction extends StatelessComponent {
26
  SnackBarAction({Key key, this.label, this.onPressed }) : super(key: key) {
27 28 29 30 31 32
    assert(label != null);
  }

  final String label;
  final Function onPressed;

33
  Widget build(BuildContext) {
34 35
    return new GestureDetector(
      onTap: onPressed,
36
      child: new Container(
37 38
        margin: const EdgeDims.only(left: kSideMargins),
        padding: const EdgeDims.symmetric(vertical: kVerticalPadding),
39 40 41 42 43
        child: new Text(label)
      )
    );
  }
}
44

45
class SnackBar extends AnimatedComponent {
46 47 48 49
  SnackBar({
    Key key,
    this.content,
    this.actions,
50
    bool showing,
51
    this.onDismissed
52
  }) : super(key: key, direction: showing ? Direction.forward : Direction.reverse, duration: _kSlideInDuration) {
53 54 55
    assert(content != null);
  }

56 57 58
  final Widget content;
  final List<SnackBarAction> actions;
  final SnackBarDismissedCallback onDismissed;
59

60 61
  SnackBarState createState() => new SnackBarState();
}
62

63
class SnackBarState extends AnimatedState<SnackBar> {
64
  void handleDismissed() {
65 66
    if (config.onDismissed != null)
      config.onDismissed();
67 68
  }

69
  Widget build(BuildContext context) {
70 71 72
    List<Widget> children = [
      new Flexible(
        child: new Container(
73
          margin: const EdgeDims.symmetric(vertical: kVerticalPadding),
74
          child: new DefaultTextStyle(
75
            style: Typography.white.subhead,
76
            child: config.content
77 78 79
          )
        )
      )
80
    ];
81 82 83
    if (config.actions != null)
      children.addAll(config.actions);
    return new SquashTransition(
84
      performance: performance.view,
85 86 87
      height: new AnimatedValue<double>(
        0.0,
        end: kSnackHeight,
88 89 90
        curve: easeIn,
        reverseCurve: easeOut
      ),
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      child: new ClipRect(
        child: new OverflowBox(
          minHeight: kSnackHeight,
          maxHeight: kSnackHeight,
          child: new Material(
            level: 2,
            color: kSnackBackground,
            type: MaterialType.canvas,
            child: new Container(
              margin: const EdgeDims.symmetric(horizontal: kSideMargins),
              child: new DefaultTextStyle(
                style: new TextStyle(color: Theme.of(context).accentColor),
                child: new Row(children)
              )
            )
Matt Perry's avatar
Matt Perry committed
106
          )
107 108 109 110 111
        )
      )
    );
  }
}