icon_theme_data.dart 4.21 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Adam Barth's avatar
Adam Barth committed
5
import 'dart:ui' show Color, hashValues;
6
import 'dart:ui' as ui show lerpDouble;
Adam Barth's avatar
Adam Barth committed
7

8
import 'package:flutter/foundation.dart';
9
import 'package:flutter/painting.dart';
10

11 12
import 'framework.dart' show BuildContext;

13
/// Defines the color, opacity, and size of icons.
14
///
15 16
/// Used by [IconTheme] to control the color, opacity, and size of icons in a
/// widget subtree.
Ian Hickson's avatar
Ian Hickson committed
17 18
///
/// To obtain the current icon theme, use [IconTheme.of]. To convert an icon
Ian Hickson's avatar
Ian Hickson committed
19 20
/// theme to a version with all the fields filled in, use [new
/// IconThemeData.fallback].
21
@immutable
22
class IconThemeData with Diagnosticable {
23 24
  /// Creates an icon theme data.
  ///
25 26
  /// The opacity applies to both explicit and default icon colors. The value
  /// is clamped between 0.0 and 1.0.
27
  const IconThemeData({this.color, double? opacity, this.size}) : _opacity = opacity;
Ian Hickson's avatar
Ian Hickson committed
28

29 30 31 32
  /// Creates an icon them with some reasonable default values.
  ///
  /// The [color] is black, the [opacity] is 1.0, and the [size] is 24.0.
  const IconThemeData.fallback()
33 34 35
    : color = const Color(0xFF000000),
      _opacity = 1.0,
      size = 24.0;
36

Ian Hickson's avatar
Ian Hickson committed
37 38
  /// Creates a copy of this icon theme but with the given fields replaced with
  /// the new values.
39
  IconThemeData copyWith({ Color? color, double? opacity, double? size }) {
40
    return IconThemeData(
41 42 43 44
      color: color ?? this.color,
      opacity: opacity ?? this.opacity,
      size: size ?? this.size,
    );
Ian Hickson's avatar
Ian Hickson committed
45 46 47 48 49
  }

  /// Returns a new icon theme that matches this icon theme but with some values
  /// replaced by the non-null parameters of the given icon theme. If the given
  /// icon theme is null, simply returns this icon theme.
50
  IconThemeData merge(IconThemeData? other) {
Ian Hickson's avatar
Ian Hickson committed
51 52
    if (other == null)
      return this;
53 54 55 56 57
    return copyWith(
      color: other.color,
      opacity: other.opacity,
      size: other.size,
    );
Ian Hickson's avatar
Ian Hickson committed
58 59
  }

60 61 62 63 64 65 66 67 68 69 70 71 72 73
  /// Called by [IconTheme.of] to convert this instance to an [IconThemeData]
  /// that fits the given [BuildContext].
  ///
  /// This method gives the ambient [IconThemeData] a chance to update itself,
  /// after it's been retrieved by [IconTheme.of], and before being returned as
  /// the final result. For instance, [CupertinoIconThemeData] overrides this method
  /// to resolve [color], in case [color] is a [CupertinoDynamicColor] and needs
  /// to be resolved against the given [BuildContext] before it can be used as a
  /// regular [Color].
  ///
  /// The default implementation returns this [IconThemeData] as-is.
  ///
  /// See also:
  ///
74
  ///  * [CupertinoIconThemeData.resolve] an implementation that resolves
75
  ///    the color of [CupertinoIconThemeData] before returning.
76 77
  IconThemeData resolve(BuildContext context) => this;

78 79
  /// Whether all the properties of this object are non-null.
  bool get isConcrete => color != null && opacity != null && size != null;
80

Adam Barth's avatar
Adam Barth committed
81
  /// The default color for icons.
82
  final Color? color;
Adam Barth's avatar
Adam Barth committed
83 84

  /// An opacity to apply to both explicit and default icon colors.
85 86
  double? get opacity => _opacity?.clamp(0.0, 1.0);
  final double? _opacity;
Adam Barth's avatar
Adam Barth committed
87

88
  /// The default size for icons.
89
  final double? size;
90

91
  /// Linearly interpolate between two icon theme data objects.
92
  ///
93
  /// {@macro dart.ui.shadow.lerp}
94
  static IconThemeData lerp(IconThemeData? a, IconThemeData? b, double t) {
95
    assert(t != null);
96
    return IconThemeData(
97 98 99
      color: Color.lerp(a?.color, b?.color, t),
      opacity: ui.lerpDouble(a?.opacity, b?.opacity, t),
      size: ui.lerpDouble(a?.size, b?.size, t),
100 101 102
    );
  }

103
  @override
104
  bool operator ==(Object other) {
105
    if (other.runtimeType != runtimeType)
Adam Barth's avatar
Adam Barth committed
106
      return false;
107 108 109 110
    return other is IconThemeData
        && other.color == color
        && other.opacity == opacity
        && other.size == size;
Adam Barth's avatar
Adam Barth committed
111 112
  }

113
  @override
114
  int get hashCode => hashValues(color, opacity, size);
Adam Barth's avatar
Adam Barth committed
115

116
  @override
117 118
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
119
    properties.add(ColorProperty('color', color, defaultValue: null));
120 121
    properties.add(DoubleProperty('opacity', opacity, defaultValue: null));
    properties.add(DoubleProperty('size', size, defaultValue: null));
122
  }
Adam Barth's avatar
Adam Barth committed
123
}