| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /// @docImport 'card.dart'; |
| 6 | /// @docImport 'material.dart'; |
| 7 | library; |
| 8 | |
| 9 | import 'dart:ui' show lerpDouble; |
| 10 | |
| 11 | import 'package:flutter/foundation.dart'; |
| 12 | import 'package:flutter/widgets.dart'; |
| 13 | |
| 14 | import 'theme.dart'; |
| 15 | |
| 16 | /// Defines default property values for descendant [Card] widgets. |
| 17 | /// |
| 18 | /// Descendant widgets obtain the current [CardTheme] object using |
| 19 | /// `CardTheme.of(context)`. Instances of [CardTheme] can be |
| 20 | /// customized with [CardTheme.copyWith]. |
| 21 | /// |
| 22 | /// Typically a [CardTheme] is specified as part of the overall [Theme] |
| 23 | /// with [ThemeData.cardTheme]. |
| 24 | /// |
| 25 | /// All [CardTheme] properties are `null` by default. When null, the [Card] |
| 26 | /// will use the values from [ThemeData] if they exist, otherwise it will |
| 27 | /// provide its own defaults. |
| 28 | /// |
| 29 | /// See also: |
| 30 | /// |
| 31 | /// * [ThemeData], which describes the overall theme information for the |
| 32 | /// application. |
| 33 | class CardTheme extends InheritedWidget with Diagnosticable { |
| 34 | /// Creates a theme that can be used for [ThemeData.cardTheme]. |
| 35 | /// |
| 36 | /// The [elevation] must be null or non-negative. |
| 37 | const CardTheme({ |
| 38 | super.key, |
| 39 | Clip? clipBehavior, |
| 40 | Color? color, |
| 41 | Color? surfaceTintColor, |
| 42 | Color? shadowColor, |
| 43 | double? elevation, |
| 44 | EdgeInsetsGeometry? margin, |
| 45 | ShapeBorder? shape, |
| 46 | CardThemeData? data, |
| 47 | Widget? child, |
| 48 | }) : assert( |
| 49 | data == null || |
| 50 | (clipBehavior ?? |
| 51 | color ?? |
| 52 | surfaceTintColor ?? |
| 53 | shadowColor ?? |
| 54 | elevation ?? |
| 55 | margin ?? |
| 56 | shape) == |
| 57 | null, |
| 58 | ), |
| 59 | assert(elevation == null || elevation >= 0.0), |
| 60 | _data = data, |
| 61 | _clipBehavior = clipBehavior, |
| 62 | _color = color, |
| 63 | _surfaceTintColor = surfaceTintColor, |
| 64 | _shadowColor = shadowColor, |
| 65 | _elevation = elevation, |
| 66 | _margin = margin, |
| 67 | _shape = shape, |
| 68 | super(child: child ?? const SizedBox()); |
| 69 | |
| 70 | final CardThemeData? _data; |
| 71 | final Clip? _clipBehavior; |
| 72 | final Color? _color; |
| 73 | final Color? _surfaceTintColor; |
| 74 | final Color? _shadowColor; |
| 75 | final double? _elevation; |
| 76 | final EdgeInsetsGeometry? _margin; |
| 77 | final ShapeBorder? _shape; |
| 78 | |
| 79 | /// Overrides the default value for [Card.clipBehavior]. |
| 80 | /// |
| 81 | /// This property is obsolete and will be deprecated in a future release: |
| 82 | /// please use the [CardThemeData.clipBehavior] property in [data] instead. |
| 83 | Clip? get clipBehavior => _data != null ? _data.clipBehavior : _clipBehavior; |
| 84 | |
| 85 | /// Overrides the default value for [Card.color]. |
| 86 | /// |
| 87 | /// This property is obsolete and will be deprecated in a future release: |
| 88 | /// please use the [CardThemeData.color] property in [data] instead. |
| 89 | Color? get color => _data != null ? _data.color : _color; |
| 90 | |
| 91 | /// Overrides the default value for [Card.surfaceTintColor]. |
| 92 | /// |
| 93 | /// This property is obsolete and will be deprecated in a future release: |
| 94 | /// please use the [CardThemeData.surfaceTintColor] property in [data] instead. |
| 95 | Color? get surfaceTintColor => _data != null ? _data.surfaceTintColor : _surfaceTintColor; |
| 96 | |
| 97 | /// Overrides the default value for [Card.shadowColor]. |
| 98 | /// |
| 99 | /// This property is obsolete and will be deprecated in a future release: |
| 100 | /// please use the [CardThemeData.shadowColor] property in [data] instead. |
| 101 | Color? get shadowColor => _data != null ? _data.shadowColor : _shadowColor; |
| 102 | |
| 103 | /// Overrides the default value for [Card.elevation]. |
| 104 | /// |
| 105 | /// This property is obsolete and will be deprecated in a future release: |
| 106 | /// please use the [CardThemeData.elevation] property in [data] instead. |
| 107 | double? get elevation => _data != null ? _data.elevation : _elevation; |
| 108 | |
| 109 | /// Overrides the default value for [Card.margin]. |
| 110 | /// |
| 111 | /// This property is obsolete and will be deprecated in a future release: |
| 112 | /// please use the [CardThemeData.margin] property in [data] instead. |
| 113 | EdgeInsetsGeometry? get margin => _data != null ? _data.margin : _margin; |
| 114 | |
| 115 | /// Overrides the default value for [Card.shape]. |
| 116 | /// |
| 117 | /// This property is obsolete and will be deprecated in a future release: |
| 118 | /// please use the [CardThemeData.shape] property in [data] instead. |
| 119 | ShapeBorder? get shape => _data != null ? _data.shape : _shape; |
| 120 | |
| 121 | /// The properties used for all descendant [Card] widgets. |
| 122 | CardThemeData get data { |
| 123 | return _data ?? |
| 124 | CardThemeData( |
| 125 | clipBehavior: _clipBehavior, |
| 126 | color: _color, |
| 127 | surfaceTintColor: _surfaceTintColor, |
| 128 | shadowColor: _shadowColor, |
| 129 | elevation: _elevation, |
| 130 | margin: _margin, |
| 131 | shape: _shape, |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | /// Creates a copy of this object with the given fields replaced with the |
| 136 | /// new values. |
| 137 | /// |
| 138 | /// This method is obsolete and will be deprecated in a future release: |
| 139 | /// please use the [CardThemeData.copyWith] instead. |
| 140 | CardTheme copyWith({ |
| 141 | Clip? clipBehavior, |
| 142 | Color? color, |
| 143 | Color? shadowColor, |
| 144 | Color? surfaceTintColor, |
| 145 | double? elevation, |
| 146 | EdgeInsetsGeometry? margin, |
| 147 | ShapeBorder? shape, |
| 148 | }) { |
| 149 | return CardTheme( |
| 150 | clipBehavior: clipBehavior ?? this.clipBehavior, |
| 151 | color: color ?? this.color, |
| 152 | shadowColor: shadowColor ?? this.shadowColor, |
| 153 | surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor, |
| 154 | elevation: elevation ?? this.elevation, |
| 155 | margin: margin ?? this.margin, |
| 156 | shape: shape ?? this.shape, |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | /// The [ThemeData.cardTheme] property of the ambient [Theme]. |
| 161 | static CardThemeData of(BuildContext context) { |
| 162 | final CardTheme? cardTheme = context.dependOnInheritedWidgetOfExactType<CardTheme>(); |
| 163 | return cardTheme?.data ?? Theme.of(context).cardTheme; |
| 164 | } |
| 165 | |
| 166 | @override |
| 167 | bool updateShouldNotify(CardTheme oldWidget) => data != oldWidget.data; |
| 168 | |
| 169 | /// Linearly interpolate between two Card themes. |
| 170 | /// |
| 171 | /// {@macro dart.ui.shadow.lerp} |
| 172 | /// |
| 173 | /// This method is obsolete and will be deprecated in a future release: |
| 174 | /// please use the [CardThemeData.lerp] instead. |
| 175 | static CardTheme lerp(CardTheme? a, CardTheme? b, double t) { |
| 176 | if (identical(a, b) && a != null) { |
| 177 | return a; |
| 178 | } |
| 179 | return CardTheme( |
| 180 | clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior, |
| 181 | color: Color.lerp(a?.color, b?.color, t), |
| 182 | shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t), |
| 183 | surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t), |
| 184 | elevation: lerpDouble(a?.elevation, b?.elevation, t), |
| 185 | margin: EdgeInsetsGeometry.lerp(a?.margin, b?.margin, t), |
| 186 | shape: ShapeBorder.lerp(a?.shape, b?.shape, t), |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | @override |
| 191 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 192 | super.debugFillProperties(properties); |
| 193 | properties.add(DiagnosticsProperty<Clip>('clipBehavior' , clipBehavior, defaultValue: null)); |
| 194 | properties.add(ColorProperty('color' , color, defaultValue: null)); |
| 195 | properties.add(ColorProperty('shadowColor' , shadowColor, defaultValue: null)); |
| 196 | properties.add(ColorProperty('surfaceTintColor' , surfaceTintColor, defaultValue: null)); |
| 197 | properties.add(DiagnosticsProperty<double>('elevation' , elevation, defaultValue: null)); |
| 198 | properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('margin' , margin, defaultValue: null)); |
| 199 | properties.add(DiagnosticsProperty<ShapeBorder>('shape' , shape, defaultValue: null)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /// Defines default property values for descendant [Card] widgets. |
| 204 | /// |
| 205 | /// Descendant widgets obtain the current [CardThemeData] object using |
| 206 | /// `CardTheme.of(context)`. Instances of [CardThemeData] can be |
| 207 | /// customized with [CardThemeData.copyWith]. |
| 208 | /// |
| 209 | /// Typically a [CardThemeData] is specified as part of the overall [Theme] |
| 210 | /// with [ThemeData.cardTheme]. |
| 211 | /// |
| 212 | /// All [CardThemeData] properties are `null` by default. When null, the [Card] |
| 213 | /// will use the values from [ThemeData] if they exist, otherwise it will |
| 214 | /// provide its own defaults. See the individual [Card] properties for details. |
| 215 | /// |
| 216 | /// See also: |
| 217 | /// |
| 218 | /// * [ThemeData], which describes the overall theme information for the |
| 219 | /// application. |
| 220 | @immutable |
| 221 | class CardThemeData with Diagnosticable { |
| 222 | /// Creates a theme that can be used for [ThemeData.cardTheme]. |
| 223 | /// |
| 224 | /// The [elevation] must be null or non-negative. |
| 225 | const CardThemeData({ |
| 226 | this.clipBehavior, |
| 227 | this.color, |
| 228 | this.shadowColor, |
| 229 | this.surfaceTintColor, |
| 230 | this.elevation, |
| 231 | this.margin, |
| 232 | this.shape, |
| 233 | }) : assert(elevation == null || elevation >= 0.0); |
| 234 | |
| 235 | /// Overrides the default value for [Card.clipBehavior]. |
| 236 | final Clip? clipBehavior; |
| 237 | |
| 238 | /// Overrides the default value for [Card.color]. |
| 239 | final Color? color; |
| 240 | |
| 241 | /// Overrides the default value for [Card.shadowColor]. |
| 242 | final Color? shadowColor; |
| 243 | |
| 244 | /// Overrides the default value for [Card.surfaceTintColor]. |
| 245 | final Color? surfaceTintColor; |
| 246 | |
| 247 | /// Overrides the default value for [Card.elevation]. |
| 248 | final double? elevation; |
| 249 | |
| 250 | /// Overrides the default value for [Card.margin]. |
| 251 | final EdgeInsetsGeometry? margin; |
| 252 | |
| 253 | /// Overrides the default value for [Card.shape]. |
| 254 | final ShapeBorder? shape; |
| 255 | |
| 256 | /// Creates a copy of this object with the given fields replaced with the |
| 257 | /// new values. |
| 258 | CardThemeData copyWith({ |
| 259 | Clip? clipBehavior, |
| 260 | Color? color, |
| 261 | Color? shadowColor, |
| 262 | Color? surfaceTintColor, |
| 263 | double? elevation, |
| 264 | EdgeInsetsGeometry? margin, |
| 265 | ShapeBorder? shape, |
| 266 | }) { |
| 267 | return CardThemeData( |
| 268 | clipBehavior: clipBehavior ?? this.clipBehavior, |
| 269 | color: color ?? this.color, |
| 270 | shadowColor: shadowColor ?? this.shadowColor, |
| 271 | surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor, |
| 272 | elevation: elevation ?? this.elevation, |
| 273 | margin: margin ?? this.margin, |
| 274 | shape: shape ?? this.shape, |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | /// Linearly interpolate between two Card themes. |
| 279 | /// |
| 280 | /// {@macro dart.ui.shadow.lerp} |
| 281 | static CardThemeData lerp(CardThemeData? a, CardThemeData? b, double t) { |
| 282 | if (identical(a, b) && a != null) { |
| 283 | return a; |
| 284 | } |
| 285 | return CardThemeData( |
| 286 | clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior, |
| 287 | color: Color.lerp(a?.color, b?.color, t), |
| 288 | shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t), |
| 289 | surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t), |
| 290 | elevation: lerpDouble(a?.elevation, b?.elevation, t), |
| 291 | margin: EdgeInsetsGeometry.lerp(a?.margin, b?.margin, t), |
| 292 | shape: ShapeBorder.lerp(a?.shape, b?.shape, t), |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | @override |
| 297 | int get hashCode => |
| 298 | Object.hash(clipBehavior, color, shadowColor, surfaceTintColor, elevation, margin, shape); |
| 299 | |
| 300 | @override |
| 301 | bool operator ==(Object other) { |
| 302 | if (identical(this, other)) { |
| 303 | return true; |
| 304 | } |
| 305 | if (other.runtimeType != runtimeType) { |
| 306 | return false; |
| 307 | } |
| 308 | return other is CardThemeData && |
| 309 | other.clipBehavior == clipBehavior && |
| 310 | other.color == color && |
| 311 | other.shadowColor == shadowColor && |
| 312 | other.surfaceTintColor == surfaceTintColor && |
| 313 | other.elevation == elevation && |
| 314 | other.margin == margin && |
| 315 | other.shape == shape; |
| 316 | } |
| 317 | |
| 318 | @override |
| 319 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
| 320 | super.debugFillProperties(properties); |
| 321 | properties.add(DiagnosticsProperty<Clip>('clipBehavior' , clipBehavior, defaultValue: null)); |
| 322 | properties.add(ColorProperty('color' , color, defaultValue: null)); |
| 323 | properties.add(ColorProperty('shadowColor' , shadowColor, defaultValue: null)); |
| 324 | properties.add(ColorProperty('surfaceTintColor' , surfaceTintColor, defaultValue: null)); |
| 325 | properties.add(DiagnosticsProperty<double>('elevation' , elevation, defaultValue: null)); |
| 326 | properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('margin' , margin, defaultValue: null)); |
| 327 | properties.add(DiagnosticsProperty<ShapeBorder>('shape' , shape, defaultValue: null)); |
| 328 | } |
| 329 | } |
| 330 | |