snack_bar.dart 2.7 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 'dart:async';

7
import 'package:sky/animation/animated_value.dart';
M
rebase  
Matt Perry 已提交
8
import 'package:sky/animation/animation_performance.dart';
9 10 11 12 13 14
import 'package:sky/painting/text_style.dart';
import 'package:sky/theme/typography.dart' as typography;
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/default_text_style.dart';
import 'package:sky/widgets/material.dart';
import 'package:sky/widgets/theme.dart';
15
import 'package:sky/widgets/transitions.dart';
M
rebase  
Matt Perry 已提交
16

17
export 'package:sky/animation/animation_performance.dart' show AnimationStatus;
M
rebase  
Matt Perry 已提交
18

19
typedef void SnackBarDismissedCallback();
M
rebase  
Matt Perry 已提交
20 21 22

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

23
class SnackBarAction extends Component {
H
Hixie 已提交
24
  SnackBarAction({Key key, this.label, this.onPressed }) : super(key: key) {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    assert(label != null);
  }

  final String label;
  final Function onPressed;

  Widget build() {
    return new Listener(
      onGestureTap: (_) => onPressed(),
      child: new Container(
        margin: const EdgeDims.only(left: 24.0),
        padding: const EdgeDims.only(top: 14.0, bottom: 14.0),
        child: new Text(label)
      )
    );
  }
}
42
class SnackBar extends Component {
43 44 45 46 47 48

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

  Widget content;
  List<SnackBarAction> actions;
  bool showing;
57
  SnackBarDismissedCallback onDismissed;
58

59
  void _onDismissed() {
60 61
    if (onDismissed != null)
      scheduleMicrotask(() { onDismissed(); });
62 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);
M
rebase  
Matt Perry 已提交
78

M
Matt Perry 已提交
79
    return new SlideTransition(
80 81 82 83 84
      duration: _kSlideInDuration,
      direction: showing ? Direction.forward : Direction.reverse,
      position: new AnimatedValue<Point>(const Point(0.0, 50.0),
                                         end: Point.origin),
      onDismissed: _onDismissed,
85
      child: new Material(
M
rebase  
Matt Perry 已提交
86 87 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),
            child: new Flex(children)
          )
95 96 97 98 99
        )
      )
    );
  }
}