card_theme.dart 4.65 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 40 41 42 43 44 45 46
    this.elevation,
    this.margin,
    this.shape,
  }) : assert(elevation == null || elevation >= 0.0);

  /// Default value for [Card.clipBehavior].
  ///
  /// If null, [Card] uses [Clip.none].
47
  final Clip? clipBehavior;
48 49 50 51

  /// Default value for [Card.color].
  ///
  /// If null, [Card] uses [ThemeData.cardColor].
52
  final Color? color;
53

54 55 56
  /// Default value for [Card.shadowColor].
  ///
  /// If null, [Card] defaults to fully opaque black.
57
  final Color? shadowColor;
58

59 60 61
  /// Default value for [Card.elevation].
  ///
  /// If null, [Card] uses a default of 1.0.
62
  final double? elevation;
63 64 65 66 67

  /// Default value for [Card.margin].
  ///
  /// If null, [Card] uses a default margin of 4.0 logical pixels on all sides:
  /// `EdgeInsets.all(4.0)`.
68
  final EdgeInsetsGeometry? margin;
69 70 71 72 73

  /// Default value for [Card.shape].
  ///
  /// If null, [Card] then uses a [RoundedRectangleBorder] with a circular
  /// corner radius of 4.0.
74
  final ShapeBorder? shape;
75 76 77 78

  /// Creates a copy of this object with the given fields replaced with the
  /// new values.
  CardTheme copyWith({
79 80 81 82 83 84
    Clip? clipBehavior,
    Color? color,
    Color? shadowColor,
    double? elevation,
    EdgeInsetsGeometry? margin,
    ShapeBorder? shape,
85 86 87 88
  }) {
    return CardTheme(
      clipBehavior: clipBehavior ?? this.clipBehavior,
      color: color ?? this.color,
89
      shadowColor: shadowColor ?? this.shadowColor,
90 91 92 93 94 95 96 97
      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) {
98
    return Theme.of(context).cardTheme;
99 100 101 102 103 104 105
  }

  /// Linearly interpolate between two Card themes.
  ///
  /// The argument `t` must not be null.
  ///
  /// {@macro dart.ui.shadow.lerp}
106
  static CardTheme lerp(CardTheme? a, CardTheme? b, double t) {
107 108 109 110
    assert(t != null);
    return CardTheme(
      clipBehavior: t < 0.5 ? a?.clipBehavior : b?.clipBehavior,
      color: Color.lerp(a?.color, b?.color, t),
111
      shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
112 113 114 115 116 117 118 119 120 121 122
      elevation: lerpDouble(a?.elevation, b?.elevation, t),
      margin: EdgeInsetsGeometry.lerp(a?.margin, b?.margin, t),
      shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
    );
  }

  @override
  int get hashCode {
    return hashValues(
      clipBehavior,
      color,
123
      shadowColor,
124 125 126 127 128 129 130
      elevation,
      margin,
      shape,
    );
  }

  @override
131
  bool operator ==(Object other) {
132 133 134 135
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
      return false;
136 137 138
    return other is CardTheme
        && other.clipBehavior == clipBehavior
        && other.color == color
139
        && other.shadowColor == shadowColor
140 141 142
        && other.elevation == elevation
        && other.margin == margin
        && other.shape == shape;
143 144 145 146 147 148
  }

  @override
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(DiagnosticsProperty<Clip>('clipBehavior', clipBehavior, defaultValue: null));
149
    properties.add(ColorProperty('color', color, defaultValue: null));
150
    properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
151 152 153 154 155
    properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
    properties.add(DiagnosticsProperty<EdgeInsetsGeometry>('margin', margin, defaultValue: null));
    properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
  }
}