bottom_sheet_theme.dart 6.59 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.modalElevation,
37
    this.shape,
38
    this.clipBehavior,
39
    this.constraints,
40 41
  });

hangyu's avatar
hangyu committed
42
  /// Overrides the default value for [BottomSheet.backgroundColor].
43 44
  ///
  /// If null, [BottomSheet] defaults to [Material]'s default.
45
  final Color? backgroundColor;
46

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

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

61 62
  /// Value for [BottomSheet.backgroundColor] when the Bottom sheet is presented
  /// as a modal bottom sheet.
63
  final Color? modalBackgroundColor;
64

hangyu's avatar
hangyu committed
65
  /// Overrides the default value for barrier color when the Bottom sheet is presented as
66 67 68
  /// a modal bottom sheet.
  final Color? modalBarrierColor;

69 70
  /// Value for [BottomSheet.elevation] when the Bottom sheet is presented as a
  /// modal bottom sheet.
71
  final double? modalElevation;
72

hangyu's avatar
hangyu committed
73
  /// Overrides the default value for [BottomSheet.shape].
74 75 76
  ///
  /// If null, no overriding shape is specified for [BottomSheet], so the
  /// [BottomSheet] is rectangular.
77
  final ShapeBorder? shape;
78

hangyu's avatar
hangyu committed
79
  /// Overrides the default value for [BottomSheet.clipBehavior].
80 81
  ///
  /// If null, [BottomSheet] uses [Clip.none].
82
  final Clip? clipBehavior;
83

84 85 86 87 88
  /// Constrains the size of the [BottomSheet].
  ///
  /// If null, the bottom sheet's size will be unconstrained.
  final BoxConstraints? constraints;

89 90 91
  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  BottomSheetThemeData copyWith({
92
    Color? backgroundColor,
93
    Color? surfaceTintColor,
94 95
    double? elevation,
    Color? modalBackgroundColor,
96
    Color? modalBarrierColor,
97 98 99
    double? modalElevation,
    ShapeBorder? shape,
    Clip? clipBehavior,
100
    BoxConstraints? constraints,
101 102 103
  }) {
    return BottomSheetThemeData(
      backgroundColor: backgroundColor ?? this.backgroundColor,
104
      surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
105
      elevation: elevation ?? this.elevation,
106
      modalBackgroundColor: modalBackgroundColor ?? this.modalBackgroundColor,
107
      modalBarrierColor: modalBarrierColor ?? this.modalBarrierColor,
108
      modalElevation: modalElevation ?? this.modalElevation,
109
      shape: shape ?? this.shape,
110
      clipBehavior: clipBehavior ?? this.clipBehavior,
111
      constraints: constraints ?? this.constraints,
112 113 114 115 116 117 118 119
    );
  }

  /// Linearly interpolate between two bottom sheet themes.
  ///
  /// If both arguments are null then null is returned.
  ///
  /// {@macro dart.ui.shadow.lerp}
120
  static BottomSheetThemeData? lerp(BottomSheetThemeData? a, BottomSheetThemeData? b, double t) {
121
    assert(t != null);
122
    if (a == null && b == null) {
123
      return null;
124
    }
125 126
    return BottomSheetThemeData(
      backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
127
      surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
128
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
129
      modalBackgroundColor: Color.lerp(a?.modalBackgroundColor, b?.modalBackgroundColor, t),
130
      modalBarrierColor: Color.lerp(a?.modalBarrierColor, b?.modalBarrierColor, t),
131
      modalElevation: lerpDouble(a?.modalElevation, b?.modalElevation, t),
132
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
133
      clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
134
      constraints: BoxConstraints.lerp(a?.constraints, b?.constraints, t),
135 136 137 138
    );
  }

  @override
139 140
  int get hashCode => Object.hash(
    backgroundColor,
141
    surfaceTintColor,
142 143
    elevation,
    modalBackgroundColor,
144
    modalBarrierColor,
145 146 147 148 149
    modalElevation,
    shape,
    clipBehavior,
    constraints,
  );
150 151 152

  @override
  bool operator ==(Object other) {
153
    if (identical(this, other)) {
154
      return true;
155 156
    }
    if (other.runtimeType != runtimeType) {
157
      return false;
158
    }
159 160
    return other is BottomSheetThemeData
        && other.backgroundColor == backgroundColor
161
        && other.surfaceTintColor == surfaceTintColor
162 163
        && other.elevation == elevation
        && other.modalBackgroundColor == modalBackgroundColor
164
        && other.modalBarrierColor == modalBarrierColor
165 166
        && other.modalElevation == modalElevation
        && other.shape == shape
167 168
        && other.clipBehavior == clipBehavior
        && other.constraints == constraints;
169 170 171 172 173
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
174
    properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
175
    properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
176
    properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
177
    properties.add(ColorProperty('modalBackgroundColor', modalBackgroundColor, defaultValue: null));
178
    properties.add(ColorProperty('modalBarrierColor', modalBarrierColor, defaultValue: null));
179
    properties.add(DoubleProperty('modalElevation', modalElevation, defaultValue: null));
180
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
181
    properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
182
    properties.add(DiagnosticsProperty<BoxConstraints>('constraints', constraints, defaultValue: null));
183 184
  }
}