app.dart 35.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:ui' as ui;

7
import 'package:flutter/cupertino.dart';
8
import 'package:flutter/foundation.dart';
9
import 'package:flutter/services.dart';
10

11
import 'arc.dart';
12
import 'colors.dart';
13 14
import 'floating_action_button.dart';
import 'icons.dart';
15
import 'material_localizations.dart';
16
import 'page.dart';
17
import 'scaffold.dart' show ScaffoldMessenger, ScaffoldMessengerState;
18
import 'scrollbar.dart';
19
import 'theme.dart';
20
import 'tooltip.dart';
21

22 23 24 25 26 27 28
/// [MaterialApp] uses this [TextStyle] as its [DefaultTextStyle] to encourage
/// developers to be intentional about their [DefaultTextStyle].
///
/// In Material Design, most [Text] widgets are contained in [Material] widgets,
/// which sets a specific [DefaultTextStyle]. If you're seeing text that uses
/// this text style, consider putting your text in a [Material] widget (or
/// another widget that sets a [DefaultTextStyle]).
29 30
const TextStyle _errorTextStyle = TextStyle(
  color: Color(0xD0FF0000),
31 32 33
  fontFamily: 'monospace',
  fontSize: 48.0,
  fontWeight: FontWeight.w900,
34
  decoration: TextDecoration.underline,
35
  decorationColor: Color(0xFFFFFF00),
36 37
  decorationStyle: TextDecorationStyle.double,
  debugLabel: 'fallback style; consider putting your text in a Material',
38 39
);

40 41 42 43 44 45 46 47 48 49 50 51 52
/// Describes which theme will be used by [MaterialApp].
enum ThemeMode {
  /// Use either the light or dark theme based on what the user has selected in
  /// the system settings.
  system,

  /// Always use the light mode regardless of system preference.
  light,

  /// Always use the dark mode (if available) regardless of system preference.
  dark,
}

53 54 55
/// An application that uses material design.
///
/// A convenience widget that wraps a number of widgets that are commonly
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/// required for material design applications. It builds upon a [WidgetsApp] by
/// adding material-design specific functionality, such as [AnimatedTheme] and
/// [GridPaper].
///
/// The [MaterialApp] configures the top-level [Navigator] to search for routes
/// in the following order:
///
///  1. For the `/` route, the [home] property, if non-null, is used.
///
///  2. Otherwise, the [routes] table is used, if it has an entry for the route.
///
///  3. Otherwise, [onGenerateRoute] is called, if provided. It should return a
///     non-null value for any _valid_ route not handled by [home] and [routes].
///
///  4. Finally if all else fails [onUnknownRoute] is called.
///
72 73 74
/// If a [Navigator] is created, at least one of these options must handle the
/// `/` route, since it is used when an invalid [initialRoute] is specified on
/// startup (e.g. by another application launching this one with an intent on
75
/// Android; see [dart:ui.PlatformDispatcher.defaultRouteName]).
76
///
77 78 79 80 81
/// This widget also configures the observer of the top-level [Navigator] (if
/// any) to perform [Hero] animations.
///
/// If [home], [routes], [onGenerateRoute], and [onUnknownRoute] are all null,
/// and [builder] is not null, then no [Navigator] is created.
82
///
83
/// {@tool snippet}
84 85 86
/// This example shows how to create a [MaterialApp] that disables the "debug"
/// banner with a [home] route that will be displayed when the app is launched.
///
87
/// ![The MaterialApp displays a Scaffold ](https://flutter.github.io/assets-for-api-docs/assets/material/basic_material_app.png)
88 89 90 91 92 93 94 95 96 97 98 99 100
///
/// ```dart
/// MaterialApp(
///   home: Scaffold(
///     appBar: AppBar(
///       title: const Text('Home'),
///     ),
///   ),
///   debugShowCheckedModeBanner: false,
/// )
/// ```
/// {@end-tool}
///
101
/// {@tool snippet}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/// This example shows how to create a [MaterialApp] that uses the [routes]
/// `Map` to define the "home" route and an "about" route.
///
/// ```dart
/// MaterialApp(
///   routes: <String, WidgetBuilder>{
///     '/': (BuildContext context) {
///       return Scaffold(
///         appBar: AppBar(
///           title: const Text('Home Route'),
///         ),
///       );
///     },
///     '/about': (BuildContext context) {
///       return Scaffold(
///         appBar: AppBar(
///           title: const Text('About Route'),
///         ),
///       );
///      }
///    },
/// )
/// ```
/// {@end-tool}
///
127
/// {@tool snippet}
128 129 130
/// This example shows how to create a [MaterialApp] that defines a [theme] that
/// will be used for material widgets in the app.
///
131
/// ![The MaterialApp displays a Scaffold with a dark background and a blue / grey AppBar at the top](https://flutter.github.io/assets-for-api-docs/assets/material/theme_material_app.png)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
///
/// ```dart
/// MaterialApp(
///   theme: ThemeData(
///     brightness: Brightness.dark,
///     primaryColor: Colors.blueGrey
///   ),
///   home: Scaffold(
///     appBar: AppBar(
///       title: const Text('MaterialApp Theme'),
///     ),
///   ),
/// )
/// ```
/// {@end-tool}
///
148 149
/// See also:
///
150 151 152 153
///  * [Scaffold], which provides standard app elements like an [AppBar] and a [Drawer].
///  * [Navigator], which is used to manage the app's stack of pages.
///  * [MaterialPageRoute], which defines an app page that transitions in a material-specific way.
///  * [WidgetsApp], which defines the basic app elements but does not depend on the material library.
154
///  * The Flutter Internationalization Tutorial,
155
///    <https://flutter.dev/tutorials/internationalization/>.
156
class MaterialApp extends StatefulWidget {
157 158
  /// Creates a MaterialApp.
  ///
159 160
  /// At least one of [home], [routes], [onGenerateRoute], or [builder] must be
  /// non-null. If only [routes] is given, it must include an entry for the
161 162 163
  /// [Navigator.defaultRouteName] (`/`), since that is the route used when the
  /// application is launched with an intent that specifies an otherwise
  /// unsupported route.
164
  ///
165
  /// This class creates an instance of [WidgetsApp].
166 167
  ///
  /// The boolean arguments, [routes], and [navigatorObservers], must not be null.
168
  const MaterialApp({
169
    Key? key,
170
    this.navigatorKey,
171
    this.scaffoldMessengerKey,
172
    this.home,
173
    Map<String, WidgetBuilder> this.routes = const <String, WidgetBuilder>{},
174
    this.initialRoute,
175
    this.onGenerateRoute,
176
    this.onGenerateInitialRoutes,
177
    this.onUnknownRoute,
178
    List<NavigatorObserver> this.navigatorObservers = const <NavigatorObserver>[],
179
    this.builder,
180
    this.title = '',
181 182 183
    this.onGenerateTitle,
    this.color,
    this.theme,
184
    this.darkTheme,
185 186
    this.highContrastTheme,
    this.highContrastDarkTheme,
187
    this.themeMode = ThemeMode.system,
188 189
    this.locale,
    this.localizationsDelegates,
190
    this.localeListResolutionCallback,
191
    this.localeResolutionCallback,
192
    this.supportedLocales = const <Locale>[Locale('en', 'US')],
193 194 195 196 197 198
    this.debugShowMaterialGrid = false,
    this.showPerformanceOverlay = false,
    this.checkerboardRasterCacheImages = false,
    this.checkerboardOffscreenLayers = false,
    this.showSemanticsDebugger = false,
    this.debugShowCheckedModeBanner = true,
199 200
    this.shortcuts,
    this.actions,
201
    this.restorationScopeId,
202
    this.scrollBehavior,
203
    this.useInheritedMediaQuery = false,
204
  }) : assert(routes != null),
205
       assert(navigatorObservers != null),
206 207 208 209 210 211 212
       assert(title != null),
       assert(debugShowMaterialGrid != null),
       assert(showPerformanceOverlay != null),
       assert(checkerboardRasterCacheImages != null),
       assert(checkerboardOffscreenLayers != null),
       assert(showSemanticsDebugger != null),
       assert(debugShowCheckedModeBanner != null),
213 214 215 216 217 218 219 220
       routeInformationProvider = null,
       routeInformationParser = null,
       routerDelegate = null,
       backButtonDispatcher = null,
       super(key: key);

  /// Creates a [MaterialApp] that uses the [Router] instead of a [Navigator].
  const MaterialApp.router({
221
    Key? key,
222
    this.scaffoldMessengerKey,
223
    this.routeInformationProvider,
224 225
    required RouteInformationParser<Object> this.routeInformationParser,
    required RouterDelegate<Object> this.routerDelegate,
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    this.backButtonDispatcher,
    this.builder,
    this.title = '',
    this.onGenerateTitle,
    this.color,
    this.theme,
    this.darkTheme,
    this.highContrastTheme,
    this.highContrastDarkTheme,
    this.themeMode = ThemeMode.system,
    this.locale,
    this.localizationsDelegates,
    this.localeListResolutionCallback,
    this.localeResolutionCallback,
    this.supportedLocales = const <Locale>[Locale('en', 'US')],
    this.debugShowMaterialGrid = false,
    this.showPerformanceOverlay = false,
    this.checkerboardRasterCacheImages = false,
    this.checkerboardOffscreenLayers = false,
    this.showSemanticsDebugger = false,
    this.debugShowCheckedModeBanner = true,
    this.shortcuts,
    this.actions,
249
    this.restorationScopeId,
250
    this.scrollBehavior,
251
    this.useInheritedMediaQuery = false,
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  }) : assert(routeInformationParser != null),
       assert(routerDelegate != null),
       assert(title != null),
       assert(debugShowMaterialGrid != null),
       assert(showPerformanceOverlay != null),
       assert(checkerboardRasterCacheImages != null),
       assert(checkerboardOffscreenLayers != null),
       assert(showSemanticsDebugger != null),
       assert(debugShowCheckedModeBanner != null),
       navigatorObservers = null,
       navigatorKey = null,
       onGenerateRoute = null,
       home = null,
       onGenerateInitialRoutes = null,
       onUnknownRoute = null,
       routes = null,
       initialRoute = null,
269
       super(key: key);
270

271
  /// {@macro flutter.widgets.widgetsApp.navigatorKey}
272
  final GlobalKey<NavigatorState>? navigatorKey;
273

274 275 276 277 278 279 280 281
  /// A key to use when building the [ScaffoldMessenger].
  ///
  /// If a [scaffoldMessengerKey] is specified, the [ScaffoldMessenger] can be
  /// directly manipulated without first obtaining it from a [BuildContext] via
  /// [ScaffoldMessenger.of]: from the [scaffoldMessengerKey], use the
  /// [GlobalKey.currentState] getter.
  final GlobalKey<ScaffoldMessengerState>? scaffoldMessengerKey;

282
  /// {@macro flutter.widgets.widgetsApp.home}
283
  final Widget? home;
284

285 286 287 288
  /// The application's top-level routing table.
  ///
  /// When a named route is pushed with [Navigator.pushNamed], the route name is
  /// looked up in this map. If the name is present, the associated
Janice Collins's avatar
Janice Collins committed
289 290 291
  /// [widgets.WidgetBuilder] is used to construct a [MaterialPageRoute] that
  /// performs an appropriate transition, including [Hero] animations, to the
  /// new route.
292
  ///
293
  /// {@macro flutter.widgets.widgetsApp.routes}
294
  final Map<String, WidgetBuilder>? routes;
Ian Hickson's avatar
Ian Hickson committed
295

xster's avatar
xster committed
296
  /// {@macro flutter.widgets.widgetsApp.initialRoute}
297
  final String? initialRoute;
298

xster's avatar
xster committed
299
  /// {@macro flutter.widgets.widgetsApp.onGenerateRoute}
300
  final RouteFactory? onGenerateRoute;
301

302
  /// {@macro flutter.widgets.widgetsApp.onGenerateInitialRoutes}
303
  final InitialRouteListFactory? onGenerateInitialRoutes;
304

xster's avatar
xster committed
305
  /// {@macro flutter.widgets.widgetsApp.onUnknownRoute}
306
  final RouteFactory? onUnknownRoute;
307

xster's avatar
xster committed
308
  /// {@macro flutter.widgets.widgetsApp.navigatorObservers}
309
  final List<NavigatorObserver>? navigatorObservers;
310

311
  /// {@macro flutter.widgets.widgetsApp.routeInformationProvider}
312
  final RouteInformationProvider? routeInformationProvider;
313 314

  /// {@macro flutter.widgets.widgetsApp.routeInformationParser}
315
  final RouteInformationParser<Object>? routeInformationParser;
316 317

  /// {@macro flutter.widgets.widgetsApp.routerDelegate}
318
  final RouterDelegate<Object>? routerDelegate;
319 320

  /// {@macro flutter.widgets.widgetsApp.backButtonDispatcher}
321
  final BackButtonDispatcher? backButtonDispatcher;
322

xster's avatar
xster committed
323
  /// {@macro flutter.widgets.widgetsApp.builder}
324
  ///
325 326
  /// Material specific features such as [showDialog] and [showMenu], and widgets
  /// such as [Tooltip], [PopupMenuButton], also require a [Navigator] to properly
327
  /// function.
328
  final TransitionBuilder? builder;
329

xster's avatar
xster committed
330
  /// {@macro flutter.widgets.widgetsApp.title}
331 332 333 334
  ///
  /// This value is passed unmodified to [WidgetsApp.title].
  final String title;

xster's avatar
xster committed
335
  /// {@macro flutter.widgets.widgetsApp.onGenerateTitle}
336 337
  ///
  /// This value is passed unmodified to [WidgetsApp.onGenerateTitle].
338
  final GenerateAppTitle? onGenerateTitle;
339

340 341 342
  /// Default visual properties, like colors fonts and shapes, for this app's
  /// material widgets.
  ///
343 344 345
  /// A second [darkTheme] [ThemeData] value, which is used to provide a dark
  /// version of the user interface can also be specified. [themeMode] will
  /// control which theme will be used if a [darkTheme] is provided.
346 347 348 349 350
  ///
  /// The default value of this property is the value of [ThemeData.light()].
  ///
  /// See also:
  ///
351
  ///  * [themeMode], which controls which theme to use.
352 353 354 355 356
  ///  * [MediaQueryData.platformBrightness], which indicates the platform's
  ///    desired brightness and is used to automatically toggle between [theme]
  ///    and [darkTheme] in [MaterialApp].
  ///  * [ThemeData.brightness], which indicates the [Brightness] of a theme's
  ///    colors.
357
  final ThemeData? theme;
358

359
  /// The [ThemeData] to use when a 'dark mode' is requested by the system.
360
  ///
361 362 363 364
  /// Some host platforms allow the users to select a system-wide 'dark mode',
  /// or the application may want to offer the user the ability to choose a
  /// dark theme just for this application. This is theme that will be used for
  /// such cases. [themeMode] will control which theme will be used.
365
  ///
366
  /// This theme should have a [ThemeData.brightness] set to [Brightness.dark].
367 368 369 370 371 372
  ///
  /// Uses [theme] instead when null. Defaults to the value of
  /// [ThemeData.light()] when both [darkTheme] and [theme] are null.
  ///
  /// See also:
  ///
373
  ///  * [themeMode], which controls which theme to use.
374 375 376 377 378
  ///  * [MediaQueryData.platformBrightness], which indicates the platform's
  ///    desired brightness and is used to automatically toggle between [theme]
  ///    and [darkTheme] in [MaterialApp].
  ///  * [ThemeData.brightness], which is typically set to the value of
  ///    [MediaQueryData.platformBrightness].
379
  final ThemeData? darkTheme;
380

381 382 383 384 385 386 387 388 389 390 391
  /// The [ThemeData] to use when 'high contrast' is requested by the system.
  ///
  /// Some host platforms (for example, iOS) allow the users to increase
  /// contrast through an accessibility setting.
  ///
  /// Uses [theme] instead when null.
  ///
  /// See also:
  ///
  ///  * [MediaQueryData.highContrast], which indicates the platform's
  ///    desire to increase contrast.
392
  final ThemeData? highContrastTheme;
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  /// The [ThemeData] to use when a 'dark mode' and 'high contrast' is requested
  /// by the system.
  ///
  /// Some host platforms (for example, iOS) allow the users to increase
  /// contrast through an accessibility setting.
  ///
  /// This theme should have a [ThemeData.brightness] set to [Brightness.dark].
  ///
  /// Uses [darkTheme] instead when null.
  ///
  /// See also:
  ///
  ///  * [MediaQueryData.highContrast], which indicates the platform's
  ///    desire to increase contrast.
408
  final ThemeData? highContrastDarkTheme;
409

410 411 412 413 414 415
  /// Determines which theme will be used by the application if both [theme]
  /// and [darkTheme] are provided.
  ///
  /// If set to [ThemeMode.system], the choice of which theme to use will
  /// be based on the user's system preferences. If the [MediaQuery.platformBrightnessOf]
  /// is [Brightness.light], [theme] will be used. If it is [Brightness.dark],
416
  /// [darkTheme] will be used (unless it is null, in which case [theme]
417 418 419 420 421 422
  /// will be used.
  ///
  /// If set to [ThemeMode.light] the [theme] will always be used,
  /// regardless of the user's system preference.
  ///
  /// If set to [ThemeMode.dark] the [darkTheme] will be used
423
  /// regardless of the user's system preference. If [darkTheme] is null
424 425 426 427 428 429
  /// then it will fallback to using [theme].
  ///
  /// The default value is [ThemeMode.system].
  ///
  /// See also:
  ///
430 431 432 433
  ///  * [theme], which is used when a light mode is selected.
  ///  * [darkTheme], which is used when a dark mode is selected.
  ///  * [ThemeData.brightness], which indicates to various parts of the
  ///    system what kind of theme is being used.
434
  final ThemeMode? themeMode;
435

xster's avatar
xster committed
436
  /// {@macro flutter.widgets.widgetsApp.color}
437
  final Color? color;
438

xster's avatar
xster committed
439
  /// {@macro flutter.widgets.widgetsApp.locale}
440
  final Locale? locale;
441

xster's avatar
xster committed
442
  /// {@macro flutter.widgets.widgetsApp.localizationsDelegates}
443
  ///
444
  /// Internationalized apps that require translations for one of the locales
445
  /// listed in [GlobalMaterialLocalizations] should specify this parameter
446 447 448 449 450
  /// and list the [supportedLocales] that the application can handle.
  ///
  /// ```dart
  /// import 'package:flutter_localizations/flutter_localizations.dart';
  /// MaterialApp(
451 452 453 454 455 456
  ///   localizationsDelegates: [
  ///     // ... app-specific localization delegate[s] here
  ///     GlobalMaterialLocalizations.delegate,
  ///     GlobalWidgetsLocalizations.delegate,
  ///   ],
  ///   supportedLocales: [
457 458 459 460 461 462 463 464 465 466 467 468 469 470
  ///     const Locale('en', 'US'), // English
  ///     const Locale('he', 'IL'), // Hebrew
  ///     // ... other locales the app supports
  ///   ],
  ///   // ...
  /// )
  /// ```
  ///
  /// ## Adding localizations for a new locale
  ///
  /// The information that follows applies to the unusual case of an app
  /// adding translations for a language not already supported by
  /// [GlobalMaterialLocalizations].
  ///
471 472 473
  /// Delegates that produce [WidgetsLocalizations] and [MaterialLocalizations]
  /// are included automatically. Apps can provide their own versions of these
  /// localizations by creating implementations of
474
  /// [LocalizationsDelegate<WidgetsLocalizations>] or
475
  /// [LocalizationsDelegate<MaterialLocalizations>] whose load methods return
476
  /// custom versions of [WidgetsLocalizations] or [MaterialLocalizations].
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  ///
  /// For example: to add support to [MaterialLocalizations] for a
  /// locale it doesn't already support, say `const Locale('foo', 'BR')`,
  /// one could just extend [DefaultMaterialLocalizations]:
  ///
  /// ```dart
  /// class FooLocalizations extends DefaultMaterialLocalizations {
  ///   FooLocalizations(Locale locale) : super(locale);
  ///   @override
  ///   String get okButtonLabel {
  ///     if (locale == const Locale('foo', 'BR'))
  ///       return 'foo';
  ///     return super.okButtonLabel;
  ///   }
  /// }
  ///
  /// ```
  ///
  /// A `FooLocalizationsDelegate` is essentially just a method that constructs
  /// a `FooLocalizations` object. We return a [SynchronousFuture] here because
  /// no asynchronous work takes place upon "loading" the localizations object.
  ///
  /// ```dart
  /// class FooLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
  ///   const FooLocalizationsDelegate();
  ///   @override
  ///   Future<FooLocalizations> load(Locale locale) {
504
  ///     return SynchronousFuture(FooLocalizations(locale));
505 506 507 508 509 510 511 512 513 514 515 516 517
  ///   }
  ///   @override
  ///   bool shouldReload(FooLocalizationsDelegate old) => false;
  /// }
  /// ```
  ///
  /// Constructing a [MaterialApp] with a `FooLocalizationsDelegate` overrides
  /// the automatically included delegate for [MaterialLocalizations] because
  /// only the first delegate of each [LocalizationsDelegate.type] is used and
  /// the automatically included delegates are added to the end of the app's
  /// [localizationsDelegates] list.
  ///
  /// ```dart
518
  /// MaterialApp(
519 520 521 522 523 524
  ///   localizationsDelegates: [
  ///     const FooLocalizationsDelegate(),
  ///   ],
  ///   // ...
  /// )
  /// ```
525 526 527 528 529 530 531
  /// See also:
  ///
  ///  * [supportedLocales], which must be specified along with
  ///    [localizationsDelegates].
  ///  * [GlobalMaterialLocalizations], a [localizationsDelegates] value
  ///    which provides material localizations for many languages.
  ///  * The Flutter Internationalization Tutorial,
532
  ///    <https://flutter.dev/tutorials/internationalization/>.
533
  final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates;
534

535 536 537
  /// {@macro flutter.widgets.widgetsApp.localeListResolutionCallback}
  ///
  /// This callback is passed along to the [WidgetsApp] built by this widget.
538
  final LocaleListResolutionCallback? localeListResolutionCallback;
539

540
  /// {@macro flutter.widgets.LocaleResolutionCallback}
541 542
  ///
  /// This callback is passed along to the [WidgetsApp] built by this widget.
543
  final LocaleResolutionCallback? localeResolutionCallback;
544

xster's avatar
xster committed
545
  /// {@macro flutter.widgets.widgetsApp.supportedLocales}
546
  ///
xster's avatar
xster committed
547
  /// It is passed along unmodified to the [WidgetsApp] built by this widget.
548
  ///
549 550 551 552 553 554 555
  /// See also:
  ///
  ///  * [localizationsDelegates], which must be specified for localized
  ///    applications.
  ///  * [GlobalMaterialLocalizations], a [localizationsDelegates] value
  ///    which provides material localizations for many languages.
  ///  * The Flutter Internationalization Tutorial,
556
  ///    <https://flutter.dev/tutorials/internationalization/>.
557 558
  final Iterable<Locale> supportedLocales;

559
  /// Turns on a performance overlay.
560 561 562
  ///
  /// See also:
  ///
563
  ///  * <https://flutter.dev/debugging/#performanceoverlay>
564 565
  final bool showPerformanceOverlay;

566 567 568
  /// Turns on checkerboarding of raster cache images.
  final bool checkerboardRasterCacheImages;

569 570 571
  /// Turns on checkerboarding of layers rendered to offscreen bitmaps.
  final bool checkerboardOffscreenLayers;

572 573 574 575
  /// Turns on an overlay that shows the accessibility information
  /// reported by the framework.
  final bool showSemanticsDebugger;

xster's avatar
xster committed
576
  /// {@macro flutter.widgets.widgetsApp.debugShowCheckedModeBanner}
577 578
  final bool debugShowCheckedModeBanner;

579
  /// {@macro flutter.widgets.widgetsApp.shortcuts}
580
  /// {@tool snippet}
581 582 583 584 585 586 587 588 589 590 591
  /// This example shows how to add a single shortcut for
  /// [LogicalKeyboardKey.select] to the default shortcuts without needing to
  /// add your own [Shortcuts] widget.
  ///
  /// Alternatively, you could insert a [Shortcuts] widget with just the mapping
  /// you want to add between the [WidgetsApp] and its child and get the same
  /// effect.
  ///
  /// ```dart
  /// Widget build(BuildContext context) {
  ///   return WidgetsApp(
592
  ///     shortcuts: <ShortcutActivator, Intent>{
593
  ///       ... WidgetsApp.defaultShortcuts,
594
  ///       const SingleActivator(LogicalKeyboardKey.select): const ActivateIntent(),
595 596
  ///     },
  ///     color: const Color(0xFFFF0000),
597
  ///     builder: (BuildContext context, Widget? child) {
598 599 600 601 602 603 604
  ///       return const Placeholder();
  ///     },
  ///   );
  /// }
  /// ```
  /// {@end-tool}
  /// {@macro flutter.widgets.widgetsApp.shortcuts.seeAlso}
605
  final Map<ShortcutActivator, Intent>? shortcuts;
606 607

  /// {@macro flutter.widgets.widgetsApp.actions}
608
  /// {@tool snippet}
609 610 611 612 613 614 615 616 617 618 619
  /// This example shows how to add a single action handling an
  /// [ActivateAction] to the default actions without needing to
  /// add your own [Actions] widget.
  ///
  /// Alternatively, you could insert a [Actions] widget with just the mapping
  /// you want to add between the [WidgetsApp] and its child and get the same
  /// effect.
  ///
  /// ```dart
  /// Widget build(BuildContext context) {
  ///   return WidgetsApp(
620
  ///     actions: <Type, Action<Intent>>{
621
  ///       ... WidgetsApp.defaultActions,
622
  ///       ActivateAction: CallbackAction<Intent>(
623
  ///         onInvoke: (Intent intent) {
624
  ///           // Do something here...
625
  ///           return null;
626 627 628 629
  ///         },
  ///       ),
  ///     },
  ///     color: const Color(0xFFFF0000),
630
  ///     builder: (BuildContext context, Widget? child) {
631 632 633 634 635 636 637
  ///       return const Placeholder();
  ///     },
  ///   );
  /// }
  /// ```
  /// {@end-tool}
  /// {@macro flutter.widgets.widgetsApp.actions.seeAlso}
638
  final Map<Type, Action<Intent>>? actions;
639

640
  /// {@macro flutter.widgets.widgetsApp.restorationScopeId}
641
  final String? restorationScopeId;
642

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  /// {@template flutter.material.materialApp.scrollBehavior}
  /// The default [ScrollBehavior] for the application.
  ///
  /// [ScrollBehavior]s describe how [Scrollable] widgets behave. Providing
  /// a [ScrollBehavior] can set the default [ScrollPhysics] across
  /// an application, and manage [Scrollable] decorations like [Scrollbar]s and
  /// [GlowingOverscrollIndicator]s.
  /// {@endtemplate}
  ///
  /// When null, defaults to [MaterialScrollBehavior].
  ///
  /// See also:
  ///
  ///  * [ScrollConfiguration], which controls how [Scrollable] widgets behave
  ///    in a subtree.
  final ScrollBehavior? scrollBehavior;

660
  /// Turns on a [GridPaper] overlay that paints a baseline grid
661 662
  /// Material apps.
  ///
663
  /// Only available in debug mode.
664 665 666
  ///
  /// See also:
  ///
667
  ///  * <https://material.io/design/layout/spacing-methods.html>
Ian Hickson's avatar
Ian Hickson committed
668
  final bool debugShowMaterialGrid;
669

670 671 672
  /// {@macro flutter.widgets.widgetsApp.useInheritedMediaQuery}
  final bool useInheritedMediaQuery;

673
  @override
674
  State<MaterialApp> createState() => _MaterialAppState();
675 676 677 678 679 680

  /// The [HeroController] used for Material page transitions.
  ///
  /// Used by the [MaterialApp].
  static HeroController createMaterialHeroController() {
    return HeroController(
681
      createRectTween: (Rect? begin, Rect? end) {
682 683 684 685
        return MaterialRectArcTween(begin: begin, end: end);
      },
    );
  }
686 687
}

688 689 690 691 692 693 694 695
/// Describes how [Scrollable] widgets behave for [MaterialApp]s.
///
/// {@macro flutter.widgets.scrollBehavior}
///
/// Setting a [MaterialScrollBehavior] will apply a
/// [GlowingOverscrollIndicator] to [Scrollable] descendants when executing on
/// [TargetPlatform.android] and [TargetPlatform.fuchsia].
///
696 697
/// When using the desktop platform, if the [Scrollable] widget scrolls in the
/// [Axis.vertical], a [Scrollbar] is applied.
698
///
699 700 701 702 703
/// [MaterialScrollBehavior.androidOverscrollIndicator] specifies the
/// overscroll indicator that is used on [TargetPlatform.android]. When null,
/// [ThemeData.androidOverscrollIndicator] is used. If also null, the default
/// overscroll indicator is the [GlowingOverscrollIndicator].
///
704 705 706 707
/// See also:
///
///  * [ScrollBehavior], the default scrolling behavior extended by this class.
class MaterialScrollBehavior extends ScrollBehavior {
708 709 710
  /// Creates a MaterialScrollBehavior that decorates [Scrollable]s with
  /// [GlowingOverscrollIndicator]s and [Scrollbar]s based on the current
  /// platform and provided [ScrollableDetails].
711 712 713 714 715
  ///
  /// [MaterialScrollBehavior.androidOverscrollIndicator] specifies the
  /// overscroll indicator that is used on [TargetPlatform.android]. When null,
  /// [ThemeData.androidOverscrollIndicator] is used. If also null, the default
  /// overscroll indicator is the [GlowingOverscrollIndicator].
716 717
  const MaterialScrollBehavior({
    AndroidOverscrollIndicator? androidOverscrollIndicator,
718 719 720 721
  }) : _androidOverscrollIndicator = androidOverscrollIndicator,
       super(androidOverscrollIndicator: androidOverscrollIndicator);

  final AndroidOverscrollIndicator? _androidOverscrollIndicator;
722 723 724 725

  @override
  TargetPlatform getPlatform(BuildContext context) => Theme.of(context).platform;

726
  @override
727 728 729 730 731
  Widget buildScrollbar(BuildContext context, Widget child, ScrollableDetails details) {
    // When modifying this function, consider modifying the implementation in
    // the base class as well.
    switch (axisDirectionToAxis(details.direction)) {
      case Axis.horizontal:
732
        return child;
733 734 735 736 737 738 739
      case Axis.vertical:
        switch (getPlatform(context)) {
          case TargetPlatform.linux:
          case TargetPlatform.macOS:
          case TargetPlatform.windows:
            return Scrollbar(
              controller: details.controller,
740
              child: child,
741 742 743 744 745 746 747
            );
          case TargetPlatform.android:
          case TargetPlatform.fuchsia:
          case TargetPlatform.iOS:
            return child;
        }
    }
748 749 750
  }

  @override
751
  Widget buildOverscrollIndicator(BuildContext context, Widget child, ScrollableDetails details) {
752 753
    // When modifying this function, consider modifying the implementation in
    // the base class as well.
754 755 756
    final AndroidOverscrollIndicator indicator = _androidOverscrollIndicator
        ?? Theme.of(context).androidOverscrollIndicator
        ?? androidOverscrollIndicator;
757 758
    switch (getPlatform(context)) {
      case TargetPlatform.iOS:
759
      case TargetPlatform.linux:
760
      case TargetPlatform.macOS:
761
      case TargetPlatform.windows:
762 763
        return child;
      case TargetPlatform.android:
764
        switch (indicator) {
765 766 767 768 769 770 771 772 773
          case AndroidOverscrollIndicator.stretch:
            return StretchingOverscrollIndicator(
              axisDirection: details.direction,
              child: child,
            );
          case AndroidOverscrollIndicator.glow:
            continue glow;
        }
      glow:
774
      case TargetPlatform.fuchsia:
775
        return GlowingOverscrollIndicator(
776
          axisDirection: details.direction,
777
          color: Theme.of(context).colorScheme.secondary,
778
          child: child,
779 780
        );
    }
781 782 783
  }
}

784
class _MaterialAppState extends State<MaterialApp> {
785
  late HeroController _heroController;
786

787 788
  bool get _usesRouter => widget.routerDelegate != null;

789 790 791
  @override
  void initState() {
    super.initState();
792
    _heroController = MaterialApp.createMaterialHeroController();
793 794
  }

795 796 797 798 799 800 801
  // Combine the Localizations for Material with the ones contributed
  // by the localizationsDelegates parameter, if any. Only the first delegate
  // of a particular LocalizationsDelegate.type is loaded so the
  // localizationsDelegate parameter can be used to override
  // _MaterialLocalizationsDelegate.
  Iterable<LocalizationsDelegate<dynamic>> get _localizationsDelegates sync* {
    if (widget.localizationsDelegates != null)
802
      yield* widget.localizationsDelegates!;
803
    yield DefaultMaterialLocalizations.delegate;
804
    yield DefaultCupertinoLocalizations.delegate;
805 806
  }

807 808 809 810
  Widget _inspectorSelectButtonBuilder(BuildContext context, VoidCallback onPressed) {
    return FloatingActionButton(
      onPressed: onPressed,
      mini: true,
811
      child: const Icon(Icons.search),
812 813 814
    );
  }

815
  Widget _materialBuilder(BuildContext context, Widget? child) {
816 817 818 819 820 821
    // Resolve which theme to use based on brightness and high contrast.
    final ThemeMode mode = widget.themeMode ?? ThemeMode.system;
    final Brightness platformBrightness = MediaQuery.platformBrightnessOf(context);
    final bool useDarkTheme = mode == ThemeMode.dark
      || (mode == ThemeMode.system && platformBrightness == ui.Brightness.dark);
    final bool highContrast = MediaQuery.highContrastOf(context);
822
    ThemeData? theme;
823 824 825 826 827 828 829 830 831 832

    if (useDarkTheme && highContrast && widget.highContrastDarkTheme != null) {
      theme = widget.highContrastDarkTheme;
    } else if (useDarkTheme && widget.darkTheme != null) {
      theme = widget.darkTheme;
    } else if (highContrast && widget.highContrastTheme != null) {
      theme = widget.highContrastTheme;
    }
    theme ??= widget.theme ?? ThemeData.light();

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    return ScaffoldMessenger(
      key: widget.scaffoldMessengerKey,
      child: AnimatedTheme(
        data: theme,
        child: widget.builder != null
          ? Builder(
              builder: (BuildContext context) {
                // Why are we surrounding a builder with a builder?
                //
                // The widget.builder may contain code that invokes
                // Theme.of(), which should return the theme we selected
                // above in AnimatedTheme. However, if we invoke
                // widget.builder() directly as the child of AnimatedTheme
                // then there is no Context separating them, and the
                // widget.builder() will not find the theme. Therefore, we
                // surround widget.builder with yet another builder so that
                // a context separates them and Theme.of() correctly
                // resolves to the theme we passed to AnimatedTheme.
                return widget.builder!(context, child);
              },
            )
854
          : child ?? const SizedBox.shrink(),
855
      ),
856 857 858 859 860 861 862 863 864 865 866 867 868 869
    );
  }

  Widget _buildWidgetApp(BuildContext context) {
    // The color property is always pulled from the light theme, even if dark
    // mode is activated. This was done to simplify the technical details
    // of switching themes and it was deemed acceptable because this color
    // property is only used on old Android OSes to color the app bar in
    // Android's switcher UI.
    //
    // blue is the primary color of the default theme.
    final Color materialColor = widget.color ?? widget.theme?.primaryColor ?? Colors.blue;
    if (_usesRouter) {
      return WidgetsApp.router(
870
        key: GlobalObjectKey(this),
871
        routeInformationProvider: widget.routeInformationProvider,
872 873
        routeInformationParser: widget.routeInformationParser!,
        routerDelegate: widget.routerDelegate!,
874 875
        backButtonDispatcher: widget.backButtonDispatcher,
        builder: _materialBuilder,
876 877 878
        title: widget.title,
        onGenerateTitle: widget.onGenerateTitle,
        textStyle: _errorTextStyle,
879
        color: materialColor,
880 881 882 883 884 885 886 887 888 889
        locale: widget.locale,
        localizationsDelegates: _localizationsDelegates,
        localeResolutionCallback: widget.localeResolutionCallback,
        localeListResolutionCallback: widget.localeListResolutionCallback,
        supportedLocales: widget.supportedLocales,
        showPerformanceOverlay: widget.showPerformanceOverlay,
        checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages,
        checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers,
        showSemanticsDebugger: widget.showSemanticsDebugger,
        debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner,
890
        inspectorSelectButtonBuilder: _inspectorSelectButtonBuilder,
891 892
        shortcuts: widget.shortcuts,
        actions: widget.actions,
893
        restorationScopeId: widget.restorationScopeId,
894
        useInheritedMediaQuery: widget.useInheritedMediaQuery,
895 896 897 898 899 900
      );
    }

    return WidgetsApp(
      key: GlobalObjectKey(this),
      navigatorKey: widget.navigatorKey,
901
      navigatorObservers: widget.navigatorObservers!,
902 903 904 905
      pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) {
        return MaterialPageRoute<T>(settings: settings, builder: builder);
      },
      home: widget.home,
906
      routes: widget.routes!,
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
      initialRoute: widget.initialRoute,
      onGenerateRoute: widget.onGenerateRoute,
      onGenerateInitialRoutes: widget.onGenerateInitialRoutes,
      onUnknownRoute: widget.onUnknownRoute,
      builder: _materialBuilder,
      title: widget.title,
      onGenerateTitle: widget.onGenerateTitle,
      textStyle: _errorTextStyle,
      color: materialColor,
      locale: widget.locale,
      localizationsDelegates: _localizationsDelegates,
      localeResolutionCallback: widget.localeResolutionCallback,
      localeListResolutionCallback: widget.localeListResolutionCallback,
      supportedLocales: widget.supportedLocales,
      showPerformanceOverlay: widget.showPerformanceOverlay,
      checkerboardRasterCacheImages: widget.checkerboardRasterCacheImages,
      checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers,
      showSemanticsDebugger: widget.showSemanticsDebugger,
      debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner,
      inspectorSelectButtonBuilder: _inspectorSelectButtonBuilder,
      shortcuts: widget.shortcuts,
      actions: widget.actions,
929
      restorationScopeId: widget.restorationScopeId,
930
      useInheritedMediaQuery: widget.useInheritedMediaQuery,
931
    );
932 933 934 935 936
  }

  @override
  Widget build(BuildContext context) {
    Widget result = _buildWidgetApp(context);
937 938 939 940 941 942 943 944 945
    result = Focus(
      canRequestFocus: false,
      onKey: (FocusNode node, RawKeyEvent event) {
        if (event is! RawKeyDownEvent || event.logicalKey != LogicalKeyboardKey.escape)
          return KeyEventResult.ignored;
        return Tooltip.dismissAllToolTips() ? KeyEventResult.handled : KeyEventResult.ignored;
      },
      child: result,
    );
Ian Hickson's avatar
Ian Hickson committed
946
    assert(() {
947
      if (widget.debugShowMaterialGrid) {
948
        result = GridPaper(
Ian Hickson's avatar
Ian Hickson committed
949 950 951
          color: const Color(0xE0F9BBE0),
          interval: 8.0,
          divisions: 2,
952 953
          subdivisions: 1,
          child: result,
Ian Hickson's avatar
Ian Hickson committed
954 955 956
        );
      }
      return true;
957
    }());
958

959
    return ScrollConfiguration(
960
      behavior: widget.scrollBehavior ?? const MaterialScrollBehavior(),
961 962 963
      child: HeroControllerScope(
        controller: _heroController,
        child: result,
964
      ),
965
    );
966
  }
967
}