icon_data.dart 4.74 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6 7 8 9 10
import 'package:flutter/foundation.dart';

/// A description of an icon fulfilled by a font glyph.
///
/// See [Icons] for a number of predefined icons available for material
/// design applications.
11 12 13 14
///
/// In release builds, the Flutter tool will tree shake out of bundled fonts
/// the code points (or instances of [IconData]) which are not referenced from
/// Dart app code. See the [staticIconProvider] annotation for more details.
15 16 17 18 19 20
@immutable
class IconData {
  /// Creates icon data.
  ///
  /// Rarely used directly. Instead, consider using one of the predefined icons
  /// like the [Icons] collection.
21 22 23
  ///
  /// The [fontPackage] argument must be non-null when using a font family that
  /// is included in a package. This is used when selecting the font.
24 25 26 27 28
  ///
  /// Instantiating non-const instances of this class in your app will
  /// mean the app cannot be built in release mode with icon tree-shaking (it
  /// need to be explicitly opted out at build time). See [staticIconProvider]
  /// for more context.
29 30 31
  const IconData(
    this.codePoint, {
    this.fontFamily,
32
    this.fontPackage,
33
    this.matchTextDirection = false,
34
    this.fontFamilyFallback,
35 36 37 38 39 40
  });

  /// The Unicode code point at which this icon is stored in the icon font.
  final int codePoint;

  /// The font family from which the glyph for the [codePoint] will be selected.
41
  final String? fontFamily;
42

43 44 45 46 47 48 49 50
  /// The name of the package from which the font family is included.
  ///
  /// The name is used by the [Icon] widget when configuring the [TextStyle] so
  /// that the given [fontFamily] is obtained from the appropriate asset.
  ///
  /// See also:
  ///
  ///  * [TextStyle], which describes how to use fonts from other packages.
51
  final String? fontPackage;
52

53 54 55 56 57 58 59
  /// Whether this icon should be automatically mirrored in right-to-left
  /// environments.
  ///
  /// The [Icon] widget respects this value by mirroring the icon when the
  /// [Directionality] is [TextDirection.rtl].
  final bool matchTextDirection;

60 61 62 63 64
  /// The ordered list of font families to fall back on when a glyph cannot be found in a higher priority font family.
  ///
  /// For more details, refer to the documentation of [TextStyle]
  final List<String>? fontFamilyFallback;

65
  @override
66
  bool operator ==(Object other) {
67
    if (other.runtimeType != runtimeType) {
68
      return false;
69
    }
70 71 72 73
    return other is IconData
        && other.codePoint == codePoint
        && other.fontFamily == fontFamily
        && other.fontPackage == fontPackage
74 75
        && other.matchTextDirection == matchTextDirection
        && listEquals(other.fontFamilyFallback, fontFamilyFallback);
76 77 78
  }

  @override
79 80 81 82 83 84 85 86 87
  int get hashCode {
    return Object.hash(
      codePoint,
      fontFamily,
      fontPackage,
      matchTextDirection,
      Object.hashAll(fontFamilyFallback ?? const <String?>[]),
    );
  }
88 89 90

  @override
  String toString() => 'IconData(U+${codePoint.toRadixString(16).toUpperCase().padLeft(5, '0')})';
91
}
92 93 94 95 96 97 98

/// [DiagnosticsProperty] that has an [IconData] as value.
class IconDataProperty extends DiagnosticsProperty<IconData> {
  /// Create a diagnostics property for [IconData].
  ///
  /// The [showName], [style], and [level] arguments must not be null.
  IconDataProperty(
99 100 101 102 103 104
    String super.name,
    super.value, {
    super.ifNull,
    super.showName,
    super.style,
    super.level,
105
  });
106 107

  @override
108 109
  Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
    final Map<String, Object?> json = super.toJsonMap(delegate);
110 111
    if (value != null) {
      json['valueProperties'] = <String, Object>{
112
        'codePoint': value!.codePoint,
113 114 115 116 117
      };
    }
    return json;
  }
}
118 119 120 121 122 123 124 125

class _StaticIconProvider {
  const _StaticIconProvider();
}

/// Annotation for classes that only provide static const [IconData] instances.
///
/// This is a hint to the font tree shaker to ignore the constant instances
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/// of [IconData] appearing in the declaration of this class when tree-shaking
/// unused code points from the bundled font.
///
/// Classes with this annotation must have only "static const" members. The
/// presence of any non-const [IconData] instances will preclude apps
/// importing the declaration into their application from being able to use
/// icon tree-shaking during release builds, resulting in larger font assets.
///
/// ```dart
/// @staticIconProvider
/// abstract final class MyCustomIcons {
///   static const String fontFamily = 'MyCustomIcons';
///   static const IconData happyFace = IconData(1, fontFamily: fontFamily);
///   static const IconData sadFace = IconData(2, fontFamily: fontFamily);
/// }
/// ```
142
const Object staticIconProvider = _StaticIconProvider();