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

5
import 'package:flutter/rendering.dart';
Ian Hickson's avatar
Ian Hickson committed
6

7 8 9
import 'basic.dart';
import 'framework.dart';
import 'icon.dart';
Ian Hickson's avatar
Ian Hickson committed
10 11
import 'icon_theme.dart';
import 'icon_theme_data.dart';
12
import 'image.dart';
Ian Hickson's avatar
Ian Hickson committed
13 14 15 16 17 18 19

/// An icon that comes from an [ImageProvider], e.g. an [AssetImage].
///
/// See also:
///
///  * [IconButton], for interactive icons.
///  * [IconTheme], which provides ambient configuration for icons.
20
///  * [Icon], for icons based on glyphs from fonts instead of images.
21
///  * [Icons], a predefined font based set of icons from the material design library.
Ian Hickson's avatar
Ian Hickson committed
22 23 24 25
class ImageIcon extends StatelessWidget {
  /// Creates an image icon.
  ///
  /// The [size] and [color] default to the value given by the current [IconTheme].
26 27
  const ImageIcon(
    this.image, {
Ian Hickson's avatar
Ian Hickson committed
28 29
    Key key,
    this.size,
30 31
    this.color,
    this.semanticLabel,
Ian Hickson's avatar
Ian Hickson committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  }) : super(key: key);

  /// The image to display as the icon.
  ///
  /// The icon can be null, in which case the widget will render as an empty
  /// space of the specified [size].
  final ImageProvider image;

  /// The size of the icon in logical pixels.
  ///
  /// Icons occupy a square with width and height equal to size.
  ///
  /// 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.
  final double size;

  /// The color to use when drawing the icon.
  ///
  /// Defaults to the current [IconTheme] color, if any. If there is
  /// no [IconTheme], then it defaults to not recolorizing the image.
  ///
  /// The image will additionally be adjusted by the opacity of the current
  /// [IconTheme], if any.
  final Color color;

58 59 60 61 62 63 64 65 66 67 68
  /// Semantic label for the icon.
  ///
  /// Announced in accessibility modes (e.g TalkBack/VoiceOver).
  /// This label does not show in the UI.
  ///
  /// See also:
  ///
  ///  * [Semantics.label], which is set to [semanticLabel] in the underlying
  ///    [Semantics] widget.
  final String semanticLabel;

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

    if (image == null)
75
      return Semantics(
76
        label: semanticLabel,
77
        child: SizedBox(width: iconSize, height: iconSize),
78
      );
Ian Hickson's avatar
Ian Hickson committed
79 80

    final double iconOpacity = iconTheme.opacity;
81
    Color iconColor = color ?? iconTheme.color;
Ian Hickson's avatar
Ian Hickson committed
82

83 84 85
    if (iconOpacity != null && iconOpacity != 1.0)
      iconColor = iconColor.withOpacity(iconColor.opacity * iconOpacity);

86
    return Semantics(
87 88 89 90 91 92 93 94 95 96
      label: semanticLabel,
      child: Image(
        image: image,
        width: iconSize,
        height: iconSize,
        color: iconColor,
        fit: BoxFit.scaleDown,
        alignment: Alignment.center,
        excludeFromSemantics: true,
      ),
Ian Hickson's avatar
Ian Hickson committed
97 98 99 100
    );
  }

  @override
101 102
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
103 104
    properties.add(DiagnosticsProperty<ImageProvider>('image', image, ifNull: '<empty>', showName: false));
    properties.add(DoubleProperty('size', size, defaultValue: null));
105
    properties.add(ColorProperty('color', color, defaultValue: null));
Ian Hickson's avatar
Ian Hickson committed
106 107
  }
}