flat_button.dart 3.65 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 'button.dart';
8
import 'theme.dart';
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/// 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.
///
29 30
/// Requires one of its ancestors to be a [Material] widget.
///
31
/// See also:
32
///
33 34
///  * [RaisedButton]
///  * [DropDownButton]
35
///  * <https://www.google.com/design/spec/components/buttons.html>
36
class FlatButton extends StatelessWidget {
37 38 39 40
  /// Creates a flat button.
  ///
  /// The [child] argument is required and is typically a [Text] widget in all
  /// caps.
41
  FlatButton({
42
    Key key,
43 44 45
    this.onPressed,
    this.textColor,
    this.disabledTextColor,
46 47
    this.color,
    this.disabledColor,
48 49 50
    this.textTheme,
    this.colorBrightness,
    this.child
51 52 53
  }) : super(key: key) {
    assert(child != null);
  }
54

55
  /// The callback that is called when the button is tapped or otherwise activated.
56 57 58 59 60 61 62 63 64 65 66 67 68
  ///
  /// 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;
69

70
  /// The color of the button, as printed on the [Material]. Defaults to null,
71
  /// meaning that the color is automatically derived from the [Theme].
72
  final Color color;
73 74 75 76

  /// 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.
77 78
  final Color disabledColor;

79 80 81 82
  /// The color scheme to use for this button's text.
  ///
  /// Defaults to the button color from [ButtonTheme].
  final ButtonTextTheme textTheme;
83

84 85 86 87 88 89
  /// The theme brightness to use for this button.
  ///
  /// Defaults to the brightness from [ThemeData.brightness].
  final ThemeBrightness colorBrightness;

  /// The widget below this widget in the tree.
90 91
  ///
  /// Typically a [Text] widget in all caps.
92 93 94 95 96
  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;
97

98
  @override
99 100 101 102 103 104 105 106 107
  Widget build(BuildContext context) {
    return new MaterialButton(
      onPressed: onPressed,
      textColor: enabled ? textColor : disabledTextColor,
      color: enabled ? color : disabledColor,
      textTheme: textTheme,
      colorBrightness: colorBrightness,
      child: child
    );
108 109
  }
}