diff --git a/packages/flutter/lib/src/material/app.dart b/packages/flutter/lib/src/material/app.dart index 7e454e5d36..813bae81cb 100644 --- a/packages/flutter/lib/src/material/app.dart +++ b/packages/flutter/lib/src/material/app.dart @@ -246,6 +246,7 @@ class _MaterialAppState extends State { ThemeData theme = config.theme ?? new ThemeData.fallback(); Widget result = new AnimatedTheme( data: theme, + isMaterialAppTheme: true, child: new WidgetsApp( key: new GlobalObjectKey(this), title: config.title, diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 1b4bf0ab28..8ab653bc9b 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -8,6 +8,7 @@ import 'package:flutter/widgets.dart'; import 'colors.dart'; import 'material.dart'; +import 'theme.dart'; const Duration _kBottomSheetDuration = const Duration(milliseconds: 200); const double _kMinFlingVelocity = 700.0; @@ -204,10 +205,12 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { class _ModalBottomSheetRoute extends PopupRoute { _ModalBottomSheetRoute({ Completer completer, - this.builder + this.builder, + this.theme, }) : super(completer: completer); final WidgetBuilder builder; + final ThemeData theme; @override Duration get transitionDuration => _kBottomSheetDuration; @@ -229,7 +232,10 @@ class _ModalBottomSheetRoute extends PopupRoute { @override Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { - return new _ModalBottomSheet(route: this); + Widget bottomSheet = new _ModalBottomSheet(route: this); + if (theme != null) + bottomSheet = new Theme(data: theme, child: bottomSheet); + return bottomSheet; } } @@ -257,7 +263,8 @@ Future showModalBottomSheet/**/({ BuildContext context, Widget final Completer completer = new Completer(); Navigator.push(context, new _ModalBottomSheetRoute( completer: completer, - builder: builder + builder: builder, + theme: Theme.of(context, shadowThemeOnly: true), )); return completer.future; } diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 3769672e75..f90ce8e36b 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -280,10 +280,12 @@ class SimpleDialog extends StatelessWidget { class _DialogRoute extends PopupRoute { _DialogRoute({ Completer completer, - this.child + this.child, + this.theme, }) : super(completer: completer); final Widget child; + final ThemeData theme; @override Duration get transitionDuration => const Duration(milliseconds: 150); @@ -296,7 +298,7 @@ class _DialogRoute extends PopupRoute { @override Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { - return child; + return theme != null ? new Theme(data: theme, child: child) : child; } @override @@ -324,6 +326,10 @@ class _DialogRoute extends PopupRoute { /// * Future showDialog/**/({ BuildContext context, Widget child }) { Completer completer = new Completer(); - Navigator.push(context, new _DialogRoute(completer: completer, child: child)); + Navigator.push(context, new _DialogRoute( + completer: completer, + child: child, + theme: Theme.of(context, shadowThemeOnly: true), + )); return completer.future; } diff --git a/packages/flutter/lib/src/material/drop_down.dart b/packages/flutter/lib/src/material/drop_down.dart index 8e93029668..e1873df96f 100644 --- a/packages/flutter/lib/src/material/drop_down.dart +++ b/packages/flutter/lib/src/material/drop_down.dart @@ -293,7 +293,8 @@ class _DropdownRoute extends PopupRoute<_DropdownRouteResult> { this.buttonRect, this.selectedIndex, this.elevation: 8, - TextStyle style + this.theme, + TextStyle style, }) : _style = style, super(completer: completer) { assert(style != null); } @@ -303,6 +304,7 @@ class _DropdownRoute extends PopupRoute<_DropdownRouteResult> { final Rect buttonRect; final int selectedIndex; final int elevation; + final ThemeData theme; // The layout gets this route's scrollableKey so that it can scroll the /// selected item into position, but only on the initial layout. bool initialLayout = true; @@ -329,9 +331,13 @@ class _DropdownRoute extends PopupRoute<_DropdownRouteResult> { @override Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { + Widget menu = new _DropdownMenu(route: this); + if (theme != null) + menu = new Theme(data: theme, child: menu); + return new CustomSingleChildLayout( delegate: new _DropdownMenuRouteLayout(route: this), - child: new _DropdownMenu(route: this) + child: menu, ); } } @@ -501,7 +507,8 @@ class _DropdownButtonState extends State> { buttonRect: _kMenuHorizontalPadding.inflateRect(itemRect), selectedIndex: _selectedIndex, elevation: config.elevation, - style: _textStyle + theme: Theme.of(context, shadowThemeOnly: true), + style: _textStyle, ); Navigator.push(context, _currentRoute); completer.future.then((_DropdownRouteResult newValue) { diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index ad19532aa0..7b2dac193c 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -374,13 +374,15 @@ class _PopupMenuRoute extends PopupRoute { this.position, this.items, this.initialValue, - this.elevation + this.elevation, + this.theme }) : super(completer: completer); final RelativeRect position; final List> items; final dynamic initialValue; final int elevation; + final ThemeData theme; @override Animation createAnimation() { @@ -411,9 +413,14 @@ class _PopupMenuRoute extends PopupRoute { selectedItemOffset += items[i].height; } } + + Widget menu = new _PopupMenu(route: this); + if (theme != null) + menu = new Theme(data: theme, child: menu); + return new CustomSingleChildLayout( delegate: new _PopupMenuRouteLayout(position, selectedItemOffset), - child: new _PopupMenu(route: this) + child: menu ); } } @@ -438,7 +445,8 @@ Future showMenu/**/({ position: position, items: items, initialValue: initialValue, - elevation: elevation + elevation: elevation, + theme: Theme.of(context, shadowThemeOnly: true), )); return completer.future; } diff --git a/packages/flutter/lib/src/material/theme.dart b/packages/flutter/lib/src/material/theme.dart index b591a47867..18ea45f565 100644 --- a/packages/flutter/lib/src/material/theme.dart +++ b/packages/flutter/lib/src/material/theme.dart @@ -18,6 +18,7 @@ const Duration kThemeAnimationDuration = const Duration(milliseconds: 200); /// /// * [AnimatedTheme] /// * [ThemeData] +/// * [MaterialApp] class Theme extends InheritedWidget { /// Applies the given theme [data] to [child]. /// @@ -25,6 +26,7 @@ class Theme extends InheritedWidget { Theme({ Key key, @required this.data, + this.isMaterialAppTheme: false, Widget child }) : super(key: key, child: child) { assert(child != null); @@ -34,14 +36,33 @@ class Theme extends InheritedWidget { /// Specifies the color and typography values for descendant widgets. final ThemeData data; + /// 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; + static final ThemeData _kFallbackTheme = new ThemeData.fallback(); /// The data from the closest instance of this class that encloses the given context. /// /// Defaults to the fallback theme data if none exists. - static ThemeData of(BuildContext context) { - Theme theme = context.inheritFromWidgetOfExactType(Theme); - return theme?.data ?? _kFallbackTheme; + /// + /// 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 app's theme - then return null. This property is specified in + /// situations where its useful to wrap a route's widgets with a Theme, but only + /// when the app's theme is being shadowed by a theme widget that is farather + /// down in the tree. See [isMaterialAppTheme]. + static ThemeData of(BuildContext context, { bool shadowThemeOnly: false }) { + final Theme theme = context.inheritFromWidgetOfExactType(Theme); + final ThemeData themeData = theme?.data ?? _kFallbackTheme; + return shadowThemeOnly ? (theme.isMaterialAppTheme ? null : themeData) : themeData; } @override @@ -77,6 +98,7 @@ class AnimatedTheme extends ImplicitlyAnimatedWidget { AnimatedTheme({ Key key, @required this.data, + this.isMaterialAppTheme: false, Curve curve: Curves.linear, Duration duration: kThemeAnimationDuration, this.child @@ -88,6 +110,9 @@ class AnimatedTheme extends ImplicitlyAnimatedWidget { /// Specifies the color and typography values for descendant widgets. final ThemeData data; + /// True if this theme was created by the [MaterialApp]. See [Theme.isMaterialAppTheme]. + final bool isMaterialAppTheme; + /// The widget below this widget in the tree. final Widget child; @@ -108,6 +133,7 @@ class _AnimatedThemeState extends AnimatedWidgetBaseState { @override Widget build(BuildContext context) { return new Theme( + isMaterialAppTheme: config.isMaterialAppTheme, child: config.child, data: _data.evaluate(animation) ); diff --git a/packages/flutter/test/material/theme_test.dart b/packages/flutter/test/material/theme_test.dart new file mode 100644 index 0000000000..c206e8c84a --- /dev/null +++ b/packages/flutter/test/material/theme_test.dart @@ -0,0 +1,173 @@ +// Copyright 2016 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. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('PopupMenu inherits app theme', (WidgetTester tester) async { + final Key popupMenuButtonKey = new UniqueKey(); + await tester.pumpWidget( + new MaterialApp( + theme: new ThemeData(brightness: Brightness.dark), + home: new Scaffold( + appBar: new AppBar( + actions: [ + new PopupMenuButton( + key: popupMenuButtonKey, + itemBuilder: (BuildContext context) { + return >[ + new PopupMenuItem(child: new Text('menuItem')) + ]; + } + ), + ] + ) + ) + ) + ); + + await tester.tap(find.byKey(popupMenuButtonKey)); + await tester.pump(const Duration(seconds: 1)); + + expect(Theme.of(tester.element(find.text('menuItem'))).brightness, equals(Brightness.dark)); + }); + + testWidgets('PopupMenu inherits shadowed app theme', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/5572 + final Key popupMenuButtonKey = new UniqueKey(); + await tester.pumpWidget( + new MaterialApp( + theme: new ThemeData(brightness: Brightness.dark), + home: new Theme( + data: new ThemeData(brightness: Brightness.light), + child: new Scaffold( + appBar: new AppBar( + actions: [ + new PopupMenuButton( + key: popupMenuButtonKey, + itemBuilder: (BuildContext context) { + return >[ + new PopupMenuItem(child: new Text('menuItem')) + ]; + } + ), + ] + ) + ) + ) + ) + ); + + await tester.tap(find.byKey(popupMenuButtonKey)); + await tester.pump(const Duration(seconds: 1)); + + expect(Theme.of(tester.element(find.text('menuItem'))).brightness, equals(Brightness.light)); + }); + + testWidgets('DropdownMenu inherits shadowed app theme', (WidgetTester tester) async { + final Key dropdownMenuButtonKey = new UniqueKey(); + await tester.pumpWidget( + new MaterialApp( + theme: new ThemeData(brightness: Brightness.dark), + home: new Theme( + data: new ThemeData(brightness: Brightness.light), + child: new Scaffold( + appBar: new AppBar( + actions: [ + new DropdownButton( + key: dropdownMenuButtonKey, + onChanged: (String newValue) { }, + value: 'menuItem', + items: >[ + new DropdownMenuItem( + value: 'menuItem', + child: new Text('menuItem'), + ), + ], + ) + ] + ) + ) + ) + ) + ); + + await tester.tap(find.byKey(dropdownMenuButtonKey)); + await tester.pump(const Duration(seconds: 1)); + + for(Element item in tester.elementList(find.text('menuItem'))) + expect(Theme.of(item).brightness, equals(Brightness.light)); + }); + + testWidgets('ModalBottomSheet inherits shadowed app theme', (WidgetTester tester) async { + await tester.pumpWidget( + new MaterialApp( + theme: new ThemeData(brightness: Brightness.dark), + home: new Theme( + data: new ThemeData(brightness: Brightness.light), + child: new Scaffold( + body: new Center( + child: new Builder( + builder: (BuildContext context) { + return new RaisedButton( + onPressed: () { + showModalBottomSheet( + context: context, + builder: (BuildContext context) => new Text('bottomSheet'), + ); + }, + child: new Text('SHOW'), + ); + } + ) + ) + ) + ) + ) + ); + + await tester.tap(find.text('SHOW')); + await tester.pump(const Duration(seconds: 1)); + expect(Theme.of(tester.element(find.text('bottomSheet'))).brightness, equals(Brightness.light)); + + await tester.tap(find.text('bottomSheet')); // dismiss the bottom sheet + await tester.pump(const Duration(seconds: 1)); + }); + + testWidgets('Dialog inherits shadowed app theme', (WidgetTester tester) async { + final GlobalKey scaffoldKey = new GlobalKey(); + await tester.pumpWidget( + new MaterialApp( + theme: new ThemeData(brightness: Brightness.dark), + home: new Theme( + data: new ThemeData(brightness: Brightness.light), + child: new Scaffold( + key: scaffoldKey, + body: new Center( + child: new Builder( + builder: (BuildContext context) { + return new RaisedButton( + onPressed: () { + showDialog( + context: context, + child: new Text('dialog'), + ); + }, + child: new Text('SHOW'), + ); + } + ) + ) + ) + ) + ) + ); + + await tester.tap(find.text('SHOW')); + await tester.pump(const Duration(seconds: 1)); + expect(Theme.of(tester.element(find.text('dialog'))).brightness, equals(Brightness.light)); + }); + +}