theme.dart 7.06 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 9
import 'theme_data.dart';

10
export 'theme_data.dart' show Brightness, ThemeData;
11

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

15
/// Applies a theme to descendant widgets.
16
///
17 18 19 20 21 22
/// 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.
///
23 24
/// See also:
///
25 26 27 28 29
///  * [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.
30
class Theme extends InheritedWidget {
31 32
  /// Applies the given theme [data] to [child].
  ///
33
  /// The [data] and [child] arguments must not be null.
34
  Theme({
35
    Key key,
36
    @required this.data,
37
    this.isMaterialAppTheme: false,
38 39 40 41 42 43
    Widget child
  }) : super(key: key, child: child) {
    assert(child != null);
    assert(data != null);
  }

44
  /// Specifies the color and typography values for descendant widgets.
45 46
  final ThemeData data;

47 48 49 50 51 52 53 54 55 56 57
  /// 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;

58 59
  static final ThemeData _kFallbackTheme = new ThemeData.fallback();

60 61
  /// The data from the closest [Theme] instance that encloses the given
  /// context.
62
  ///
63 64
  /// Defaults to [new ThemeData.fallback] if there is no [Theme] in the given
  /// build context.
65
  ///
66 67 68 69 70 71 72
  /// 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].
73 74 75 76 77 78 79 80 81 82 83 84 85 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
  ///
  /// 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,
  ///           ),
  ///         );
  ///       },
  ///     ),
  ///   );
  /// }
  /// ```
113 114
  static ThemeData of(BuildContext context, { bool shadowThemeOnly: false }) {
    final Theme theme = context.inheritFromWidgetOfExactType(Theme);
115 116 117 118 119 120
    if (shadowThemeOnly) {
      if (theme == null || theme.isMaterialAppTheme)
        return null;
      return theme.data;
    }
    return (theme != null) ? theme.data : _kFallbackTheme;
121 122
  }

123
  @override
124
  bool updateShouldNotify(Theme old) => data != old.data;
Hixie's avatar
Hixie committed
125

126
  @override
Hixie's avatar
Hixie committed
127 128 129 130
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('$data');
  }
131
}
132

133 134
/// An animated value that interpolates [ThemeData]s.
class ThemeDataTween extends Tween<ThemeData> {
135
  /// Creates an interpolation between [begin] and [end].
136
  ThemeDataTween({ ThemeData begin, ThemeData end }) : super(begin: begin, end: end);
137

138
  @override
139 140 141
  ThemeData lerp(double t) => ThemeData.lerp(begin, end, t);
}

142
/// Animated version of [Theme] which automatically transitions the colors,
143
/// etc, over a given duration whenever the given theme changes.
144 145 146
///
/// See also:
///
147 148 149 150 151
///  * [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.
152
class AnimatedTheme extends ImplicitlyAnimatedWidget {
153 154
  /// Creates an animated theme.
  ///
155 156
  /// By default, the theme transition uses a linear curve. The [data] and
  /// [child] arguments must not be null.
157 158
  AnimatedTheme({
    Key key,
159
    @required this.data,
160
    this.isMaterialAppTheme: false,
161
    Curve curve: Curves.linear,
162
    Duration duration: kThemeAnimationDuration,
163
    @required this.child,
164 165 166 167 168
  }) : super(key: key, curve: curve, duration: duration) {
    assert(child != null);
    assert(data != null);
  }

169
  /// Specifies the color and typography values for descendant widgets.
170 171
  final ThemeData data;

172 173 174
  /// True if this theme was created by the [MaterialApp]. See [Theme.isMaterialAppTheme].
  final bool isMaterialAppTheme;

175
  /// The widget below this widget in the tree.
176 177
  final Widget child;

178
  @override
179 180 181 182
  _AnimatedThemeState createState() => new _AnimatedThemeState();
}

class _AnimatedThemeState extends AnimatedWidgetBaseState<AnimatedTheme> {
183
  ThemeDataTween _data;
184

185
  @override
186
  void forEachTween(TweenVisitor<dynamic> visitor) {
187
    // TODO(ianh): Use constructor tear-offs when it becomes possible
188
    _data = visitor(_data, config.data, (dynamic value) => new ThemeDataTween(begin: value));
189 190 191
    assert(_data != null);
  }

192
  @override
193 194
  Widget build(BuildContext context) {
    return new Theme(
195
      isMaterialAppTheme: config.isMaterialAppTheme,
196
      child: config.child,
197
      data: _data.evaluate(animation)
198 199 200
    );
  }

201
  @override
202 203 204 205 206 207
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    if (_data != null)
      description.add('$_data');
  }
}