WidgetsApp constructor
Creates a widget that wraps a number of widgets that are commonly required for an application.
The boolean arguments, color
, and navigatorObservers
must not be null.
Most callers will want to use the home
or routes
parameters, or both.
The home
parameter is a convenience for the following routes
map:
<String, WidgetBuilder>{ '/': (BuildContext context) => myWidget }
It is possible to specify both home
and routes
, but only if routes
does
not contain an entry for '/'
. Conversely, if home
is omitted, routes
must contain an entry for '/'
.
If home
or routes
are not null, the routing implementation needs to know how
appropriately build PageRoutes
. This can be achieved by supplying the
pageRouteBuilder
parameter. The pageRouteBuilder
is used by MaterialApp
and CupertinoApp to create MaterialPageRoutes and CupertinoPageRoute,
respectively.
The builder
parameter is designed to provide the ability to wrap the visible
content of the app in some other widget. It is recommended that you use home
rather than builder
if you intend to only display a single route in your app.
WidgetsApp is also possible to provide a custom implementation of routing via the
onGeneratedRoute
and onUnknownRoute
parameters. These parameters correspond
to Navigator.onGenerateRoute and Navigator.onUnknownRoute. If home
, routes
,
and builder
are null, or if they fail to create a requested route,
onGeneratedRoute
will be invoked. If that fails, onUnknownRoute
will be invoked.
The pageRouteBuilder
will create a PageRoute that wraps newly built routes.
If the builder
is non-null and the onGenerateRoute
argument is null, then the
builder
will not be provided only with the context and the child widget, whereas
the pageRouteBuilder
will be provided with RouteSettings. If onGenerateRoute
is not provided, navigatorKey
, onUnknownRoute
, navigatorObservers
, and
initialRoute
must have their default values, as they will have no effect.
The supportedLocales
argument must be a list of one or more elements.
By default supportedLocales is [const Locale('en', 'US')]
.
Implementation
WidgetsApp({ // can't be const because the asserts use methods on Iterable :-(
Key key,
this.navigatorKey,
this.onGenerateRoute,
this.onUnknownRoute,
this.navigatorObservers = const <NavigatorObserver>[],
this.initialRoute,
this.pageRouteBuilder,
this.home,
this.routes = const <String, WidgetBuilder>{},
this.builder,
this.title = '',
this.onGenerateTitle,
this.textStyle,
@required this.color,
this.locale,
this.localizationsDelegates,
this.localeListResolutionCallback,
this.localeResolutionCallback,
this.supportedLocales = const <Locale>[Locale('en', 'US')],
this.showPerformanceOverlay = false,
this.checkerboardRasterCacheImages = false,
this.checkerboardOffscreenLayers = false,
this.showSemanticsDebugger = false,
this.debugShowWidgetInspector = false,
this.debugShowCheckedModeBanner = true,
this.inspectorSelectButtonBuilder,
}) : assert(navigatorObservers != null),
assert(routes != null),
assert(
home == null ||
!routes.containsKey(Navigator.defaultRouteName),
'If the home property is specified, the routes table '
'cannot include an entry for "/", since it would be redundant.'
),
assert(
builder != null ||
home != null ||
routes.containsKey(Navigator.defaultRouteName) ||
onGenerateRoute != null ||
onUnknownRoute != null,
'Either the home property must be specified, '
'or the routes table must include an entry for "/", '
'or there must be on onGenerateRoute callback specified, '
'or there must be an onUnknownRoute callback specified, '
'or the builder property must be specified, '
'because otherwise there is nothing to fall back on if the '
'app is started with an intent that specifies an unknown route.'
),
assert(
(home != null ||
routes.isNotEmpty ||
onGenerateRoute != null ||
onUnknownRoute != null)
||
(builder != null &&
navigatorKey == null &&
initialRoute == null &&
navigatorObservers.isEmpty),
'If no route is provided using '
'home, routes, onGenerateRoute, or onUnknownRoute, '
'a non-null callback for the builder property must be provided, '
'and the other navigator-related properties, '
'navigatorKey, initialRoute, and navigatorObservers, '
'must have their initial values '
'(null, null, and the empty list, respectively).'
),
assert(
builder != null ||
onGenerateRoute != null ||
pageRouteBuilder != null,
'If neither builder nor onGenerateRoute are provided, the '
'pageRouteBuilder must be specified so that the default handler '
'will know what kind of PageRoute transition to build.'
),
assert(title != null),
assert(color != null),
assert(supportedLocales != null && supportedLocales.isNotEmpty),
assert(showPerformanceOverlay != null),
assert(checkerboardRasterCacheImages != null),
assert(checkerboardOffscreenLayers != null),
assert(showSemanticsDebugger != null),
assert(debugShowCheckedModeBanner != null),
assert(debugShowWidgetInspector != null),
super(key: key);