icon_theme_data.dart 3.9 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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
/// Defines the color, opacity, and size of icons.
9
///
10 11
/// Used by [IconTheme] to control the color, opacity, and size of icons in a
/// widget subtree.
Ian Hickson's avatar
Ian Hickson committed
12 13
///
/// To obtain the current icon theme, use [IconTheme.of]. To convert an icon
Ian Hickson's avatar
Ian Hickson committed
14 15
/// theme to a version with all the fields filled in, use [new
/// IconThemeData.fallback].
Adam Barth's avatar
Adam Barth committed
16
class IconThemeData {
17 18
  /// Creates an icon theme data.
  ///
19 20
  /// The opacity applies to both explicit and default icon colors. The value
  /// is clamped between 0.0 and 1.0.
Ian Hickson's avatar
Ian Hickson committed
21 22
  const IconThemeData({ this.color, double opacity, this.size }) : _opacity = opacity;

23 24 25 26 27 28 29 30
  /// 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()
    : color = const Color(0xFF000000),
      _opacity = 1.0,
      size = 24.0;

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

  /// 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;
    return copyWith(
      color: other.color,
      opacity: other.opacity,
      size: other.size
    );
  }

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

Adam Barth's avatar
Adam Barth committed
57 58 59 60
  /// The default color for icons.
  final Color color;

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

64 65 66
  /// The default size for icons.
  final double size;

67
  /// Linearly interpolate between two icon theme data objects.
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  ///
  /// The `t` argument represents position on the timeline, with 0.0 meaning
  /// that the interpolation has not started, returning `a` (or something
  /// equivalent to `a`), 1.0 meaning that the interpolation has finished,
  /// returning `b` (or something equivalent to `b`), and values in between
  /// meaning that the interpolation is at the relevant point on the timeline
  /// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
  /// 1.0, so negative values and values greater than 1.0 are valid (and can
  /// easily be generated by curves such as [Curves.elasticInOut]).
  ///
  /// Values for `t` are usually obtained from an [Animation<double>], such as
  /// an [AnimationController].
  static IconThemeData lerp(IconThemeData a, IconThemeData b, double t) {
    assert(t != null);
82
    return new IconThemeData(
83 84 85
      color: Color.lerp(a.color, b.color, t),
      opacity: ui.lerpDouble(a.opacity, b.opacity, t),
      size: ui.lerpDouble(a.size, b.size, t),
86 87 88
    );
  }

89
  @override
Adam Barth's avatar
Adam Barth committed
90
  bool operator ==(dynamic other) {
91
    if (other.runtimeType != runtimeType)
Adam Barth's avatar
Adam Barth committed
92 93
      return false;
    final IconThemeData typedOther = other;
94 95 96
    return color == typedOther.color
        && opacity == typedOther.opacity
        && size == typedOther.size;
Adam Barth's avatar
Adam Barth committed
97 98
  }

99
  @override
100
  int get hashCode => hashValues(color, opacity, size);
Adam Barth's avatar
Adam Barth committed
101

102
  @override
103
  String toString() {
104
    final List<String> result = <String>[];
105 106
    if (color != null)
      result.add('color: $color');
Ian Hickson's avatar
Ian Hickson committed
107 108
    if (_opacity != null)
      result.add('opacity: $_opacity');
109 110
    if (size != null)
      result.add('size: $size');
111
    if (result.isEmpty)
112 113 114
      return '<no theme>';
    return result.join(', ');
  }
Adam Barth's avatar
Adam Barth committed
115
}