raised_button.dart 4.37 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/foundation.dart';
6
import 'package:flutter/widgets.dart';
7

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

12 13 14 15 16 17 18 19 20
/// A material design "raised button".
///
/// A raised button consists of a rectangular piece of material that hovers over
/// the interface.
///
/// Use raised buttons to add dimension to otherwise mostly flat layouts, e.g.
/// in long busy lists of content, or in wide spaces. Avoid using raised buttons
/// on already-raised content such as dialogs or cards.
///
21 22 23 24
/// If the [onPressed] callback is null, then the button will be disabled and by
/// default will appear like a flat button in the [disabledColor]. 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.
25
///
26
/// Requires one of its ancestors to be a [Material] widget.
27
///
28
/// See also:
29
///
30
///  * [FlatButton]
31
///  * [DropdownButton]
32
///  * [FloatingActionButton]
33
///  * <https://material.google.com/components/buttons.html>
34
class RaisedButton extends StatelessWidget {
35 36 37 38
  /// Creates a raised button.
  ///
  /// The [child] argument is required and is typically a [Text] widget in all
  /// caps.
39
  RaisedButton({
40
    Key key,
41
    @required this.onPressed,
42 43 44 45 46
    this.color,
    this.disabledColor,
    this.elevation: 2,
    this.highlightElevation: 8,
    this.disabledElevation: 0,
47 48 49 50
    this.colorBrightness,
    this.child
  }) : super(key: key);

51
  /// The callback that is called when the button is tapped or otherwise activated.
52 53 54
  ///
  /// If this is set to null, the button will be disabled.
  final VoidCallback onPressed;
55

56 57
  /// The color of the button, as printed on the [Material]. Defaults to null,
  /// meaning that the color is automatically derived from the [Theme].
58 59 60 61 62 63 64 65
  ///
  /// ```dart
  ///  new RaisedButton(
  ///    color: Colors.blue[500],
  ///    onPressed: _handleTap,
  ///    child: new Text('DEMO'),
  ///  ),
  /// ```
66
  final Color color;
67 68 69 70

  /// 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.
71 72
  final Color disabledColor;

73
  /// The z-coordinate at which to place this button.
74 75
  ///
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
76 77
  ///
  /// Defaults to 2, the appropriate elevation for raised buttons.
78
  final int elevation;
79

80
  /// The z-coordinate at which to place this button when highlighted.
81 82
  ///
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
83 84 85
  ///
  /// Defaults to 8, the appropriate elevation for raised buttons while they are
  /// being touched.
86
  final int highlightElevation;
87

88
  /// The z-coordinate at which to place this button when disabled.
89 90
  ///
  /// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, 24
91 92
  ///
  /// Defaults to 0, the appropriate elevation for disabled raised buttons.
93 94
  final int disabledElevation;

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
  final Widget child;
104

105 106 107 108 109 110 111
  /// 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;

  Color _getColor(BuildContext context) {
    if (enabled) {
      return color ?? Theme.of(context).buttonColor;
112
    } else {
113 114
      if (disabledColor != null)
        return disabledColor;
115
      final Brightness brightness = Theme.of(context).brightness;
116
      assert(brightness != null);
pq's avatar
pq committed
117
      switch (brightness) {
118
        case Brightness.light:
119
          return Colors.black12;
120
        case Brightness.dark:
121 122
          return Colors.white12;
      }
pq's avatar
pq committed
123
      return null;
124 125
    }
  }
126 127 128 129 130 131 132 133 134

  @override
  Widget build(BuildContext context) {
    return new MaterialButton(
      onPressed: onPressed,
      color: _getColor(context),
      elevation: enabled ? elevation : disabledElevation,
      highlightElevation: enabled ? highlightElevation : disabledElevation,
      colorBrightness: colorBrightness,
135
      child: child,
136 137
    );
  }
138
}