floating_action_button.dart 4.86 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
import 'package:meta/meta.dart';
7

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

// TODO(eseidel): This needs to change based on device size?
17
// http://material.google.com/layout/metrics-keylines.html#metrics-keylines-keylines-spacing
18
const double _kSize = 56.0;
Devon Carew's avatar
Devon Carew committed
19
const double _kSizeMini = 40.0;
20
final Object _kDefaultHeroTag = new Object();
21

22
/// A material design floating action button.
23 24
///
/// A floating action button is a circular icon button that hovers over content
25 26
/// to promote a primary action in the application. Floating action buttons are
/// most commonly used in the [Scaffold.floatingActionButton] field.
27 28 29 30 31 32
///
/// Use at most a single floating action button per screen. Floating action
/// buttons should be used for positive actions such as "create", "share", or
/// "navigate".
///
/// If the [onPressed] callback is not specified or null, then the button will
33
/// be disabled and will not react to touch.
34
///
35 36
/// See also:
///
37
///  * [Scaffold]
38 39
///  * [RaisedButton]
///  * [FlatButton]
40
///  * <https://material.google.com/components/buttons-floating-action-button.html>
41
class FloatingActionButton extends StatefulWidget {
42 43 44
  /// Creates a floating action button.
  ///
  /// Most commonly used in the [Scaffold.floatingActionButton] field.
45
  const FloatingActionButton({
46
    Key key,
47
    @required this.child,
Adam Barth's avatar
Adam Barth committed
48
    this.tooltip,
49
    this.backgroundColor,
50
    this.heroTag,
51 52
    this.elevation: 6,
    this.highlightElevation: 12,
53
    @required this.onPressed,
Devon Carew's avatar
Devon Carew committed
54
    this.mini: false
55 56
  }) : super(key: key);

57
  /// The widget below this widget in the tree.
58
  final Widget child;
59

60 61 62 63
  /// Text that describes the action that will occur when the button is pressed.
  ///
  /// This text is displayed when the user long-presses on the button and is
  /// used for accessibility.
Adam Barth's avatar
Adam Barth committed
64
  final String tooltip;
65 66 67 68

  /// The color to use when filling the button.
  ///
  /// Defaults to the accent color of the current theme.
69
  final Color backgroundColor;
70

71 72 73 74 75
  /// The tag to apply to the button's [Hero] widget.
  ///
  /// Defaults to a tag that matches other floating action buttons.
  final Object heroTag;

76
  /// The callback that is called when the button is tapped or otherwise activated.
77 78
  ///
  /// If this is set to null, the button will be disabled.
79
  final VoidCallback onPressed;
80

81
  /// The z-coordinate at which to place this button.
82 83
  ///
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
84 85
  ///
  /// Defaults to 6, the appropriate elevation for floating action buttons.
86
  final int elevation;
87

88
  /// The z-coordinate at which to place this button when the user is touching the button.
89 90 91 92 93
  ///
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
  ///
  /// Defaults to 12, the appropriate elevation for floating action buttons
  /// while they are being touched.
94
  final int highlightElevation;
95

96 97 98 99 100
  /// Controls the size of this button.
  ///
  /// By default, floating action buttons are non-mini and have a height and
  /// width of 56.0 logical pixels. Mini floating action buttons have a height
  /// and width of 40.0 logical pixels.
Devon Carew's avatar
Devon Carew committed
101
  final bool mini;
102

103
  @override
104
  _FloatingActionButtonState createState() => new _FloatingActionButtonState();
105
}
106

107 108 109 110 111 112 113 114 115
class _FloatingActionButtonState extends State<FloatingActionButton> {
  bool _highlight = false;

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

116
  @override
117
  Widget build(BuildContext context) {
Adam Barth's avatar
Adam Barth committed
118
    Color iconColor = Colors.white;
119
    Color materialColor = config.backgroundColor;
120
    if (materialColor == null) {
Ian Hickson's avatar
Ian Hickson committed
121
      final ThemeData themeData = Theme.of(context);
122
      materialColor = themeData.accentColor;
123
      iconColor = themeData.accentIconTheme.color;
124 125
    }

Adam Barth's avatar
Adam Barth committed
126
    Widget result = new Center(
Ian Hickson's avatar
Ian Hickson committed
127 128
      child: new IconTheme.merge(
        context: context,
Adam Barth's avatar
Adam Barth committed
129
        data: new IconThemeData(color: iconColor),
130
        child: config.child
Adam Barth's avatar
Adam Barth committed
131 132 133 134 135 136 137 138 139 140
      )
    );

    if (config.tooltip != null) {
      result = new Tooltip(
        message: config.tooltip,
        child: result
      );
    }

141 142 143 144 145 146 147 148 149 150 151 152 153 154
    return new Hero(
      tag: config.heroTag ?? _kDefaultHeroTag,
      child: new Material(
        color: materialColor,
        type: MaterialType.circle,
        elevation: _highlight ? config.highlightElevation : config.elevation,
        child: new Container(
          width: config.mini ? _kSizeMini : _kSize,
          height: config.mini ? _kSizeMini : _kSize,
          child: new InkWell(
            onTap: config.onPressed,
            onHighlightChanged: _handleHighlightChanged,
            child: result
          )
155 156 157 158 159
        )
      )
    );
  }
}