raised_button.dart 2.02 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 9
import 'material_button.dart';
import 'theme.dart';
10 11 12

class RaisedButton extends MaterialButton {
  RaisedButton({
13
    Key key,
14
    Widget child,
15 16 17 18 19 20
    this.color,
    this.colorBrightness,
    this.disabledColor,
    this.elevation: 2,
    this.highlightElevation: 8,
    this.disabledElevation: 0,
21
    VoidCallback onPressed
22 23
  }) : super(key: key,
             child: child,
24
             onPressed: onPressed);
25

26 27 28 29 30 31 32 33 34 35
  final Color color;
  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;

36
  _RaisedButtonState createState() => new _RaisedButtonState();
37
}
38

39
class _RaisedButtonState extends MaterialButtonState<RaisedButton> {
40

41 42 43 44 45 46 47 48 49
  int get elevation {
    if (config.enabled) {
      if (highlight)
        return config.highlightElevation;
      return config.elevation;
    } else {
      return config.disabledElevation;
    }
  }
50

51
  Color getColor(BuildContext context) {
52
    if (config.enabled) {
53 54
      if (config.color != null)
        return config.color;
55
      switch (Theme.of(context).brightness) {
56
        case ThemeBrightness.light:
57
          return Colors.grey[300];
58
        case ThemeBrightness.dark:
59
          Map<int, Color> swatch = Theme.of(context).primarySwatch ?? Colors.blue;
60
          return swatch[600];
61 62
      }
    } else {
63 64
      if (config.disabledColor != null)
        return config.disabledColor;
65 66 67 68 69 70
      switch (Theme.of(context).brightness) {
        case ThemeBrightness.light:
          return Colors.black12;
        case ThemeBrightness.dark:
          return Colors.white12;
      }
71 72 73
    }
  }

74
  ThemeBrightness getColorBrightness(BuildContext context) {
75
    return config.colorBrightness ?? Theme.of(context).brightness;
76 77
  }

78
}