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

5
import 'dart:ui' as ui show lerpDouble;
Adam Barth's avatar
Adam Barth committed
6
import 'dart:ui' show Color, hashValues;
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 14
///
/// To obtain the current icon theme, use [IconTheme.of]. To convert an icon
/// theme to a version with all the fields filled in, use [fallback].
Adam Barth's avatar
Adam Barth committed
15
class IconThemeData {
16 17
  /// Creates an icon theme data.
  ///
18 19
  /// 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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  const IconThemeData({ this.color, double opacity, this.size }) : _opacity = opacity;

  /// 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
    );
  }

  /// Creates an icon theme that is identical to this icon theme but with
  /// any null fields filled in. Specific fallbacks can be given, but in their
  /// absence, this method defaults to black, fully opaque, and size 24.0.
  IconThemeData fallback({
    Color color: const Color(0xFF000000),
    double opacity: 1.0,
    double size: 24.0
  }) {
    if (this.color != null && this.opacity != null && this.size != null)
      return this;
    return new IconThemeData(
      color: this.color ?? color,
      opacity: this.opacity ?? opacity,
      size: this.size ?? size
    );
  }
61

Adam Barth's avatar
Adam Barth committed
62 63 64 65
  /// 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
66
  double get opacity => _opacity?.clamp(0.0, 1.0);
67
  final double _opacity;
Adam Barth's avatar
Adam Barth committed
68

69 70 71
  /// The default size for icons.
  final double size;

72
  /// Linearly interpolate between two icon theme data objects.
73 74
  static IconThemeData lerp(IconThemeData begin, IconThemeData end, double t) {
    return new IconThemeData(
Adam Barth's avatar
Adam Barth committed
75
      color: Color.lerp(begin.color, end.color, t),
76 77
      opacity: ui.lerpDouble(begin.opacity, end.opacity, t),
      size: ui.lerpDouble(begin.size, end.size, t)
78 79 80
    );
  }

81
  @override
Adam Barth's avatar
Adam Barth committed
82
  bool operator ==(dynamic other) {
83
    if (other.runtimeType != runtimeType)
Adam Barth's avatar
Adam Barth committed
84 85
      return false;
    final IconThemeData typedOther = other;
86 87 88
    return color == typedOther.color
        && opacity == typedOther.opacity
        && size == typedOther.size;
Adam Barth's avatar
Adam Barth committed
89 90
  }

91
  @override
92
  int get hashCode => hashValues(color, opacity, size);
Adam Barth's avatar
Adam Barth committed
93

94
  @override
95 96 97 98
  String toString() {
    List<String> result = <String>[];
    if (color != null)
      result.add('color: $color');
Ian Hickson's avatar
Ian Hickson committed
99 100
    if (_opacity != null)
      result.add('opacity: $_opacity');
101 102 103 104 105 106
    if (size != null)
      result.add('size: $size');
    if (result.length == 0)
      return '<no theme>';
    return result.join(', ');
  }
Adam Barth's avatar
Adam Barth committed
107
}