icon_theme_data.dart 3.34 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
import 'package:flutter/foundation.dart';
9
import 'package:flutter/painting.dart';
10

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

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

Ian Hickson's avatar
Ian Hickson committed
34 35
  /// Creates a copy of this icon theme but with the given fields replaced with
  /// the new values.
36
  IconThemeData copyWith({ Color color, double opacity, double size }) {
37
    return IconThemeData(
38 39 40 41
      color: color ?? this.color,
      opacity: opacity ?? this.opacity,
      size: size ?? this.size,
    );
Ian Hickson's avatar
Ian Hickson committed
42 43 44 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.
  IconThemeData merge(IconThemeData other) {
    if (other == null)
      return this;
50 51 52 53 54
    return copyWith(
      color: other.color,
      opacity: other.opacity,
      size: other.size,
    );
Ian Hickson's avatar
Ian Hickson committed
55 56
  }

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

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

67 68 69
  /// The default size for icons.
  final double size;

70
  /// Linearly interpolate between two icon theme data objects.
71
  ///
72
  /// {@macro dart.ui.shadow.lerp}
73 74
  static IconThemeData lerp(IconThemeData a, IconThemeData b, double t) {
    assert(t != null);
75
    return IconThemeData(
76 77 78
      color: Color.lerp(a?.color, b?.color, t),
      opacity: ui.lerpDouble(a?.opacity, b?.opacity, t),
      size: ui.lerpDouble(a?.size, b?.size, t),
79 80 81
    );
  }

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

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

95
  @override
96 97
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
98
    properties.add(ColorProperty('color', color, defaultValue: null));
99 100
    properties.add(DoubleProperty('opacity', opacity, defaultValue: null));
    properties.add(DoubleProperty('size', size, defaultValue: null));
101
  }
Adam Barth's avatar
Adam Barth committed
102
}