icon_theme.dart 2.79 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Adam Barth's avatar
Adam Barth 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/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({
21 22 23
    Key? key,
    required this.data,
    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({
33 34 35
    Key? key,
    required IconThemeData data,
    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).resolve(context);
63 64 65 66 67 68 69
    return iconThemeData.isConcrete
      ? iconThemeData
      : iconThemeData.copyWith(
        size: iconThemeData.size ?? const IconThemeData.fallback().size,
        color: iconThemeData.color ?? const IconThemeData.fallback().color,
        opacity: iconThemeData.opacity ?? const IconThemeData.fallback().opacity,
      );
70 71
  }

72
  static IconThemeData _getInheritedIconThemeData(BuildContext context) {
73
    final IconTheme? iconTheme = context.dependOnInheritedWidgetOfExactType<IconTheme>();
74
    return iconTheme?.data ?? const IconThemeData.fallback();
Adam Barth's avatar
Adam Barth committed
75 76
  }

77
  @override
78
  bool updateShouldNotify(IconTheme oldWidget) => data != oldWidget.data;
Adam Barth's avatar
Adam Barth committed
79

80 81
  @override
  Widget wrap(BuildContext context, Widget child) {
82
    return IconTheme(data: data, child: child);
83 84
  }

85
  @override
86 87
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
88
    data.debugFillProperties(properties);
Adam Barth's avatar
Adam Barth committed
89 90
  }
}