icon_theme_data.dart 4.19 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
class IconThemeData with Diagnosticable {
22 23
  /// Creates an icon theme data.
  ///
24 25
  /// The opacity applies to both explicit and default icon colors. The value
  /// is clamped between 0.0 and 1.0.
26
  const IconThemeData({this.color, double opacity, this.size}) : _opacity = opacity;
Ian Hickson's avatar
Ian Hickson committed
27

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

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

  /// 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;
52 53 54 55 56
    return copyWith(
      color: other.color,
      opacity: other.opacity,
      size: other.size,
    );
Ian Hickson's avatar
Ian Hickson committed
57 58
  }

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

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

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

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

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

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

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

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

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