raised_button.dart 2.89 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 'colors.dart';
9
import 'theme.dart';
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/// 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.
///
/// If the [onPressed] callback is not specified or 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.
///
/// See also:
27
///
28
///  * [FlatButton] class
29
///  * <https://www.google.com/design/spec/components/buttons.html>
30 31
class RaisedButton extends MaterialButton {
  RaisedButton({
32
    Key key,
33
    Widget child,
34 35 36 37 38 39
    this.color,
    this.colorBrightness,
    this.disabledColor,
    this.elevation: 2,
    this.highlightElevation: 8,
    this.disabledElevation: 0,
40
    VoidCallback onPressed
41 42
  }) : super(key: key,
             child: child,
43
             onPressed: onPressed);
44

45 46
  /// The color of the button, as printed on the [Material]. Defaults to null,
  /// meaning that the color is automatically derived from the [Theme].
47
  final Color color;
48 49 50 51

  /// 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.
52 53 54 55 56 57 58 59 60
  final Color disabledColor;

  /// Controls the default text color if the text color isn't explicit set.
  final ThemeBrightness colorBrightness;

  final int elevation;
  final int highlightElevation;
  final int disabledElevation;

61
  @override
62
  _RaisedButtonState createState() => new _RaisedButtonState();
63
}
64

65
class _RaisedButtonState extends MaterialButtonState<RaisedButton> {
66
  @override
67 68 69 70 71 72 73 74 75
  int get elevation {
    if (config.enabled) {
      if (highlight)
        return config.highlightElevation;
      return config.elevation;
    } else {
      return config.disabledElevation;
    }
  }
76

77
  @override
78
  Color getColor(BuildContext context) {
79
    if (config.enabled) {
80
      return config.color ?? Theme.of(context).buttonColor;
81
    } else {
82 83
      if (config.disabledColor != null)
        return config.disabledColor;
84 85 86 87 88 89
      switch (Theme.of(context).brightness) {
        case ThemeBrightness.light:
          return Colors.black12;
        case ThemeBrightness.dark:
          return Colors.white12;
      }
90 91 92
    }
  }

93
  @override
94
  ThemeBrightness getColorBrightness(BuildContext context) {
95
    return config.colorBrightness ?? Theme.of(context).brightness;
96
  }
97
}