icon_theme.dart 2.7 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
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/foundation.dart';
Adam Barth's avatar
Adam Barth committed
6

7 8
import 'basic.dart';
import 'framework.dart';
Adam Barth's avatar
Adam Barth committed
9
import 'icon_theme_data.dart';
10
import 'inherited_theme.dart';
Adam Barth's avatar
Adam Barth committed
11

12
/// Controls the default color, opacity, and size of icons in a widget subtree.
Ian Hickson's avatar
Ian Hickson committed
13 14
///
/// The icon theme is honored by [Icon] and [ImageIcon] widgets.
15
class IconTheme extends InheritedTheme {
16 17
  /// Creates an icon theme that controls the color, opacity, and size of
  /// descendant widgets.
18
  ///
19
  /// Both [data] and [child] arguments must not be null.
20
  const IconTheme({
Adam Barth's avatar
Adam Barth committed
21
    Key key,
22
    @required this.data,
23
    @required Widget child,
24 25 26
  }) : assert(data != null),
       assert(child != null),
       super(key: key, child: child);
Adam Barth's avatar
Adam Barth committed
27

Ian Hickson's avatar
Ian Hickson committed
28 29 30
  /// Creates an icon theme that controls the color, opacity, and size of
  /// descendant widgets, and merges in the current icon theme, if any.
  ///
31 32
  /// The [data] and [child] arguments must not be null.
  static Widget merge({
Ian Hickson's avatar
Ian Hickson committed
33 34
    Key key,
    @required IconThemeData data,
35
    @required Widget child,
Ian Hickson's avatar
Ian Hickson committed
36
  }) {
37
    return Builder(
38
      builder: (BuildContext context) {
39
        return IconTheme(
40 41 42 43 44
          key: key,
          data: _getInheritedIconThemeData(context).merge(data),
          child: child,
        );
      },
Ian Hickson's avatar
Ian Hickson committed
45 46 47
    );
  }

48
  /// The color, opacity, and size to use for icons in this subtree.
Adam Barth's avatar
Adam Barth committed
49 50
  final IconThemeData data;

Ian Hickson's avatar
Ian Hickson committed
51 52 53 54
  /// The data from the closest instance of this class that encloses the given
  /// context.
  ///
  /// Defaults to the current [ThemeData.iconTheme].
55 56 57 58 59 60
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// IconThemeData theme = IconTheme.of(context);
  /// ```
Adam Barth's avatar
Adam Barth committed
61
  static IconThemeData of(BuildContext context) {
62
    final IconThemeData iconThemeData = _getInheritedIconThemeData(context);
63 64 65
    return iconThemeData.isConcrete ? iconThemeData : const IconThemeData.fallback().merge(iconThemeData);
  }

66
  static IconThemeData _getInheritedIconThemeData(BuildContext context) {
67
    final IconTheme iconTheme = context.inheritFromWidgetOfExactType(IconTheme);
68
    return iconTheme?.data ?? const IconThemeData.fallback();
Adam Barth's avatar
Adam Barth committed
69 70
  }

71
  @override
72
  bool updateShouldNotify(IconTheme oldWidget) => data != oldWidget.data;
Adam Barth's avatar
Adam Barth committed
73

74 75 76 77 78 79
  @override
  Widget wrap(BuildContext context, Widget child) {
    final IconTheme iconTheme = context.ancestorWidgetOfExactType(IconTheme);
    return identical(this, iconTheme) ? child : IconTheme(data: data, child: child);
  }

80
  @override
81 82
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
83
    properties.add(DiagnosticsProperty<IconThemeData>('data', data, showName: false));
Adam Barth's avatar
Adam Barth committed
84 85
  }
}