floating_action_button_theme.dart 12.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' show lerpDouble;

import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';

10 11
import 'material_state.dart';

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/// Defines default property values for descendant [FloatingActionButton]
/// widgets.
///
/// Descendant widgets obtain the current [FloatingActionButtonThemeData] object
/// using `Theme.of(context).floatingActionButtonTheme`. Instances of
/// [FloatingActionButtonThemeData] can be customized with
/// [FloatingActionButtonThemeData.copyWith].
///
/// Typically a [FloatingActionButtonThemeData] is specified as part of the
/// overall [Theme] with [ThemeData.floatingActionButtonTheme].
///
/// All [FloatingActionButtonThemeData] properties are `null` by default.
/// When null, the [FloatingActionButton] will use the values from [ThemeData]
/// if they exist, otherwise it will provide its own defaults.
///
/// See also:
///
///  * [ThemeData], which describes the overall theme information for the
///    application.
31
@immutable
32
class FloatingActionButtonThemeData with Diagnosticable {
33 34 35 36
  /// Creates a theme that can be used for
  /// [ThemeData.floatingActionButtonTheme].
  const FloatingActionButtonThemeData({
    this.foregroundColor,
37 38 39
    this.backgroundColor,
    this.focusColor,
    this.hoverColor,
40
    this.splashColor,
41
    this.elevation,
42 43
    this.focusElevation,
    this.hoverElevation,
44 45 46
    this.disabledElevation,
    this.highlightElevation,
    this.shape,
47
    this.enableFeedback,
48
    this.iconSize,
49 50 51 52 53
    this.sizeConstraints,
    this.smallSizeConstraints,
    this.largeSizeConstraints,
    this.extendedSizeConstraints,
    this.extendedIconLabelSpacing,
54
    this.extendedPadding,
55
    this.extendedTextStyle,
56
    this.mouseCursor,
57 58
  });

59 60
  /// Color to be used for the unselected, enabled [FloatingActionButton]'s
  /// foreground.
61
  final Color? foregroundColor;
62

63 64
  /// Color to be used for the unselected, enabled [FloatingActionButton]'s
  /// background.
65
  final Color? backgroundColor;
66

67
  /// The color to use for filling the button when the button has input focus.
68
  final Color? focusColor;
69 70 71

  /// The color to use for filling the button when the button has a pointer
  /// hovering over it.
72
  final Color? hoverColor;
73

74
  /// The splash color for this [FloatingActionButton]'s [InkWell].
75
  final Color? splashColor;
76

77 78
  /// The z-coordinate to be used for the unselected, enabled
  /// [FloatingActionButton]'s elevation foreground.
79
  final double? elevation;
80

81 82 83 84
  /// The z-coordinate at which to place this button relative to its parent when
  /// the button has the input focus.
  ///
  /// This controls the size of the shadow below the floating action button.
85
  final double? focusElevation;
86 87 88 89 90

  /// The z-coordinate at which to place this button relative to its parent when
  /// the button is enabled and has a pointer hovering over it.
  ///
  /// This controls the size of the shadow below the floating action button.
91
  final double? hoverElevation;
92

93 94
  /// The z-coordinate to be used for the disabled [FloatingActionButton]'s
  /// elevation foreground.
95
  final double? disabledElevation;
96 97 98

  /// The z-coordinate to be used for the selected, enabled
  /// [FloatingActionButton]'s elevation foreground.
99
  final double? highlightElevation;
100 101

  /// The shape to be used for the floating action button's [Material].
102
  final ShapeBorder? shape;
103

104 105 106 107 108 109
  /// If specified, defines the feedback property for [FloatingActionButton].
  ///
  /// If [FloatingActionButton.enableFeedback] is provided, [enableFeedback] is
  /// ignored.
  final bool? enableFeedback;

110 111 112
  /// Overrides the default icon size for the [FloatingActionButton];
  final double? iconSize;

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  /// Overrides the default size constraints for the [FloatingActionButton].
  final BoxConstraints? sizeConstraints;

  /// Overrides the default size constraints for [FloatingActionButton.small].
  final BoxConstraints? smallSizeConstraints;

  /// Overrides the default size constraints for [FloatingActionButton.large].
  final BoxConstraints? largeSizeConstraints;

  /// Overrides the default size constraints for [FloatingActionButton.extended].
  final BoxConstraints? extendedSizeConstraints;

  /// The spacing between the icon and the label for an extended
  /// [FloatingActionButton].
  final double? extendedIconLabelSpacing;

129 130 131
  /// The padding for an extended [FloatingActionButton]'s content.
  final EdgeInsetsGeometry? extendedPadding;

132 133 134
  /// The text style for an extended [FloatingActionButton]'s label.
  final TextStyle? extendedTextStyle;

135 136 137 138 139
  /// {@macro flutter.material.RawMaterialButton.mouseCursor}
  ///
  /// If specified, overrides the default value of [FloatingActionButton.mouseCursor].
  final MaterialStateProperty<MouseCursor?>? mouseCursor;

140 141 142
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  FloatingActionButtonThemeData copyWith({
143 144 145 146 147 148 149 150 151 152 153
    Color? foregroundColor,
    Color? backgroundColor,
    Color? focusColor,
    Color? hoverColor,
    Color? splashColor,
    double? elevation,
    double? focusElevation,
    double? hoverElevation,
    double? disabledElevation,
    double? highlightElevation,
    ShapeBorder? shape,
154
    bool? enableFeedback,
155
    double? iconSize,
156 157 158 159 160
    BoxConstraints? sizeConstraints,
    BoxConstraints? smallSizeConstraints,
    BoxConstraints? largeSizeConstraints,
    BoxConstraints? extendedSizeConstraints,
    double? extendedIconLabelSpacing,
161
    EdgeInsetsGeometry? extendedPadding,
162
    TextStyle? extendedTextStyle,
163
    MaterialStateProperty<MouseCursor?>? mouseCursor,
164 165 166
  }) {
    return FloatingActionButtonThemeData(
      foregroundColor: foregroundColor ?? this.foregroundColor,
167 168 169
      backgroundColor: backgroundColor ?? this.backgroundColor,
      focusColor: focusColor ?? this.focusColor,
      hoverColor: hoverColor ?? this.hoverColor,
170
      splashColor: splashColor ?? this.splashColor,
171
      elevation: elevation ?? this.elevation,
172 173
      focusElevation: focusElevation ?? this.focusElevation,
      hoverElevation: hoverElevation ?? this.hoverElevation,
174 175 176
      disabledElevation: disabledElevation ?? this.disabledElevation,
      highlightElevation: highlightElevation ?? this.highlightElevation,
      shape: shape ?? this.shape,
177
      enableFeedback: enableFeedback ?? this.enableFeedback,
178
      iconSize: iconSize ?? this.iconSize,
179 180 181 182 183
      sizeConstraints: sizeConstraints ?? this.sizeConstraints,
      smallSizeConstraints: smallSizeConstraints ?? this.smallSizeConstraints,
      largeSizeConstraints: largeSizeConstraints ?? this.largeSizeConstraints,
      extendedSizeConstraints: extendedSizeConstraints ?? this.extendedSizeConstraints,
      extendedIconLabelSpacing: extendedIconLabelSpacing ?? this.extendedIconLabelSpacing,
184
      extendedPadding: extendedPadding ?? this.extendedPadding,
185
      extendedTextStyle: extendedTextStyle ?? this.extendedTextStyle,
186
      mouseCursor: mouseCursor ?? this.mouseCursor,
187 188 189 190 191 192 193 194
    );
  }

  /// Linearly interpolate between two floating action button themes.
  ///
  /// If both arguments are null then null is returned.
  ///
  /// {@macro dart.ui.shadow.lerp}
195
  static FloatingActionButtonThemeData? lerp(FloatingActionButtonThemeData? a, FloatingActionButtonThemeData? b, double t) {
196 197
    if (identical(a, b)) {
      return a;
198
    }
199 200
    return FloatingActionButtonThemeData(
      foregroundColor: Color.lerp(a?.foregroundColor, b?.foregroundColor, t),
201 202 203
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
      focusColor: Color.lerp(a?.focusColor, b?.focusColor, t),
      hoverColor: Color.lerp(a?.hoverColor, b?.hoverColor, t),
204
      splashColor: Color.lerp(a?.splashColor, b?.splashColor, t),
205
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
206 207
      focusElevation: lerpDouble(a?.focusElevation, b?.focusElevation, t),
      hoverElevation: lerpDouble(a?.hoverElevation, b?.hoverElevation, t),
208 209 210
      disabledElevation: lerpDouble(a?.disabledElevation, b?.disabledElevation, t),
      highlightElevation: lerpDouble(a?.highlightElevation, b?.highlightElevation, t),
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
211
      enableFeedback: t < 0.5 ? a?.enableFeedback : b?.enableFeedback,
212
      iconSize: lerpDouble(a?.iconSize, b?.iconSize, t),
213 214 215 216 217
      sizeConstraints: BoxConstraints.lerp(a?.sizeConstraints, b?.sizeConstraints, t),
      smallSizeConstraints: BoxConstraints.lerp(a?.smallSizeConstraints, b?.smallSizeConstraints, t),
      largeSizeConstraints: BoxConstraints.lerp(a?.largeSizeConstraints, b?.largeSizeConstraints, t),
      extendedSizeConstraints: BoxConstraints.lerp(a?.extendedSizeConstraints, b?.extendedSizeConstraints, t),
      extendedIconLabelSpacing: lerpDouble(a?.extendedIconLabelSpacing, b?.extendedIconLabelSpacing, t),
218
      extendedPadding: EdgeInsetsGeometry.lerp(a?.extendedPadding, b?.extendedPadding, t),
219
      extendedTextStyle: TextStyle.lerp(a?.extendedTextStyle, b?.extendedTextStyle, t),
220
      mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor,
221 222 223 224
    );
  }

  @override
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  int get hashCode => Object.hash(
    foregroundColor,
    backgroundColor,
    focusColor,
    hoverColor,
    splashColor,
    elevation,
    focusElevation,
    hoverElevation,
    disabledElevation,
    highlightElevation,
    shape,
    enableFeedback,
    iconSize,
    sizeConstraints,
    smallSizeConstraints,
    largeSizeConstraints,
    extendedSizeConstraints,
    extendedIconLabelSpacing,
    extendedPadding,
245 246 247 248
    Object.hash(
      extendedTextStyle,
      mouseCursor,
    ),
249
  );
250 251 252

  @override
  bool operator ==(Object other) {
253
    if (identical(this, other)) {
254
      return true;
255 256
    }
    if (other.runtimeType != runtimeType) {
257
      return false;
258
    }
259 260 261 262 263 264 265 266 267 268 269
    return other is FloatingActionButtonThemeData
        && other.foregroundColor == foregroundColor
        && other.backgroundColor == backgroundColor
        && other.focusColor == focusColor
        && other.hoverColor == hoverColor
        && other.splashColor == splashColor
        && other.elevation == elevation
        && other.focusElevation == focusElevation
        && other.hoverElevation == hoverElevation
        && other.disabledElevation == disabledElevation
        && other.highlightElevation == highlightElevation
270
        && other.shape == shape
271
        && other.enableFeedback == enableFeedback
272
        && other.iconSize == iconSize
273 274 275 276
        && other.sizeConstraints == sizeConstraints
        && other.smallSizeConstraints == smallSizeConstraints
        && other.largeSizeConstraints == largeSizeConstraints
        && other.extendedSizeConstraints == extendedSizeConstraints
277
        && other.extendedIconLabelSpacing == extendedIconLabelSpacing
278
        && other.extendedPadding == extendedPadding
279 280
        && other.extendedTextStyle == extendedTextStyle
        && other.mouseCursor == mouseCursor;
281 282 283 284 285
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
286 287 288 289 290 291 292 293 294 295 296 297 298

    properties.add(ColorProperty('foregroundColor', foregroundColor, defaultValue: null));
    properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
    properties.add(ColorProperty('focusColor', focusColor, defaultValue: null));
    properties.add(ColorProperty('hoverColor', hoverColor, defaultValue: null));
    properties.add(ColorProperty('splashColor', splashColor, defaultValue: null));
    properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
    properties.add(DoubleProperty('focusElevation', focusElevation, defaultValue: null));
    properties.add(DoubleProperty('hoverElevation', hoverElevation, defaultValue: null));
    properties.add(DoubleProperty('disabledElevation', disabledElevation, defaultValue: null));
    properties.add(DoubleProperty('highlightElevation', highlightElevation, defaultValue: null));
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
    properties.add(DiagnosticsProperty<bool>('enableFeedback', enableFeedback, defaultValue: null));
299
    properties.add(DoubleProperty('iconSize', iconSize, defaultValue: null));
300 301 302 303 304
    properties.add(DiagnosticsProperty<BoxConstraints>('sizeConstraints', sizeConstraints, defaultValue: null));
    properties.add(DiagnosticsProperty<BoxConstraints>('smallSizeConstraints', smallSizeConstraints, defaultValue: null));
    properties.add(DiagnosticsProperty<BoxConstraints>('largeSizeConstraints', largeSizeConstraints, defaultValue: null));
    properties.add(DiagnosticsProperty<BoxConstraints>('extendedSizeConstraints', extendedSizeConstraints, defaultValue: null));
    properties.add(DoubleProperty('extendedIconLabelSpacing', extendedIconLabelSpacing, defaultValue: null));
305
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('extendedPadding', extendedPadding, defaultValue: null));
306
    properties.add(DiagnosticsProperty<TextStyle>('extendedTextStyle', extendedTextStyle, defaultValue: null));
307
    properties.add(DiagnosticsProperty<MaterialStateProperty<MouseCursor?>>('mouseCursor', mouseCursor, defaultValue: null));
308 309
  }
}