WidgetsApp constructor

WidgetsApp({
  1. Key? key,
  2. GlobalKey<NavigatorState>? navigatorKey,
  3. RouteFactory? onGenerateRoute,
  4. InitialRouteListFactory? onGenerateInitialRoutes,
  5. RouteFactory? onUnknownRoute,
  6. NotificationListenerCallback<NavigationNotification>? onNavigationNotification,
  7. List<NavigatorObserver> navigatorObservers = const <NavigatorObserver>[],
  8. String? initialRoute,
  9. PageRouteFactory? pageRouteBuilder,
  10. Widget? home,
  11. Map<String, WidgetBuilder> routes = const <String, WidgetBuilder>{},
  12. TransitionBuilder? builder,
  13. String? title,
  14. GenerateAppTitle? onGenerateTitle,
  15. TextStyle? textStyle,
  16. required Color color,
  17. Locale? locale,
  18. Iterable<LocalizationsDelegate>? localizationsDelegates,
  19. LocaleListResolutionCallback? localeListResolutionCallback,
  20. LocaleResolutionCallback? localeResolutionCallback,
  21. Iterable<Locale> supportedLocales = const <Locale>[Locale('en', 'US')],
  22. bool showPerformanceOverlay = false,
  23. bool showSemanticsDebugger = false,
  24. bool debugShowWidgetInspector = false,
  25. bool debugShowCheckedModeBanner = true,
  26. InspectorSelectButtonBuilder? inspectorSelectButtonBuilder,
  27. Map<ShortcutActivator, Intent>? shortcuts,
  28. Map<Type, Action<Intent>>? actions,
  29. String? restorationScopeId,
  30. @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')].

This sample shows a basic Flutter application using WidgetsApp.
link

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;