icon.dart 3.65 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 15 16 17 18 19 20 21 22 23 24 25
/// A material design icon.
///
/// Available icons are shown on this page:
/// <https://design.google.com/icons/>
///
/// 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].
///
/// To use this class, make sure you set `uses-material-design: true`
/// in your project's `flutter.yaml` file. This ensures that the
/// MaterialIcons font is included in your application. This font is
/// used to display the icons.
26 27 28
///
/// See also:
///
29 30 31
///  * [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
32
///  * [ImageIcon], for showing icons from [AssetImage]s or other [ImageProvider]s.
33
class Icon extends StatelessWidget {
34 35
  /// Creates an icon.
  ///
36
  /// The [size] and [color] default to the value given by the current [IconTheme].
Ian Hickson's avatar
Ian Hickson committed
37
  const Icon(this.icon, {
38
    Key key,
39
    this.size,
Adam Barth's avatar
Adam Barth committed
40
    this.color
41 42 43 44
  }) : super(key: key);

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

Adam Barth's avatar
Adam Barth committed
49 50 51
  /// The size of the icon in logical pixels.
  ///
  /// Icons occupy a square with width and height equal to size.
52 53 54 55
  ///
  /// 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.
56
  final double size;
Adam Barth's avatar
Adam Barth committed
57 58

  /// The color to use when drawing the icon.
59 60 61 62 63 64
  ///
  /// 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.
65 66 67
  ///
  /// The given color will be adjusted by the opacity of the current
  /// [IconTheme], if any.
Adam Barth's avatar
Adam Barth committed
68
  final Color color;
69

70
  @override
71
  Widget build(BuildContext context) {
Ian Hickson's avatar
Ian Hickson committed
72 73 74
    final IconThemeData iconTheme = IconTheme.of(context).fallback();

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

Adam Barth's avatar
Adam Barth committed
76
    if (icon == null)
77
      return new SizedBox(width: iconSize, height: iconSize);
Hans Muller's avatar
Hans Muller committed
78

Ian Hickson's avatar
Ian Hickson committed
79 80
    final double iconOpacity = iconTheme.opacity;
    Color iconColor = color ?? iconTheme.color;
Hans Muller's avatar
Hans Muller committed
81
    if (iconOpacity != 1.0)
82
      iconColor = iconColor.withOpacity(iconColor.opacity * iconOpacity);
83

84 85
    return new ExcludeSemantics(
      child: new SizedBox(
86 87
        width: iconSize,
        height: iconSize,
88
        child: new Center(
89 90 91 92 93 94 95 96 97
          child: new RichText(
            text: new TextSpan(
              text: new String.fromCharCode(icon.codePoint),
              style: new TextStyle(
                inherit: false,
                color: iconColor,
                fontSize: iconSize,
                fontFamily: 'MaterialIcons'
              )
98
            )
99 100 101
          )
        )
      )
102 103
    );
  }
Hixie's avatar
Hixie committed
104

105
  @override
Hixie's avatar
Hixie committed
106 107
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
108 109 110 111 112 113 114 115
    if (icon != null) {
      description.add('$icon');
    } else {
      description.add('<empty>');
    }
    if (size != null)
      description.add('size: $size');
    if (color != null)
116
      description.add('color: $color');
Hixie's avatar
Hixie committed
117
  }
118
}