flat_button.dart 7.94 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/widgets.dart';
6

7
import 'button.dart';
8
import 'button_theme.dart';
9
import 'material_button.dart';
10
import 'theme.dart';
11
import 'theme_data.dart';
12

13
/// A Material Design "flat button".
14
///
15
/// ### This class is deprecated, please use [TextButton] instead.
16 17
///
/// FlatButton, RaisedButton, and OutlineButton have been replaced by
18 19 20 21
/// [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.
22 23 24
/// 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).
25 26
@Deprecated(
  'Use TextButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). '
27
  'This feature was deprecated after v1.26.0-18.0.pre.',
28
)
29
class FlatButton extends MaterialButton {
30
  /// Create a simple text button.
31 32
  ///
  /// The [autofocus] and [clipBehavior] arguments must not be null.
33 34
  @Deprecated(
    'Use TextButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). '
35
    'This feature was deprecated after v1.26.0-18.0.pre.',
36
  )
37
  const FlatButton({
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    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,
    EdgeInsetsGeometry? padding,
    VisualDensity? visualDensity,
    ShapeBorder? shape,
56
    Clip clipBehavior = Clip.none,
57
    FocusNode? focusNode,
58
    bool autofocus = false,
59 60 61 62
    MaterialTapTargetSize? materialTapTargetSize,
    required Widget child,
    double? height,
    double? minWidth,
63 64
  }) : assert(clipBehavior != null),
       assert(autofocus != null),
65
       super(
66
         key: key,
67 68
         height: height,
         minWidth: minWidth,
69
         onPressed: onPressed,
70
         onLongPress: onLongPress,
71
         onHighlightChanged: onHighlightChanged,
72
         mouseCursor: mouseCursor,
73 74 75 76 77
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
78 79
         focusColor: focusColor,
         hoverColor: hoverColor,
80 81 82 83
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         padding: padding,
84
         visualDensity: visualDensity,
85 86
         shape: shape,
         clipBehavior: clipBehavior,
87
         focusNode: focusNode,
88
         autofocus: autofocus,
89 90 91
         materialTapTargetSize: materialTapTargetSize,
         child: child,
      );
92 93 94 95 96 97 98

  /// Create a text 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.
  ///
99
  /// The [icon], [label], and [clipBehavior] arguments must not be null.
100 101
  @Deprecated(
    'Use TextButton instead. See the migration guide in flutter.dev/go/material-button-migration-guide). '
102
    'This feature was deprecated after v1.26.0-18.0.pre.',
103
  )
104
  factory FlatButton.icon({
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    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,
    EdgeInsetsGeometry? padding,
    ShapeBorder? shape,
122
    Clip clipBehavior,
123
    FocusNode? focusNode,
124
    bool autofocus,
125
    MaterialTapTargetSize? materialTapTargetSize,
126 127
    required Widget icon,
    required Widget label,
128 129
    double? minWidth,
    double? height,
130
  }) = _FlatButtonWithIcon;
131

132
  @override
133
  Widget build(BuildContext context) {
134
    final ThemeData theme = Theme.of(context);
135
    final ButtonThemeData buttonTheme = ButtonTheme.of(context);
136
    return RawMaterialButton(
137
      onPressed: onPressed,
138
      onLongPress: onLongPress,
139
      onHighlightChanged: onHighlightChanged,
140
      mouseCursor: mouseCursor,
141
      fillColor: buttonTheme.getFillColor(this),
142
      textStyle: theme.textTheme.button!.copyWith(color: buttonTheme.getTextColor(this)),
143 144
      focusColor: buttonTheme.getFocusColor(this),
      hoverColor: buttonTheme.getHoverColor(this),
145 146 147
      highlightColor: buttonTheme.getHighlightColor(this),
      splashColor: buttonTheme.getSplashColor(this),
      elevation: buttonTheme.getElevation(this),
148 149
      focusElevation: buttonTheme.getFocusElevation(this),
      hoverElevation: buttonTheme.getHoverElevation(this),
150 151 152
      highlightElevation: buttonTheme.getHighlightElevation(this),
      disabledElevation: buttonTheme.getDisabledElevation(this),
      padding: buttonTheme.getPadding(this),
153
      visualDensity: visualDensity ?? theme.visualDensity,
154 155 156 157
      constraints: buttonTheme.getConstraints(this).copyWith(
        minWidth: minWidth,
        minHeight: height,
      ),
158
      shape: buttonTheme.getShape(this),
159
      clipBehavior: clipBehavior,
160
      focusNode: focusNode,
161
      autofocus: autofocus,
162
      materialTapTargetSize: buttonTheme.getMaterialTapTargetSize(this),
163
      animationDuration: buttonTheme.getAnimationDuration(this),
164
      child: child,
165
    );
166 167
  }
}
168

Shi-Hao Hong's avatar
Shi-Hao Hong committed
169
/// The type of FlatButtons created with [FlatButton.icon].
170 171 172
///
/// This class only exists to give FlatButtons created with [FlatButton.icon]
/// a distinct class for the sake of [ButtonTheme]. It can not be instantiated.
173
class _FlatButtonWithIcon extends FlatButton with MaterialButtonWithIconMixin {
174
  _FlatButtonWithIcon({
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    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,
    EdgeInsetsGeometry? padding,
    ShapeBorder? shape,
192
    Clip clipBehavior = Clip.none,
193
    FocusNode? focusNode,
194
    bool autofocus = false,
195 196 197 198 199
    MaterialTapTargetSize? materialTapTargetSize,
    required Widget icon,
    required Widget label,
    double? minWidth,
    double? height,
200 201
  }) : assert(icon != null),
       assert(label != null),
202
       assert(clipBehavior != null),
203
       assert(autofocus != null),
204 205 206
       super(
         key: key,
         onPressed: onPressed,
207
         onLongPress: onLongPress,
208
         onHighlightChanged: onHighlightChanged,
209
         mouseCursor: mouseCursor,
210 211 212 213 214
         textTheme: textTheme,
         textColor: textColor,
         disabledTextColor: disabledTextColor,
         color: color,
         disabledColor: disabledColor,
215 216
         focusColor: focusColor,
         hoverColor: hoverColor,
217 218 219 220 221 222
         highlightColor: highlightColor,
         splashColor: splashColor,
         colorBrightness: colorBrightness,
         padding: padding,
         shape: shape,
         clipBehavior: clipBehavior,
223
         focusNode: focusNode,
224
         autofocus: autofocus,
225 226 227 228 229 230
         materialTapTargetSize: materialTapTargetSize,
         child: Row(
           mainAxisSize: MainAxisSize.min,
           children: <Widget>[
             icon,
             const SizedBox(width: 8.0),
231
             label,
232 233
           ],
         ),
234 235
         minWidth: minWidth,
         height: height,
236 237 238
       );

}