raised_button.dart 9.93 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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/rendering.dart';
7
import 'package:flutter/widgets.dart';
8

9
import 'button.dart';
10
import 'button_theme.dart';
11
import 'material_button.dart';
12
import 'theme.dart';
13
import 'theme_data.dart';
14

15
/// A Material Design "raised button".
16
///
17
/// ### This class is deprecated, please use [ElevatedButton] instead.
18 19
///
/// FlatButton, RaisedButton, and OutlineButton have been replaced by
20 21 22 23
/// [TextButton], [ElevatedButton], and [OutlinedButton] respectively.
/// ButtonTheme has been replaced by [TextButtonTheme],
/// [ElevatedButtonTheme], and [OutlinedButtonTheme]. The original classes
/// will eventually be removed, please migrate code that uses them.
24 25 26
/// There's a detailed migration guide for the new button and button
/// theme classes in
/// [flutter.dev/go/material-button-migration-guide](https://flutter.dev/go/material-button-migration-guide).
27 28
@Deprecated(
  'Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). '
29
  'This feature was deprecated after v1.26.0-18.0.pre.',
30
)
31
class RaisedButton extends MaterialButton {
32
  /// Create a filled button.
33
  ///
34 35 36 37
  /// The [autofocus] and [clipBehavior] arguments must not be null.
  /// Additionally,  [elevation], [hoverElevation], [focusElevation],
  /// [highlightElevation], and [disabledElevation] must be non-negative, if
  /// specified.
38 39
  @Deprecated(
    'Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). '
40
    'This feature was deprecated after v1.26.0-18.0.pre.',
41
  )
42
  const RaisedButton({
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    Key? key,
    required VoidCallback? onPressed,
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHighlightChanged,
    MouseCursor? mouseCursor,
    ButtonTextTheme? textTheme,
    Color? textColor,
    Color? disabledTextColor,
    Color? color,
    Color? disabledColor,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    Brightness? colorBrightness,
    double? elevation,
    double? focusElevation,
    double? hoverElevation,
    double? highlightElevation,
    double? disabledElevation,
    EdgeInsetsGeometry? padding,
    VisualDensity? visualDensity,
    ShapeBorder? shape,
66
    Clip clipBehavior = Clip.none,
67
    FocusNode? focusNode,
68
    bool autofocus = false,
69 70 71
    MaterialTapTargetSize? materialTapTargetSize,
    Duration? animationDuration,
    Widget? child,
72 73
  }) : assert(autofocus != null),
       assert(elevation == null || elevation >= 0.0),
74 75
       assert(focusElevation == null || focusElevation >= 0.0),
       assert(hoverElevation == null || hoverElevation >= 0.0),
76 77
       assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(disabledElevation == null || disabledElevation >= 0.0),
78
       assert(clipBehavior != null),
79 80 81
       super(
         key: key,
         onPressed: onPressed,
82
         onLongPress: onLongPress,
83
         onHighlightChanged: onHighlightChanged,
84
         mouseCursor: mouseCursor,
85 86 87 88 89
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
90 91
         focusColor: focusColor,
         hoverColor: hoverColor,
92 93 94 95
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         elevation: elevation,
96 97
         focusElevation: focusElevation,
         hoverElevation: hoverElevation,
98 99 100
         highlightElevation: highlightElevation,
         disabledElevation: disabledElevation,
         padding: padding,
101
         visualDensity: visualDensity,
102 103
         shape: shape,
         clipBehavior: clipBehavior,
104
         focusNode: focusNode,
105
         autofocus: autofocus,
106 107 108 109
         materialTapTargetSize: materialTapTargetSize,
         animationDuration: animationDuration,
         child: child,
       );
110 111 112 113 114 115 116

  /// Create a filled button from a pair of widgets that serve as the button's
  /// [icon] and [label].
  ///
  /// The icon and label are arranged in a row and padded by 12 logical pixels
  /// at the start, and 16 at the end, with an 8 pixel gap in between.
  ///
117 118
  /// The [elevation], [highlightElevation], [disabledElevation], [icon],
  /// [label], and [clipBehavior] arguments must not be null.
119 120
  @Deprecated(
    'Use ElevatedButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). '
121
    'This feature was deprecated after v1.26.0-18.0.pre.',
122
  )
123
  factory RaisedButton.icon({
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    Key? key,
    required VoidCallback? onPressed,
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHighlightChanged,
    MouseCursor? mouseCursor,
    ButtonTextTheme? textTheme,
    Color? textColor,
    Color? disabledTextColor,
    Color? color,
    Color? disabledColor,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    Brightness? colorBrightness,
    double? elevation,
    double? highlightElevation,
    double? disabledElevation,
    ShapeBorder? shape,
143
    Clip clipBehavior,
144
    FocusNode? focusNode,
145
    bool autofocus,
146 147 148
    EdgeInsetsGeometry? padding,
    MaterialTapTargetSize? materialTapTargetSize,
    Duration? animationDuration,
149 150
    required Widget icon,
    required Widget label,
151
  }) = _RaisedButtonWithIcon;
152

153
  @override
154
  Widget build(BuildContext context) {
155
    final ThemeData theme = Theme.of(context);
156
    final ButtonThemeData buttonTheme = ButtonTheme.of(context);
157
    return RawMaterialButton(
158
      onPressed: onPressed,
159
      onLongPress: onLongPress,
160
      onHighlightChanged: onHighlightChanged,
161
      mouseCursor: mouseCursor,
162
      clipBehavior: clipBehavior,
163
      fillColor: buttonTheme.getFillColor(this),
164
      textStyle: theme.textTheme.button!.copyWith(color: buttonTheme.getTextColor(this)),
165 166
      focusColor: buttonTheme.getFocusColor(this),
      hoverColor: buttonTheme.getHoverColor(this),
167 168 169
      highlightColor: buttonTheme.getHighlightColor(this),
      splashColor: buttonTheme.getSplashColor(this),
      elevation: buttonTheme.getElevation(this),
170 171
      focusElevation: buttonTheme.getFocusElevation(this),
      hoverElevation: buttonTheme.getHoverElevation(this),
172 173 174
      highlightElevation: buttonTheme.getHighlightElevation(this),
      disabledElevation: buttonTheme.getDisabledElevation(this),
      padding: buttonTheme.getPadding(this),
175
      visualDensity: visualDensity ?? theme.visualDensity,
176 177
      constraints: buttonTheme.getConstraints(this),
      shape: buttonTheme.getShape(this),
178
      focusNode: focusNode,
179
      autofocus: autofocus,
180 181
      animationDuration: buttonTheme.getAnimationDuration(this),
      materialTapTargetSize: buttonTheme.getMaterialTapTargetSize(this),
182
      child: child,
183 184
    );
  }
185 186

  @override
187 188
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
189
    properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
190 191
    properties.add(DiagnosticsProperty<double>('focusElevation', focusElevation, defaultValue: null));
    properties.add(DiagnosticsProperty<double>('hoverElevation', hoverElevation, defaultValue: null));
192 193
    properties.add(DiagnosticsProperty<double>('highlightElevation', highlightElevation, defaultValue: null));
    properties.add(DiagnosticsProperty<double>('disabledElevation', disabledElevation, defaultValue: null));
194
  }
195
}
196

Shi-Hao Hong's avatar
Shi-Hao Hong committed
197
/// The type of RaisedButtons created with [RaisedButton.icon].
198 199 200
///
/// This class only exists to give RaisedButtons created with [RaisedButton.icon]
/// a distinct class for the sake of [ButtonTheme]. It can not be instantiated.
201
class _RaisedButtonWithIcon extends RaisedButton with MaterialButtonWithIconMixin {
202
  _RaisedButtonWithIcon({
203
    Key? key,
204
    required VoidCallback? onPressed,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    VoidCallback? onLongPress,
    ValueChanged<bool>? onHighlightChanged,
    MouseCursor? mouseCursor,
    ButtonTextTheme? textTheme,
    Color? textColor,
    Color? disabledTextColor,
    Color? color,
    Color? disabledColor,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    Brightness? colorBrightness,
    double? elevation,
    double? highlightElevation,
    double? disabledElevation,
    ShapeBorder? shape,
222
    Clip clipBehavior = Clip.none,
223
    FocusNode? focusNode,
224
    bool autofocus = false,
225 226 227 228 229
    EdgeInsetsGeometry? padding,
    MaterialTapTargetSize? materialTapTargetSize,
    Duration? animationDuration,
    required Widget icon,
    required Widget label,
230 231 232
  }) : assert(elevation == null || elevation >= 0.0),
       assert(highlightElevation == null || highlightElevation >= 0.0),
       assert(disabledElevation == null || disabledElevation >= 0.0),
233
       assert(clipBehavior != null),
234 235
       assert(icon != null),
       assert(label != null),
236
       assert(autofocus != null),
237 238 239
       super(
         key: key,
         onPressed: onPressed,
240
         onLongPress: onLongPress,
241
         onHighlightChanged: onHighlightChanged,
242
         mouseCursor: mouseCursor,
243 244 245 246 247
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
248 249
         focusColor: focusColor,
         hoverColor: hoverColor,
250 251 252 253 254 255 256 257
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         elevation: elevation,
         highlightElevation: highlightElevation,
         disabledElevation: disabledElevation,
         shape: shape,
         clipBehavior: clipBehavior,
258
         focusNode: focusNode,
259
         autofocus: autofocus,
260
         padding: padding,
261 262 263 264 265 266 267
         materialTapTargetSize: materialTapTargetSize,
         animationDuration: animationDuration,
         child: Row(
           mainAxisSize: MainAxisSize.min,
           children: <Widget>[
             icon,
             const SizedBox(width: 8.0),
268
             label,
269 270 271 272
           ],
         ),
       );
}