@@ -17,6 +17,7 @@ import 'floating_action_button.dart';
1717import 'icons.dart' ;
1818import 'material_localizations.dart' ;
1919import 'page.dart' ;
20+ import 'scaffold.dart' ;
2021import 'theme.dart' ;
2122
2223/// [MaterialApp] uses this [TextStyle] as its [DefaultTextStyle] to encourage
@@ -168,6 +169,7 @@ class MaterialApp extends StatefulWidget {
168169 const MaterialApp ({
169170 Key key,
170171 this .navigatorKey,
172+ this .scaffoldMessengerKey,
171173 this .home,
172174 this .routes = const < String , WidgetBuilder > {},
173175 this .initialRoute,
@@ -215,6 +217,7 @@ class MaterialApp extends StatefulWidget {
215217 /// Creates a [MaterialApp] that uses the [Router] instead of a [Navigator] .
216218 const MaterialApp .router ({
217219 Key key,
220+ this .scaffoldMessengerKey,
218221 this .routeInformationProvider,
219222 @required this .routeInformationParser,
220223 @required this .routerDelegate,
@@ -263,6 +266,14 @@ class MaterialApp extends StatefulWidget {
263266 /// {@macro flutter.widgets.widgetsApp.navigatorKey}
264267 final GlobalKey <NavigatorState > navigatorKey;
265268
269+ /// A key to use when building the [ScaffoldMessenger] .
270+ ///
271+ /// If a [scaffoldMessengerKey] is specified, the [ScaffoldMessenger] can be
272+ /// directly manipulated without first obtaining it from a [BuildContext] via
273+ /// [ScaffoldMessenger.of] : from the [scaffoldMessengerKey] , use the
274+ /// [GlobalKey.currentState] getter.
275+ final GlobalKey <ScaffoldMessengerState > scaffoldMessengerKey;
276+
266277 /// {@macro flutter.widgets.widgetsApp.home}
267278 final Widget home;
268279
@@ -722,27 +733,30 @@ class _MaterialAppState extends State<MaterialApp> {
722733 }
723734 theme ?? = widget.theme ?? ThemeData .light ();
724735
725- return AnimatedTheme (
726- data: theme,
727- isMaterialAppTheme: true ,
728- child: widget.builder != null
729- ? Builder (
730- builder: (BuildContext context) {
731- // Why are we surrounding a builder with a builder?
732- //
733- // The widget.builder may contain code that invokes
734- // Theme.of(), which should return the theme we selected
735- // above in AnimatedTheme. However, if we invoke
736- // widget.builder() directly as the child of AnimatedTheme
737- // then there is no Context separating them, and the
738- // widget.builder() will not find the theme. Therefore, we
739- // surround widget.builder with yet another builder so that
740- // a context separates them and Theme.of() correctly
741- // resolves to the theme we passed to AnimatedTheme.
742- return widget.builder (context, child);
743- },
744- )
745- : child,
736+ return ScaffoldMessenger (
737+ key: widget.scaffoldMessengerKey,
738+ child: AnimatedTheme (
739+ data: theme,
740+ isMaterialAppTheme: true ,
741+ child: widget.builder != null
742+ ? Builder (
743+ builder: (BuildContext context) {
744+ // Why are we surrounding a builder with a builder?
745+ //
746+ // The widget.builder may contain code that invokes
747+ // Theme.of(), which should return the theme we selected
748+ // above in AnimatedTheme. However, if we invoke
749+ // widget.builder() directly as the child of AnimatedTheme
750+ // then there is no Context separating them, and the
751+ // widget.builder() will not find the theme. Therefore, we
752+ // surround widget.builder with yet another builder so that
753+ // a context separates them and Theme.of() correctly
754+ // resolves to the theme we passed to AnimatedTheme.
755+ return widget.builder (context, child);
756+ },
757+ )
758+ : child,
759+ )
746760 );
747761 }
748762
0 commit comments