flat_button.dart 3.91 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

8
import 'button.dart';
9
import 'theme.dart';
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/// A material design "flat button".
///
/// A flat button is a section printed on a [Material] widget that reacts to
/// touches by filling with color.
///
/// Use flat buttons on toolbars, in dialogs, or inline with other content but
/// offset from that content with padding so that the button's presence is
/// obvious. Flat buttons intentionally do not have visible borders and must
/// therefore rely on their position relative to other content for context. In
/// dialogs and cards, they should be grouped together in one of the bottom
/// corners. Avoid using flat buttons where they would blend in with other
/// content, for example in the middle of lists.
///
/// If the [onPressed] callback is not specified or null, then the button will
/// be disabled, will not react to touch, and will be colored as specified by
/// the [disabledColor] property instead of the [color] property. If you are
/// trying to change the button's [color] and it is not having any effect, check
/// that you are passing a non-null [onPressed] handler.
///
30 31
/// Requires one of its ancestors to be a [Material] widget.
///
32
/// See also:
33
///
34
///  * [RaisedButton]
35
///  * [DropdownButton]
36
///  * <https://material.google.com/components/buttons.html>
37
class FlatButton extends StatelessWidget {
38 39 40 41
  /// Creates a flat button.
  ///
  /// The [child] argument is required and is typically a [Text] widget in all
  /// caps.
42
  FlatButton({
43
    Key key,
44
    @required this.onPressed,
45 46
    this.textColor,
    this.disabledTextColor,
47 48
    this.color,
    this.disabledColor,
49 50 51
    this.textTheme,
    this.colorBrightness,
    this.child
52 53 54
  }) : super(key: key) {
    assert(child != null);
  }
55

56
  /// The callback that is called when the button is tapped or otherwise activated.
57 58 59 60 61 62 63 64 65 66 67 68 69
  ///
  /// If this is set to null, the button will be disabled.
  final VoidCallback onPressed;

  /// The color to use for this button's text.
  ///
  /// Defaults to the color determined by the [textTheme].
  final Color textColor;

  /// The color to use for this button's text when the button cannot be pressed.
  ///
  /// Defaults to a color derived from the [Theme].
  final Color disabledTextColor;
70

71
  /// The color of the button, as printed on the [Material]. Defaults to null,
72
  /// meaning that the color is automatically derived from the [Theme].
73 74 75 76 77 78 79 80 81 82
  ///
  /// Typically, a material design color will be used, as follows:
  ///
  /// ```dart
  ///  new FlatButton(
  ///    color: Colors.blue[500],
  ///    onPressed: _handleTap,
  ///    child: new Text('DEMO'),
  ///  ),
  /// ```
83
  final Color color;
84 85 86 87

  /// The color of the button when the button is disabled. Buttons are disabled
  /// by default. To enable a button, set its [onPressed] property to a non-null
  /// value.
88 89
  final Color disabledColor;

90 91 92 93
  /// The color scheme to use for this button's text.
  ///
  /// Defaults to the button color from [ButtonTheme].
  final ButtonTextTheme textTheme;
94

95 96 97
  /// The theme brightness to use for this button.
  ///
  /// Defaults to the brightness from [ThemeData.brightness].
98
  final Brightness colorBrightness;
99 100

  /// The widget below this widget in the tree.
101 102
  ///
  /// Typically a [Text] widget in all caps.
103 104 105 106 107
  final Widget child;

  /// 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.
  bool get enabled => onPressed != null;
108

109
  @override
110 111 112 113 114 115 116 117 118
  Widget build(BuildContext context) {
    return new MaterialButton(
      onPressed: onPressed,
      textColor: enabled ? textColor : disabledTextColor,
      color: enabled ? color : disabledColor,
      textTheme: textTheme,
      colorBrightness: colorBrightness,
      child: child
    );
119 120
  }
}