card_theme.dart 5.3 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 27 28
// 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/widgets.dart';

import 'theme.dart';

/// Defines default property values for descendant [Card] widgets.
///
/// Descendant widgets obtain the current [CardTheme] object using
/// `CardTheme.of(context)`. Instances of [CardTheme] can be
/// customized with [CardTheme.copyWith].
///
/// Typically a [CardTheme] is specified as part of the overall [Theme]
/// with [ThemeData.cardTheme].
///
/// All [CardTheme] properties are `null` by default. When null, the [Card]
/// 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.
29
@immutable
30
class CardTheme with Diagnosticable {
31 32 33 34 35 36 37

  /// Creates a theme that can be used for [ThemeData.cardTheme].
  ///
  /// The [elevation] must be null or non-negative.
  const CardTheme({
    this.clipBehavior,
    this.color,
38
    this.shadowColor,
39
    this.surfaceTintColor,
40 41 42 43 44
    this.elevation,
    this.margin,
    this.shape,
  }) : assert(elevation == null || elevation >= 0.0);

hangyu's avatar
hangyu committed
45
  /// Overrides the default value for [Card.clipBehavior].
46 47
  ///
  /// If null, [Card] uses [Clip.none].
48
  final Clip? clipBehavior;
49

hangyu's avatar
hangyu committed
50
  /// Overrides the default value for [Card.color].
51 52
  ///
  /// If null, [Card] uses [ThemeData.cardColor].
53
  final Color? color;
54

hangyu's avatar
hangyu committed
55
  /// Overrides the default value for [Card.shadowColor].
56 57
  ///
  /// If null, [Card] defaults to fully opaque black.
58
  final Color? shadowColor;
59

hangyu's avatar
hangyu committed
60
  /// Overrides the default value for [Card.surfaceTintColor].
61 62 63 64 65 66
  ///
  /// If null, [Card] will not display an overlay color.
  ///
  /// See [Material.surfaceTintColor] for more details.
  final Color? surfaceTintColor;

hangyu's avatar
hangyu committed
67
  /// Overrides the default value for [Card.elevation].
68 69
  ///
  /// If null, [Card] uses a default of 1.0.
70
  final double? elevation;
71

hangyu's avatar
hangyu committed
72
  /// Overrides the default value for [Card.margin].
73 74 75
  ///
  /// If null, [Card] uses a default margin of 4.0 logical pixels on all sides:
  /// `EdgeInsets.all(4.0)`.
76
  final EdgeInsetsGeometry? margin;
77

hangyu's avatar
hangyu committed
78
  /// Overrides the default value for [Card.shape].
79 80 81
  ///
  /// If null, [Card] then uses a [RoundedRectangleBorder] with a circular
  /// corner radius of 4.0.
82
  final ShapeBorder? shape;
83 84 85 86

  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  CardTheme copyWith({
87 88 89
    Clip? clipBehavior,
    Color? color,
    Color? shadowColor,
90
    Color? surfaceTintColor,
91 92 93
    double? elevation,
    EdgeInsetsGeometry? margin,
    ShapeBorder? shape,
94 95 96 97
  }) {
    return CardTheme(
      clipBehavior: clipBehavior ?? this.clipBehavior,
      color: color ?? this.color,
98
      shadowColor: shadowColor ?? this.shadowColor,
99
      surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
100 101 102 103 104 105 106 107
      elevation: elevation ?? this.elevation,
      margin: margin ?? this.margin,
      shape: shape ?? this.shape,
    );
  }

  /// The [ThemeData.cardTheme] property of the ambient [Theme].
  static CardTheme of(BuildContext context) {
108
    return Theme.of(context).cardTheme;
109 110 111 112 113 114 115
  }

  /// Linearly interpolate between two Card themes.
  ///
  /// The argument `t` must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
116
  static CardTheme lerp(CardTheme? a, CardTheme? b, double t) {
117 118 119 120
    assert(t != null);
    return CardTheme(
      clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
      color: Color.lerp(a?.color, b?.color, t),
121
      shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
122
      surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
123 124 125 126 127 128 129
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
      margin: EdgeInsetsGeometry.lerp(a?.margin, b?.margin, t),
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
    );
  }

  @override
130 131 132 133
  int get hashCode => Object.hash(
    clipBehavior,
    color,
    shadowColor,
134
    surfaceTintColor,
135 136 137 138
    elevation,
    margin,
    shape,
  );
139 140

  @override
141
  bool operator ==(Object other) {
142
    if (identical(this, other)) {
143
      return true;
144 145
    }
    if (other.runtimeType != runtimeType) {
146
      return false;
147
    }
148 149 150
    return other is CardTheme
        && other.clipBehavior == clipBehavior
        && other.color == color
151
        && other.shadowColor == shadowColor
152
        && other.surfaceTintColor == surfaceTintColor
153 154 155
        && other.elevation == elevation
        && other.margin == margin
        && other.shape == shape;
156 157 158 159 160 161
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
162
    properties.add(ColorProperty('color', color, defaultValue: null));
163
    properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
164
    properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
165 166 167 168 169
    properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('margin', margin, defaultValue: null));
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
  }
}