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

5 6
import 'dart:ui' show hashValues;

7 8 9 10 11 12 13 14 15 16 17 18
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.
19 20 21
  ///
  /// 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.
22 23 24
  const IconData(
    this.codePoint, {
    this.fontFamily,
25
    this.fontPackage,
26
    this.matchTextDirection = false,
27 28 29 30 31 32 33 34
  });

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

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

45 46 47 48 49 50 51
  /// 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;

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

  @override
64
  int get hashCode => hashValues(codePoint, fontFamily, fontPackage, matchTextDirection);
65 66 67

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

/// [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(
    String name,
    IconData value, {
78 79 80 81
    String ifNull,
    bool showName = true,
    DiagnosticsTreeStyle style = DiagnosticsTreeStyle.singleLine,
    DiagnosticLevel level = DiagnosticLevel.info,
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  }) : assert(showName != null),
       assert(style != null),
       assert(level != null),
       super(name, value,
         showName: showName,
         ifNull: ifNull,
         style: style,
         level: level,
       );

  @override
  Map<String, Object> toJsonMap(DiagnosticsSerializationDelegate delegate) {
    final Map<String, Object> json = super.toJsonMap(delegate);
    if (value != null) {
      json['valueProperties'] = <String, Object>{
        'codePoint': value.codePoint,
      };
    }
    return json;
  }
}