icon_theme_data.dart 4.22 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.

5 6
// @dart = 2.8

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

10
import 'package:flutter/foundation.dart';
11
import 'package:flutter/painting.dart';
12

13 14
import 'framework.dart' show BuildContext;

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

31 32 33 34
  /// 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()
35 36 37
    : color = const Color(0xFF000000),
      _opacity = 1.0,
      size = 24.0;
38

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

  /// 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.
  IconThemeData merge(IconThemeData other) {
    if (other == null)
      return this;
55 56 57 58 59
    return copyWith(
      color: other.color,
      opacity: other.opacity,
      size: other.size,
    );
Ian Hickson's avatar
Ian Hickson committed
60 61
  }

62 63 64 65 66 67 68 69 70 71 72 73 74 75
  /// 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:
  ///
76
  ///  * [CupertinoIconThemeData.resolve] an implementation that resolves
77
  ///    the color of [CupertinoIconThemeData] before returning.
78 79
  IconThemeData resolve(BuildContext context) => this;

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

Adam Barth's avatar
Adam Barth committed
83 84 85 86
  /// The default color for icons.
  final Color color;

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

90 91 92
  /// The default size for icons.
  final double size;

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

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

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

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