theme.dart 8.91 KB
Newer Older
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';
6
import 'package:flutter/widgets.dart';
7

8
import 'material_localizations.dart';
9
import 'theme_data.dart';
10
import 'typography.dart';
11

12
export 'theme_data.dart' show Brightness, ThemeData;
13

14
/// The duration over which theme changes animate by default.
15
const Duration kThemeAnimationDuration = const Duration(milliseconds: 200);
16

17
/// Applies a theme to descendant widgets.
18
///
19 20 21 22 23 24
/// A theme describes the colors and typographic choices of an application.
///
/// Descendant widgets obtain the current theme's [ThemeData] object using
/// [Theme.of]. When a widget uses [Theme.of], it is automatically rebuilt if
/// the theme later changes, so that the changes can be applied.
///
25 26 27
/// The [Theme] widget implies an [IconTheme] widget, set to the value of the
/// [ThemeData.iconTheme] of the [data] for the [Theme].
///
28 29
/// See also:
///
30 31 32 33 34
///  * [ThemeData], which describes the actual configuration of a theme.
///  * [AnimatedTheme], which animates the [ThemeData] when it changes rather
///    than changing the theme all at once.
///  * [MaterialApp], which includes an [AnimatedTheme] widget configured via
///    the [MaterialApp.theme] argument.
35
class Theme extends StatelessWidget {
36 37
  /// Applies the given theme [data] to [child].
  ///
38
  /// The [data] and [child] arguments must not be null.
39
  const Theme({
40
    Key key,
41
    @required this.data,
42
    this.isMaterialAppTheme: false,
43
    @required this.child,
44 45
  }) : assert(child != null),
       assert(data != null),
46
       super(key: key);
47

48
  /// Specifies the color and typography values for descendant widgets.
49 50
  final ThemeData data;

51 52 53 54 55 56 57 58 59 60 61
  /// True if this theme was installed by the [MaterialApp].
  ///
  /// When an app uses the [Navigator] to push a route, the route's widgets
  /// will only inherit from the app's theme, even though the widget that
  /// triggered the push may inherit from a theme that "shadows" the app's
  /// theme because it's deeper in the widget tree. Apps can find the shadowing
  /// theme with `Theme.of(context, shadowThemeOnly: true)` and pass it along
  /// to the class that creates a route's widgets. Material widgets that push
  /// routes, like [PopupMenuButton] and [DropdownButton], do this.
  final bool isMaterialAppTheme;

62
  /// The widget below this widget in the tree.
63 64
  ///
  /// {@macro flutter.widgets.child}
65 66
  final Widget child;

67 68
  static final ThemeData _kFallbackTheme = new ThemeData.fallback();

69 70
  /// The data from the closest [Theme] instance that encloses the given
  /// context.
71
  ///
72 73 74 75
  /// If the given context is enclosed in a [Localizations] widget providing
  /// [MaterialLocalizations], the returned data is localized according to the
  /// nearest available [MaterialLocalizations].
  ///
76 77
  /// Defaults to [new ThemeData.fallback] if there is no [Theme] in the given
  /// build context.
78
  ///
79 80 81 82 83 84 85
  /// If [shadowThemeOnly] is true and the closest [Theme] ancestor was
  /// installed by the [MaterialApp] — in other words if the closest [Theme]
  /// ancestor does not shadow the application's theme — then this returns null.
  /// This argument should be used in situations where its useful to wrap a
  /// route's widgets with a [Theme], but only when the application's overall
  /// theme is being shadowed by a [Theme] widget that is deeper in the tree.
  /// See [isMaterialAppTheme].
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// @override
  /// Widget build(BuildContext context) {
  ///   return new Text(
  ///     'Example',
  ///     style: Theme.of(context).textTheme.title,
  ///   );
  /// }
  /// ```
  ///
  /// When the [Theme] is actually created in the same `build` function
  /// (possibly indirectly, e.g. as part of a [MaterialApp]), the `context`
  /// argument to the `build` function can't be used to find the [Theme] (since
  /// it's "above" the widget being returned). In such cases, the following
  /// technique with a [Builder] can be used to provide a new scope with a
  /// [BuildContext] that is "under" the [Theme]:
  ///
  /// ```dart
  /// @override
  /// Widget build(BuildContext context) {
  ///   return new MaterialApp(
  ///     theme: new ThemeData.light(),
  ///     body: new Builder(
  ///       // Create an inner BuildContext so that we can refer to
  ///       // the Theme with Theme.of().
  ///       builder: (BuildContext context) {
  ///         return new Center(
  ///           child: new Text(
  ///             'Example',
  ///             style: Theme.of(context).textTheme.title,
  ///           ),
  ///         );
  ///       },
  ///     ),
  ///   );
  /// }
  /// ```
126
  static ThemeData of(BuildContext context, { bool shadowThemeOnly: false }) {
127 128
    final _InheritedTheme inheritedTheme =
        context.inheritFromWidgetOfExactType(_InheritedTheme);
129
    if (shadowThemeOnly) {
130
      if (inheritedTheme == null || inheritedTheme.theme.isMaterialAppTheme)
131
        return null;
132
      return inheritedTheme.theme.data;
133
    }
134 135 136 137 138

    final ThemeData colorTheme = (inheritedTheme != null) ? inheritedTheme.theme.data : _kFallbackTheme;
    final MaterialLocalizations localizations = MaterialLocalizations.of(context);
    final TextTheme geometryTheme = localizations?.localTextGeometry ?? MaterialTextGeometry.englishLike;
    return ThemeData.localize(colorTheme, geometryTheme);
139 140
  }

141
  @override
142 143 144 145 146 147 148 149 150
  Widget build(BuildContext context) {
    return new _InheritedTheme(
      theme: this,
      child: new IconTheme(
        data: data.iconTheme,
        child: child,
      ),
    );
  }
Hixie's avatar
Hixie committed
151

152
  @override
153
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
154 155
    super.debugFillProperties(description);
    description.add(new DiagnosticsProperty<ThemeData>('data', data, showName: false));
Hixie's avatar
Hixie committed
156
  }
157
}
158

159 160 161 162 163 164 165 166 167 168 169
class _InheritedTheme extends InheritedWidget {
  const _InheritedTheme({
    Key key,
    @required this.theme,
    @required Widget child
  }) : assert(theme != null),
       super(key: key, child: child);

  final Theme theme;

  @override
170
  bool updateShouldNotify(_InheritedTheme old) => theme.data != old.theme.data;
171 172
}

173 174 175 176 177 178
/// An interpolation between two [ThemeData]s.
///
/// This class specializes the interpolation of [Tween<ThemeData>] to call the
/// [ThemeData.lerp] method.
///
/// See [Tween] for a discussion on how to use interpolation objects.
179
class ThemeDataTween extends Tween<ThemeData> {
180 181 182 183 184
  /// Creates a [ThemeData] tween.
  ///
  /// The [begin] and [end] properties must be non-null before the tween is
  /// first used, but the arguments can be null if the values are going to be
  /// filled in later.
185
  ThemeDataTween({ ThemeData begin, ThemeData end }) : super(begin: begin, end: end);
186

187
  @override
188 189 190
  ThemeData lerp(double t) => ThemeData.lerp(begin, end, t);
}

191
/// Animated version of [Theme] which automatically transitions the colors,
192
/// etc, over a given duration whenever the given theme changes.
193 194 195
///
/// See also:
///
196 197 198 199 200
///  * [Theme], which [AnimatedTheme] uses to actually apply the interpolated
///    theme.
///  * [ThemeData], which describes the actual configuration of a theme.
///  * [MaterialApp], which includes an [AnimatedTheme] widget configured via
///    the [MaterialApp.theme] argument.
201
class AnimatedTheme extends ImplicitlyAnimatedWidget {
202 203
  /// Creates an animated theme.
  ///
204 205
  /// By default, the theme transition uses a linear curve. The [data] and
  /// [child] arguments must not be null.
206
  const AnimatedTheme({
207
    Key key,
208
    @required this.data,
209
    this.isMaterialAppTheme: false,
210
    Curve curve: Curves.linear,
211
    Duration duration: kThemeAnimationDuration,
212
    @required this.child,
213 214 215
  }) : assert(child != null),
       assert(data != null),
       super(key: key, curve: curve, duration: duration);
216

217
  /// Specifies the color and typography values for descendant widgets.
218 219
  final ThemeData data;

220 221 222
  /// True if this theme was created by the [MaterialApp]. See [Theme.isMaterialAppTheme].
  final bool isMaterialAppTheme;

223
  /// The widget below this widget in the tree.
224 225
  ///
  /// {@macro flutter.widgets.child}
226 227
  final Widget child;

228
  @override
229 230 231 232
  _AnimatedThemeState createState() => new _AnimatedThemeState();
}

class _AnimatedThemeState extends AnimatedWidgetBaseState<AnimatedTheme> {
233
  ThemeDataTween _data;
234

235
  @override
236
  void forEachTween(TweenVisitor<dynamic> visitor) {
237
    // TODO(ianh): Use constructor tear-offs when it becomes possible
238
    _data = visitor(_data, widget.data, (dynamic value) => new ThemeDataTween(begin: value));
239 240 241
    assert(_data != null);
  }

242
  @override
243 244
  Widget build(BuildContext context) {
    return new Theme(
245 246
      isMaterialAppTheme: widget.isMaterialAppTheme,
      child: widget.child,
247
      data: _data.evaluate(animation)
248 249 250
    );
  }

251
  @override
252
  void debugFillProperties(DiagnosticPropertiesBuilder description) {
253 254
    super.debugFillProperties(description);
    description.add(new DiagnosticsProperty<ThemeDataTween>('data', _data, showName: false, defaultValue: null));
255 256
  }
}