WidgetsApp constructor
- Key? key,
- RouteFactory? onGenerateRoute,
- InitialRouteListFactory? onGenerateInitialRoutes,
- RouteFactory? onUnknownRoute,
- String? initialRoute,
- PageRouteFactory? pageRouteBuilder,
- Widget? home,
- Map<
String, WidgetBuilder> routes = const <String, WidgetBuilder>{}, - TransitionBuilder? builder,
- String? title,
- GenerateAppTitle? onGenerateTitle,
- TextStyle? textStyle,
- required Color color,
- Locale? locale,
- Iterable<
LocalizationsDelegate> ? localizationsDelegates, - LocaleListResolutionCallback? localeListResolutionCallback,
- LocaleResolutionCallback? localeResolutionCallback,
- Iterable<
Locale> supportedLocales = const <Locale>[Locale('en', 'US')], - bool showPerformanceOverlay = false,
- bool showSemanticsDebugger = false,
- bool debugShowWidgetInspector = false,
- bool debugShowCheckedModeBanner = true,
- InspectorSelectButtonBuilder? inspectorSelectButtonBuilder,
- Map<
ShortcutActivator, Intent> ? shortcuts, - Map<
Type, Action< ? actions,Intent> > - String? restorationScopeId,
- @Deprecated('Remove this parameter as it is now ignored. ' 'WidgetsApp never introduces its own MediaQuery; the View widget takes care of that. ' 'This feature was deprecated after v3.7.0-29.0.pre.') bool useInheritedMediaQuery = false,
Creates a widget that wraps a number of widgets that are commonly required for an application.
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
onGenerateRoute
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,
onGenerateRoute
will be invoked. If that fails, onUnknownRoute
will be invoked.
The pageRouteBuilder
is called to create a PageRoute that wraps newly built routes.
If the builder
is non-null and the onGenerateRoute
argument is null, then the
builder
will be provided only with the context and the child widget, whereas
the pageRouteBuilder
will be provided with RouteSettings; in that configuration,
the navigatorKey
, onUnknownRoute
, navigatorObservers
, and
initialRoute
properties 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')]
.
To create a local project with this code sample, run:
flutter create --sample=widgets.WidgetsApp.WidgetsApp.1 mysample
Implementation
WidgetsApp({ // can't be const because the asserts use methods on Iterable :-(
super.key,
this.navigatorKey,
this.onGenerateRoute,
this.onGenerateInitialRoutes,
this.onUnknownRoute,
this.onNavigationNotification,
List<NavigatorObserver> this.navigatorObservers = const <NavigatorObserver>[],
this.initialRoute,
this.pageRouteBuilder,
this.home,
Map<String, WidgetBuilder> 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.showSemanticsDebugger = false,
this.debugShowWidgetInspector = false,
this.debugShowCheckedModeBanner = true,
this.inspectorSelectButtonBuilder,
this.shortcuts,
this.actions,
this.restorationScopeId,
@Deprecated(
'Remove this parameter as it is now ignored. '
'WidgetsApp never introduces its own MediaQuery; the View widget takes care of that. '
'This feature was deprecated after v3.7.0-29.0.pre.'
)
this.useInheritedMediaQuery = false,
}) : assert(
home == null ||
onGenerateInitialRoutes == null,
'If onGenerateInitialRoutes is specified, the home argument will be '
'redundant.',
),
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(supportedLocales.isNotEmpty),
routeInformationProvider = null,
routeInformationParser = null,
routerDelegate = null,
backButtonDispatcher = null,
routerConfig = null;