icon_theme.dart 3.61 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 13 14
// Examples can assume:
// late BuildContext context;

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

28
  /// Creates an icon theme that controls the properties of
Ian Hickson's avatar
Ian Hickson committed
29 30
  /// 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 set of properties 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
  /// The data from the closest instance of this class that encloses the given
52
  /// context, if any.
Ian Hickson's avatar
Ian Hickson committed
53
  ///
54 55 56 57 58 59 60 61 62 63
  /// If there is no ambient icon theme, defaults to [IconThemeData.fallback].
  /// The returned [IconThemeData] is concrete (all values are non-null; see
  /// [IconThemeData.isConcrete]). Any properties on the ambient icon theme that
  /// are null get defaulted to the values specified on
  /// [IconThemeData.fallback].
  ///
  /// The [Theme] widget from the `material` library introduces an [IconTheme]
  /// widget set to the [ThemeData.iconTheme], so in a Material Design
  /// application, this will typically default to the icon theme from the
  /// ambient [Theme].
64 65 66 67 68 69
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// IconThemeData theme = IconTheme.of(context);
  /// ```
Adam Barth's avatar
Adam Barth committed
70
  static IconThemeData of(BuildContext context) {
71
    final IconThemeData iconThemeData = _getInheritedIconThemeData(context).resolve(context);
72 73 74 75
    return iconThemeData.isConcrete
      ? iconThemeData
      : iconThemeData.copyWith(
        size: iconThemeData.size ?? const IconThemeData.fallback().size,
76 77 78 79
        fill: iconThemeData.fill ?? const IconThemeData.fallback().fill,
        weight: iconThemeData.weight ?? const IconThemeData.fallback().weight,
        grade: iconThemeData.grade ?? const IconThemeData.fallback().grade,
        opticalSize: iconThemeData.opticalSize ?? const IconThemeData.fallback().opticalSize,
80 81
        color: iconThemeData.color ?? const IconThemeData.fallback().color,
        opacity: iconThemeData.opacity ?? const IconThemeData.fallback().opacity,
82
        shadows: iconThemeData.shadows ?? const IconThemeData.fallback().shadows,
83
      );
84 85
  }

86
  static IconThemeData _getInheritedIconThemeData(BuildContext context) {
87
    final IconTheme? iconTheme = context.dependOnInheritedWidgetOfExactType<IconTheme>();
88
    return iconTheme?.data ?? const IconThemeData.fallback();
Adam Barth's avatar
Adam Barth committed
89 90
  }

91
  @override
92
  bool updateShouldNotify(IconTheme oldWidget) => data != oldWidget.data;
Adam Barth's avatar
Adam Barth committed
93

94 95
  @override
  Widget wrap(BuildContext context, Widget child) {
96
    return IconTheme(data: data, child: child);
97 98
  }

99
  @override
100 101
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
102
    data.debugFillProperties(properties);
Adam Barth's avatar
Adam Barth committed
103 104
  }
}