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