icon_data.dart 2.92 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 11 12 13 14 15 16
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.
@immutable
class IconData {
  /// Creates icon data.
  ///
  /// Rarely used directly. Instead, consider using one of the predefined icons
  /// like the [Icons] collection.
17 18 19
  ///
  /// 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.
20 21 22
  const IconData(
    this.codePoint, {
    this.fontFamily,
23
    this.fontPackage,
24
    this.matchTextDirection = false,
25 26 27 28 29 30
  });

  /// 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.
31
  final String? fontFamily;
32

33 34 35 36 37 38 39 40
  /// 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.
41
  final String? fontPackage;
42

43 44 45 46 47 48 49
  /// 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;

50
  @override
51
  bool operator ==(Object other) {
52
    if (other.runtimeType != runtimeType) {
53
      return false;
54
    }
55 56 57 58 59
    return other is IconData
        && other.codePoint == codePoint
        && other.fontFamily == fontFamily
        && other.fontPackage == fontPackage
        && other.matchTextDirection == matchTextDirection;
60 61 62
  }

  @override
63
  int get hashCode => Object.hash(codePoint, fontFamily, fontPackage, matchTextDirection);
64 65 66

  @override
  String toString() => 'IconData(U+${codePoint.toRadixString(16).toUpperCase().padLeft(5, '0')})';
67
}
68 69 70 71 72 73 74

/// [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(
75 76 77 78 79 80
    String super.name,
    super.value, {
    super.ifNull,
    super.showName,
    super.style,
    super.level,
81 82
  }) : assert(showName != null),
       assert(style != null),
83
       assert(level != null);
84 85

  @override
86 87
  Map<String, Object?> toJsonMap(DiagnosticsSerializationDelegate delegate) {
    final Map<String, Object?> json = super.toJsonMap(delegate);
88 89
    if (value != null) {
      json['valueProperties'] = <String, Object>{
90
        'codePoint': value!.codePoint,
91 92 93 94 95
      };
    }
    return json;
  }
}