snack_bar.dart 2.87 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/animated_value.dart';
Matt Perry's avatar
Matt Perry committed
7
import 'package:sky/animation/animation_performance.dart';
8
import 'package:sky/animation/curves.dart';
9 10
import 'package:sky/painting/text_style.dart';
import 'package:sky/theme/typography.dart' as typography;
11 12 13 14 15 16 17
import 'package:sky/src/widgets/basic.dart';
import 'package:sky/src/widgets/default_text_style.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
18

19
export 'package:sky/animation/animation_performance.dart' show AnimationStatus;
Matt Perry's avatar
Matt Perry committed
20

21
typedef void SnackBarDismissedCallback();
Matt Perry's avatar
Matt Perry committed
22 23 24

const Duration _kSlideInDuration = const Duration(milliseconds: 200);

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

  final String label;
  final Function onPressed;

  Widget build() {
34 35
    return new GestureDetector(
      onTap: onPressed,
36 37 38 39 40 41 42 43
      child: new Container(
        margin: const EdgeDims.only(left: 24.0),
        padding: const EdgeDims.only(top: 14.0, bottom: 14.0),
        child: new Text(label)
      )
    );
  }
}
44

45
class SnackBar extends Component {
46 47 48

  SnackBar({
    Key key,
49
    this.anchor,
50 51 52
    this.content,
    this.actions,
    this.showing,
53
    this.onDismissed
54 55 56 57
  }) : super(key: key) {
    assert(content != null);
  }

58
  Anchor anchor;
59 60 61
  Widget content;
  List<SnackBarAction> actions;
  bool showing;
62
  SnackBarDismissedCallback onDismissed;
63

64 65 66 67 68 69 70 71 72 73 74
  Widget build() {
    List<Widget> children = [
      new Flexible(
        child: new Container(
          margin: const EdgeDims.symmetric(vertical: 14.0),
          child: new DefaultTextStyle(
            style: typography.white.subhead,
            child: content
          )
        )
      )
75 76 77
    ];
    if (actions != null)
      children.addAll(actions);
Matt Perry's avatar
Matt Perry committed
78

79
    return new SlideTransition(
80 81
      duration: _kSlideInDuration,
      direction: showing ? Direction.forward : Direction.reverse,
82 83 84
      position: new AnimatedValue<Point>(Point.origin,
                                         end: const Point(0.0, -52.0),
                                         curve: easeIn, reverseCurve: easeOut),
85
      onDismissed: onDismissed,
86
      anchor: anchor,
87
      child: new Material(
Matt Perry's avatar
Matt Perry committed
88 89 90 91 92 93 94
        level: 2,
        color: const Color(0xFF323232),
        type: MaterialType.canvas,
        child: new Container(
          margin: const EdgeDims.symmetric(horizontal: 24.0),
          child: new DefaultTextStyle(
            style: new TextStyle(color: Theme.of(this).accentColor),
95
            child: new Row(children)
Matt Perry's avatar
Matt Perry committed
96
          )
97 98 99 100 101
        )
      )
    );
  }
}