icon.dart 4.32 KB
Newer Older
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 'package:flutter/widgets.dart';
6

7
import 'icons.dart';
8
import 'icon_button.dart';
Adam Barth's avatar
Adam Barth committed
9
import 'icon_theme.dart';
Ian Hickson's avatar
Ian Hickson committed
10
import 'icon_theme_data.dart';
11
import 'theme.dart';
12

13 14
/// A material design icon.
///
15
/// Icons are not interactive. For an interactive icon, consider [IconButton].
16
///
17 18 19
/// Icons are identified by their name (as given on that page), with spaces
/// converted to underscores, from the [Icons] class. For example, the "alarm
/// add" icon is [Icons.alarm_add].
20
///
21
/// Available icons are shown on this page: <https://design.google.com/icons/>
22
///
23
/// To use this class, make sure you set `uses-material-design: true` in your
24 25 26 27 28 29 30 31 32
/// project's `pubspec.yaml` file in the `flutter` section. This ensures that
/// the MaterialIcons font is included in your application. This font is used to
/// display the icons. For example:
///
/// ```yaml
/// name: my_awesome_application
/// flutter:
///   uses-material-design: true
/// ```
33 34 35
///
/// See also:
///
36 37 38
///  * [IconButton], for interactive icons.
///  * [Icons], for the list of available icons for use with this class.
///  * [IconTheme], which provides ambient configuration for icons.
Ian Hickson's avatar
Ian Hickson committed
39
///  * [ImageIcon], for showing icons from [AssetImage]s or other [ImageProvider]s.
40
class Icon extends StatelessWidget {
41 42
  /// Creates an icon.
  ///
43
  /// The [size] and [color] default to the value given by the current [IconTheme].
Ian Hickson's avatar
Ian Hickson committed
44
  const Icon(this.icon, {
45
    Key key,
46
    this.size,
Adam Barth's avatar
Adam Barth committed
47
    this.color
48 49 50 51
  }) : super(key: key);

  /// The icon to display. The available icons are described in [Icons].
  ///
Ian Hickson's avatar
Ian Hickson committed
52 53
  /// The icon can be null, in which case the widget will render as an empty
  /// space of the specified [size].
54
  final IconData icon;
55

Adam Barth's avatar
Adam Barth committed
56 57 58
  /// The size of the icon in logical pixels.
  ///
  /// Icons occupy a square with width and height equal to size.
59 60 61 62
  ///
  /// Defaults to the current [IconTheme] size, if any. If there is no
  /// [IconTheme], or it does not specify an explicit size, then it defaults to
  /// 24.0.
63 64 65 66 67
  ///
  /// If this [Icon] is being placed inside an [IconButton], then use
  /// [IconButton.size] instead, so that the [IconButton] can make the splash
  /// area the appropriate size as well. The [IconButton] uses an [IconTheme] to
  /// pass down the size to the [Icon].
68
  final double size;
Adam Barth's avatar
Adam Barth committed
69 70

  /// The color to use when drawing the icon.
71 72 73 74 75 76
  ///
  /// Defaults to the current [IconTheme] color, if any. If there is
  /// no [IconTheme], then it defaults to white if the theme is dark
  /// and black if the theme is light. See [Theme] to set the current
  /// theme and [ThemeData.brightness] for setting the current theme's
  /// brightness.
77 78 79
  ///
  /// The given color will be adjusted by the opacity of the current
  /// [IconTheme], if any.
80 81 82 83 84 85 86 87 88
  ///
  /// Typically, a material design color will be used, as follows:
  ///
  /// ```dart
  ///  new Icon(
  ///    icon: Icons.widgets,
  ///    color: Colors.blue[400],
  ///  ),
  /// ```
Adam Barth's avatar
Adam Barth committed
89
  final Color color;
90

91
  @override
92
  Widget build(BuildContext context) {
93
    final IconThemeData iconTheme = IconTheme.of(context);
Ian Hickson's avatar
Ian Hickson committed
94 95

    final double iconSize = size ?? iconTheme.size;
96

Adam Barth's avatar
Adam Barth committed
97
    if (icon == null)
98
      return new SizedBox(width: iconSize, height: iconSize);
Hans Muller's avatar
Hans Muller committed
99

Ian Hickson's avatar
Ian Hickson committed
100 101
    final double iconOpacity = iconTheme.opacity;
    Color iconColor = color ?? iconTheme.color;
Hans Muller's avatar
Hans Muller committed
102
    if (iconOpacity != 1.0)
103
      iconColor = iconColor.withOpacity(iconColor.opacity * iconOpacity);
104

105 106
    return new ExcludeSemantics(
      child: new SizedBox(
107 108
        width: iconSize,
        height: iconSize,
109
        child: new Center(
110 111 112 113 114 115 116 117 118
          child: new RichText(
            text: new TextSpan(
              text: new String.fromCharCode(icon.codePoint),
              style: new TextStyle(
                inherit: false,
                color: iconColor,
                fontSize: iconSize,
                fontFamily: 'MaterialIcons'
              )
119
            )
120 121 122
          )
        )
      )
123 124
    );
  }
Hixie's avatar
Hixie committed
125

126
  @override
Hixie's avatar
Hixie committed
127 128
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
129 130 131 132 133 134 135 136
    if (icon != null) {
      description.add('$icon');
    } else {
      description.add('<empty>');
    }
    if (size != null)
      description.add('size: $size');
    if (color != null)
137
      description.add('color: $color');
Hixie's avatar
Hixie committed
138
  }
139
}