bottom_sheet_theme.dart 8.29 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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';

/// Defines default property values for [BottomSheet]'s [Material].
///
/// Descendant widgets obtain the current [BottomSheetThemeData] object
/// using `Theme.of(context).bottomSheetTheme`. Instances of
/// [BottomSheetThemeData] can be customized with
/// [BottomSheetThemeData.copyWith].
///
/// Typically a [BottomSheetThemeData] is specified as part of the
/// overall [Theme] with [ThemeData.bottomSheetTheme].
///
/// All [BottomSheetThemeData] properties are `null` by default.
/// When null, the [BottomSheet] will provide its own defaults.
///
/// See also:
///
///  * [ThemeData], which describes the overall theme information for the
///    application.
27
@immutable
28
class BottomSheetThemeData with Diagnosticable {
29 30 31
  /// Creates a theme that can be used for [ThemeData.bottomSheetTheme].
  const BottomSheetThemeData({
    this.backgroundColor,
32
    this.surfaceTintColor,
33
    this.elevation,
34
    this.modalBackgroundColor,
35
    this.modalBarrierColor,
36
    this.shadowColor,
37
    this.modalElevation,
38
    this.shape,
39 40 41
    this.showDragHandle,
    this.dragHandleColor,
    this.dragHandleSize,
42
    this.clipBehavior,
43
    this.constraints,
44 45
  });

hangyu's avatar
hangyu committed
46
  /// Overrides the default value for [BottomSheet.backgroundColor].
47 48
  ///
  /// If null, [BottomSheet] defaults to [Material]'s default.
49
  final Color? backgroundColor;
50

hangyu's avatar
hangyu committed
51
  /// Overrides the default value for surfaceTintColor.
52 53 54 55 56 57
  ///
  /// If null, [BottomSheet] will not display an overlay color.
  ///
  /// See [Material.surfaceTintColor] for more details.
  final Color? surfaceTintColor;

hangyu's avatar
hangyu committed
58
  /// Overrides the default value for [BottomSheet.elevation].
59 60 61 62
  ///
  /// {@macro flutter.material.material.elevation}
  ///
  /// If null, [BottomSheet] defaults to 0.0.
63
  final double? elevation;
64

65 66
  /// Value for [BottomSheet.backgroundColor] when the Bottom sheet is presented
  /// as a modal bottom sheet.
67
  final Color? modalBackgroundColor;
68

hangyu's avatar
hangyu committed
69
  /// Overrides the default value for barrier color when the Bottom sheet is presented as
70 71 72
  /// a modal bottom sheet.
  final Color? modalBarrierColor;

73 74 75
  /// Overrides the default value for [BottomSheet.shadowColor].
  final Color? shadowColor;

76 77
  /// Value for [BottomSheet.elevation] when the Bottom sheet is presented as a
  /// modal bottom sheet.
78
  final double? modalElevation;
79

hangyu's avatar
hangyu committed
80
  /// Overrides the default value for [BottomSheet.shape].
81 82 83
  ///
  /// If null, no overriding shape is specified for [BottomSheet], so the
  /// [BottomSheet] is rectangular.
84
  final ShapeBorder? shape;
85

86 87 88 89 90 91 92 93 94
  /// Overrides the default value for [BottomSheet.showDragHandle].
  final bool? showDragHandle;

  /// Overrides the default value for [BottomSheet.dragHandleColor].
  final Color? dragHandleColor;

  /// Overrides the default value for [BottomSheet.dragHandleSize].
  final Size? dragHandleSize;

hangyu's avatar
hangyu committed
95
  /// Overrides the default value for [BottomSheet.clipBehavior].
96 97
  ///
  /// If null, [BottomSheet] uses [Clip.none].
98
  final Clip? clipBehavior;
99

100 101 102 103 104
  /// Constrains the size of the [BottomSheet].
  ///
  /// If null, the bottom sheet's size will be unconstrained.
  final BoxConstraints? constraints;

105 106 107
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  BottomSheetThemeData copyWith({
108
    Color? backgroundColor,
109
    Color? surfaceTintColor,
110 111
    double? elevation,
    Color? modalBackgroundColor,
112
    Color? modalBarrierColor,
113
    Color? shadowColor,
114 115
    double? modalElevation,
    ShapeBorder? shape,
116 117 118
    bool? showDragHandle,
    Color? dragHandleColor,
    Size? dragHandleSize,
119
    Clip? clipBehavior,
120
    BoxConstraints? constraints,
121 122 123
  }) {
    return BottomSheetThemeData(
      backgroundColor: backgroundColor ?? this.backgroundColor,
124
      surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
125
      elevation: elevation ?? this.elevation,
126
      modalBackgroundColor: modalBackgroundColor ?? this.modalBackgroundColor,
127
      modalBarrierColor: modalBarrierColor ?? this.modalBarrierColor,
128
      shadowColor: shadowColor ?? this.shadowColor,
129
      modalElevation: modalElevation ?? this.modalElevation,
130
      shape: shape ?? this.shape,
131 132 133
      showDragHandle: showDragHandle ?? this.showDragHandle,
      dragHandleColor: dragHandleColor ?? this.dragHandleColor,
      dragHandleSize: dragHandleSize ?? this.dragHandleSize,
134
      clipBehavior: clipBehavior ?? this.clipBehavior,
135
      constraints: constraints ?? this.constraints,
136 137 138 139 140 141 142 143
    );
  }

  /// Linearly interpolate between two bottom sheet themes.
  ///
  /// If both arguments are null then null is returned.
  ///
  /// {@macro dart.ui.shadow.lerp}
144
  static BottomSheetThemeData? lerp(BottomSheetThemeData? a, BottomSheetThemeData? b, double t) {
145 146
    if (identical(a, b)) {
      return a;
147
    }
148 149
    return BottomSheetThemeData(
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
150
      surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
151
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
152
      modalBackgroundColor: Color.lerp(a?.modalBackgroundColor, b?.modalBackgroundColor, t),
153
      modalBarrierColor: Color.lerp(a?.modalBarrierColor, b?.modalBarrierColor, t),
154
      shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
155
      modalElevation: lerpDouble(a?.modalElevation, b?.modalElevation, t),
156
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
157 158 159
      showDragHandle: t < 0.5 ? a?.showDragHandle : b?.showDragHandle,
      dragHandleColor: Color.lerp(a?.dragHandleColor, b?.dragHandleColor, t),
      dragHandleSize: Size.lerp(a?.dragHandleSize, b?.dragHandleSize, t),
160
      clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
161
      constraints: BoxConstraints.lerp(a?.constraints, b?.constraints, t),
162 163 164 165
    );
  }

  @override
166 167
  int get hashCode => Object.hash(
    backgroundColor,
168
    surfaceTintColor,
169 170
    elevation,
    modalBackgroundColor,
171
    modalBarrierColor,
172
    shadowColor,
173 174
    modalElevation,
    shape,
175 176 177
    showDragHandle,
    dragHandleColor,
    dragHandleSize,
178 179 180
    clipBehavior,
    constraints,
  );
181 182 183

  @override
  bool operator ==(Object other) {
184
    if (identical(this, other)) {
185
      return true;
186 187
    }
    if (other.runtimeType != runtimeType) {
188
      return false;
189
    }
190 191
    return other is BottomSheetThemeData
        && other.backgroundColor == backgroundColor
192
        && other.surfaceTintColor == surfaceTintColor
193 194
        && other.elevation == elevation
        && other.modalBackgroundColor == modalBackgroundColor
195
        && other.shadowColor == shadowColor
196
        && other.modalBarrierColor == modalBarrierColor
197 198
        && other.modalElevation == modalElevation
        && other.shape == shape
199 200 201
        && other.showDragHandle == showDragHandle
        && other.dragHandleColor == dragHandleColor
        && other.dragHandleSize == dragHandleSize
202 203
        && other.clipBehavior == clipBehavior
        && other.constraints == constraints;
204 205 206 207 208
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
209
    properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
210
    properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
211
    properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
212
    properties.add(ColorProperty('modalBackgroundColor', modalBackgroundColor, defaultValue: null));
213
    properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
214
    properties.add(ColorProperty('modalBarrierColor', modalBarrierColor, defaultValue: null));
215
    properties.add(DoubleProperty('modalElevation', modalElevation, defaultValue: null));
216
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
217 218 219
    properties.add(DiagnosticsProperty<bool>('showDragHandle', showDragHandle, defaultValue: null));
    properties.add(ColorProperty('dragHandleColor', dragHandleColor, defaultValue: null));
    properties.add(DiagnosticsProperty<Size>('dragHandleSize', dragHandleSize, defaultValue: null));
220
    properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
221
    properties.add(DiagnosticsProperty<BoxConstraints>('constraints', constraints, defaultValue: null));
222 223
  }
}