floating_action_button.dart 3.02 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/animation.dart';
6
import 'package:flutter/widgets.dart';
7

Adam Barth's avatar
Adam Barth committed
8 9
import 'icon_theme.dart';
import 'icon_theme_data.dart';
10 11 12
import 'ink_well.dart';
import 'material.dart';
import 'theme.dart';
13 14 15 16

// TODO(eseidel): This needs to change based on device size?
// http://www.google.com/design/spec/layout/metrics-keylines.html#metrics-keylines-keylines-spacing
const double _kSize = 56.0;
Devon Carew's avatar
Devon Carew committed
17
const double _kSizeMini = 40.0;
18 19
const Duration _kChildSegue = const Duration(milliseconds: 400);
const Interval _kChildSegueInterval = const Interval(0.65, 1.0);
20

21 22
class FloatingActionButton extends StatefulComponent {
  const FloatingActionButton({
23
    Key key,
24 25
    this.child,
    this.backgroundColor,
26 27
    this.elevation: 6,
    this.highlightElevation: 12,
Devon Carew's avatar
Devon Carew committed
28 29
    this.onPressed,
    this.mini: false
30 31
  }) : super(key: key);

32 33
  final Widget child;
  final Color backgroundColor;
34
  final VoidCallback onPressed;
35 36
  final int elevation;
  final int highlightElevation;
Devon Carew's avatar
Devon Carew committed
37
  final bool mini;
38

39
  _FloatingActionButtonState createState() => new _FloatingActionButtonState();
40
}
41

42
class _FloatingActionButtonState extends State<FloatingActionButton> {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  final Performance _childSegue = new Performance(duration: _kChildSegue);

  void initState() {
    super.initState();
    _childSegue.play();
  }

  void didUpdateConfig(FloatingActionButton oldConfig) {
    super.didUpdateConfig(oldConfig);
    if (Widget.canUpdate(oldConfig.child, config.child) && config.backgroundColor == oldConfig.backgroundColor)
      return;
    _childSegue
      ..progress = 0.0
      ..play();
  }

59 60 61 62 63 64 65 66 67
  bool _highlight = false;

  void _handleHighlightChanged(bool value) {
    setState(() {
      _highlight = value;
    });
  }

  Widget build(BuildContext context) {
68
    IconThemeColor iconThemeColor = IconThemeColor.white;
69
    Color materialColor = config.backgroundColor;
70
    if (materialColor == null) {
71
      ThemeData themeData = Theme.of(context);
72 73 74 75 76 77 78
      materialColor = themeData.accentColor;
      iconThemeColor = themeData.accentColorBrightness == ThemeBrightness.dark ? IconThemeColor.white : IconThemeColor.black;
    }

    return new Material(
      color: materialColor,
      type: MaterialType.circle,
79 80
      elevation: _highlight ? config.highlightElevation : config.elevation,
      child: new Container(
Devon Carew's avatar
Devon Carew committed
81 82
        width: config.mini ? _kSizeMini : _kSize,
        height: config.mini ? _kSizeMini : _kSize,
83 84 85 86 87 88
        child: new InkWell(
          onTap: config.onPressed,
          onHighlightChanged: _handleHighlightChanged,
          child: new Center(
            child: new IconTheme(
              data: new IconThemeData(color: iconThemeColor),
89 90 91 92 93
              child: new RotationTransition(
                performance: _childSegue,
                turns: new AnimatedValue<double>(-0.125, end: 0.0, curve: _kChildSegueInterval),
                child: config.child
              )
94 95 96 97 98 99 100
            )
          )
        )
      )
    );
  }
}