button.dart 8.08 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

7
import 'colors.dart';
8
import 'constants.dart';
9
import 'debug.dart';
10
import 'flat_button.dart';
11 12
import 'icon_theme_data.dart';
import 'icon_theme.dart';
13 14
import 'ink_well.dart';
import 'material.dart';
15
import 'raised_button.dart';
16
import 'theme.dart';
17

18 19 20 21 22 23 24
/// Whether a button should use the accent color for its text.
///
/// See also:
///
///  * [ButtonTheme]
///  * [RaisedButton]
///  * [FlatButton]
25
enum ButtonTextTheme {
26 27 28 29 30 31
  /// The button should use the normal color (e.g., black or white depending on the [ThemeData.brightness]) for its text.
  normal,

  /// The button should use the accent color (e.g., [ThemeData.accentColor]) for its text.
  accent,
}
32

33 34 35 36
/// Defines the button color used by a widget subtree.
///
/// See also:
///
37
///  * [ButtonTextTheme]
38 39
///  * [RaisedButton]
///  * [FlatButton]
40
class ButtonTheme extends InheritedWidget {
41 42 43 44
  /// Creates a button theme.
  ///
  /// The child argument is required.
  const ButtonTheme({
45
    Key key,
46 47 48 49
    this.textTheme: ButtonTextTheme.normal,
    this.minWidth: 88.0,
    this.height: 36.0,
    this.padding: const EdgeInsets.symmetric(horizontal: 16.0),
50
    Widget child
51 52
  }) : super(key: key, child: child);

53 54 55 56 57 58 59 60
  /// Creates a button theme that is appropriate for footer buttons.
  ///
  /// This theme is denser, with a smaller [minWidth] and [padding], than the
  /// default theme. Also, this theme uses [ButtonTextTheme.accent] rather than
  /// [ButtonTextTheme.normal].
  ///
  /// For example, buttons at the bottom of [Dialog] or [Card] widgets use this
  /// button theme.
61 62 63 64 65 66 67 68
  const ButtonTheme.footer({
    Key key,
    this.textTheme: ButtonTextTheme.accent,
    this.minWidth: 64.0,
    this.height: 36.0,
    this.padding: const EdgeInsets.symmetric(horizontal: 8.0),
    Widget child
  }) : super(key: key, child: child);
69

70
  /// The button color that this subtree should use.
71
  final ButtonTextTheme textTheme;
72

73 74 75 76 77 78
  /// The smallest horizontal extent that the button will occupy.
  ///
  /// Defaults to 88.0 logical pixels.
  final double minWidth;

  /// The vertical extent of the button.
79
  ///
80 81 82 83 84 85 86 87 88 89
  /// Defaults to 36.0 logical pixels.
  final double height;

  /// The amount of space to surround the child inside the bounds of the button.
  ///
  /// Defaults to 16.0 pixels of horizontal padding.
  final EdgeInsets padding;

  /// The color from the closest instance of this class that encloses the given context.
  static ButtonTheme of(BuildContext context) {
Ian Hickson's avatar
Ian Hickson committed
90
    ButtonTheme result = context.inheritFromWidgetOfExactType(ButtonTheme);
91
    return result ?? const ButtonTheme();
92 93
  }

94
  @override
95 96 97 98 99 100
  bool updateShouldNotify(ButtonTheme oldTheme) {
    return textTheme != oldTheme.textTheme
        || padding != oldTheme.padding
        || minWidth != oldTheme.minWidth
        || height != oldTheme.height;
  }
101 102
}

103
/// A material design button.
104
///
105
/// Rather than using this class directly, consider using [FlatButton] or [RaisedButton].
106 107 108
///
/// MaterialButtons whose [onPressed] handler is null will be disabled. To have
/// an enabled button, make sure to pass a non-null value for onPressed.
109 110 111 112 113
class MaterialButton extends StatefulWidget {
  /// Creates a material button.
  ///
  /// Rather than creating a material button directly, consider using
  /// [FlatButton] or [RaisedButton].
114
  MaterialButton({
115
    Key key,
116
    this.colorBrightness,
117
    this.textTheme,
118
    this.textColor,
119 120 121 122 123 124 125 126
    this.color,
    this.elevation,
    this.highlightElevation,
    this.minWidth,
    this.height,
    this.padding,
    this.onPressed,
    this.child
127
  }) : super(key: key);
128

129 130 131 132 133 134 135 136
  /// The theme brightness to use for this button.
  ///
  /// Defaults to the brightness from [ThemeData.brightness].
  final ThemeBrightness colorBrightness;

  /// The color scheme to use for this button's text.
  ///
  /// Defaults to the button color from [ButtonTheme].
137
  final ButtonTextTheme textTheme;
138

139
  /// The color to use for this button's text.
140
  final Color textColor;
141

142 143 144 145
  /// The color of the button, as printed on the [Material].
  final Color color;

  /// The z-coordinate at which to place this button.
146
  ///
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
  final int elevation;

  /// The z-coordinate at which to place this button when highlighted.
  ///
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
  final int highlightElevation;

  /// The smallest horizontal extent that the button will occupy.
  ///
  /// Defaults to the value from the current [ButtonTheme].
  final double minWidth;

  /// The vertical extent of the button.
  ///
  /// Defaults to the value from the current [ButtonTheme].
  final double height;

  /// The amount of space to surround the child inside the bounds of the button.
  ///
  /// Defaults to the value from the current [ButtonTheme].
  final EdgeInsets padding;
169 170 171 172

  /// The callback that is invoked when the button is tapped or otherwise activated.
  ///
  /// If this is set to null, the button will be disabled.
173
  final VoidCallback onPressed;
Hixie's avatar
Hixie committed
174

175 176 177
  /// The widget below this widget in the tree.
  final Widget child;

178 179
  /// Whether the button is enabled or disabled. Buttons are disabled by default. To
  /// enable a button, set its [onPressed] property to a non-null value.
180 181
  bool get enabled => onPressed != null;

182 183 184
  @override
  _MaterialButtonState createState() => new _MaterialButtonState();

185
  @override
Hixie's avatar
Hixie committed
186 187 188 189 190
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    if (!enabled)
      description.add('disabled');
  }
191 192
}

193 194
class _MaterialButtonState extends State<MaterialButton> {
  bool _highlight = false;
195

196
  ThemeBrightness get _colorBrightness {
197 198 199
    return config.colorBrightness ?? Theme.of(context).brightness;
  }

200 201 202
  Color get _textColor {
    if (config.textColor != null)
      return config.textColor;
203
    if (config.enabled) {
204 205
      switch (config.textTheme ?? ButtonTheme.of(context).textTheme) {
        case ButtonTextTheme.accent:
206
          return Theme.of(context).accentColor;
207 208
        case ButtonTextTheme.normal:
          switch (_colorBrightness) {
209 210 211 212 213 214
            case ThemeBrightness.light:
              return Colors.black87;
            case ThemeBrightness.dark:
              return Colors.white;
          }
      }
215 216 217 218 219 220 221
    } else {
      switch (_colorBrightness) {
        case ThemeBrightness.light:
          return Colors.black26;
        case ThemeBrightness.dark:
          return Colors.white30;
      }
222 223 224
    }
  }

225 226
  void _handleHighlightChanged(bool value) {
    setState(() {
227
      _highlight = value;
228 229 230
    });
  }

231
  @override
232
  Widget build(BuildContext context) {
233
    assert(debugCheckHasMaterial(context));
234 235 236 237 238
    final Color textColor = _textColor;
    final TextStyle style = Theme.of(context).textTheme.button.copyWith(color: textColor);
    final ButtonTheme buttonTheme = ButtonTheme.of(context);
    final double height = config.height ?? buttonTheme.height;
    final int elevation = (_highlight ? config.highlightElevation : config.elevation) ?? 0;
239 240 241 242 243 244 245 246
    Widget contents = new IconTheme(
      data: new IconThemeData(
        color: textColor
      ),
      child: new InkWell(
        onTap: config.onPressed,
        onHighlightChanged: _handleHighlightChanged,
        child: new Container(
247
          padding: config.padding ?? ButtonTheme.of(context).padding,
248 249 250 251
          child: new Center(
            widthFactor: 1.0,
            child: config.child
          )
252
        )
253
      )
254
    );
255
    if (elevation > 0 || config.color != null) {
256 257
      contents = new Material(
        type: MaterialType.button,
258
        color: config.color,
259 260 261 262 263
        elevation: elevation,
        textStyle: style,
        child: contents
      );
    } else {
264
      contents = new AnimatedDefaultTextStyle(
265
        style: style,
266
        duration: kThemeChangeDuration,
267 268 269
        child: contents
      );
    }
270 271 272 273 274 275
    return new ConstrainedBox(
      constraints: new BoxConstraints(
        minWidth: config.minWidth ?? buttonTheme.minWidth,
        minHeight: height,
        maxHeight: height
      ),
276
      child: contents
277 278 279
    );
  }
}