icon_theme.dart 2.45 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 10
import 'icon_theme_data.dart';

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

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

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

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

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

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

73
  @override
74
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
75 76
    super.debugFillProperties(description);
    description.add(new DiagnosticsProperty<IconThemeData>('data', data, showName: false));
Adam Barth's avatar
Adam Barth committed
77 78
  }
}