返回
助力跨平台应用开发利器:Flutter MaterialApp控件详解
见解分享
2024-02-20 05:17:30
MaterialApp:构建应用的基础
MaterialApp 是 Flutter 中一个非常重要的控件,它是构建整个应用程序的基础。它为应用程序提供了必要的配置信息,如应用程序的标题、主题、导航和路由等。
定义与功能
MaterialApp 的定义如下:
class MaterialApp extends StatelessWidget {
const MaterialApp({
Key? key,
this.title = '',
this.home,
this.theme,
this.darkTheme,
this.themeMode = ThemeMode.system,
this.locale,
this.localizationsDelegates,
this.localeListResolutionCallback,
this.supportedLocales = const <Locale>[Locale('en', 'US')],
this.debugShowCheckedModeBanner = true,
this.shortcuts,
this.actions,
this.restorationScopeId,
this.scrollBehavior,
}) : super(key: key);
/// The title of the application.
///
/// This title is used for accessibility purposes. It appears in
/// the operating system task switcher and other application
/// metadata requests.
///
/// See [debugDefaultTargetPlatformOverride] for a way to override
/// the default title for testing.
final String title;
/// The widget for the default route of the application.
///
/// The application is considered to be stateless until the first
/// frame for the given widget is built.
///
/// See [Navigator.push] for the semantics of this widget and more
/// explanations about its relationship with [Navigator].
///
/// In a typical Material application, this will be a
/// [Scaffold] widget that contains other routes as its children.
final Widget? home;
/// The overall theme of the application.
///
/// The initial theme for the application. When this is set, a
/// [Theme] widget will be created based on this theme and
/// automatically included at the root of the widget tree, which will
/// affect all the descendant widgets.
///
/// This theme can be overridden for a specific subtree of widgets
/// by providing a [Theme] widget with a different theme closer
/// in the widget tree.
///
/// Typically this will be a [ThemeData] object, but a [Theme]
/// object may also be provided.
final ThemeData? theme;
/// The overall dark theme of the application.
///
/// The initial dark theme for the application. When this is set, a
/// [Theme] widget will be created based on this theme and
/// automatically included at the root of the widget tree, which will
/// affect all the descendant widgets, when the [themeMode] is set to
/// [ThemeMode.dark] or [ThemeMode.system] and the platform is in dark
/// mode.
///
/// This theme can be overridden for a specific subtree of widgets
/// by providing a [Theme] widget with a different theme closer
/// in the widget tree.
///
/// Typically this will be a [ThemeData] object, but a [Theme]
/// object may also be provided.
final ThemeData? darkTheme;
/// The mode that determines which theme to use.
///
/// If [darkTheme] is set, the current theme will depend on the
/// [ThemeMode] that's used. If [themeMode] is [ThemeMode.system],
/// the current theme will be [darkTheme] if the platform is in dark
/// mode, otherwise [theme]. If [themeMode] is [ThemeMode.light], the
/// current theme will always be [theme]. If [themeMode] is
/// [ThemeMode.dark], the current theme will always be [darkTheme].
///
/// The effective theme can be overridden for a specific subtree of
/// widgets by providing a [Theme] widget with a different [themeMode]
/// closer in the widget tree.
///
/// Defaults to [ThemeMode.system].
///
/// This theme mode can also be controlled using the
/// [MediaQueryData.platformBrightness] parameter of [MediaQueryData]
/// or [ThemeData.platformBrightness].
final ThemeMode themeMode;
/// The locale of the application.
///
/// This locale is used to determine the language and formatting
/// conventions used in the application, as well as the
/// measurements and directionality used.
final Locale? locale;
/// The delegates for providing localized resources for this
/// application.
///
/// See [Localizations] for more information.
///
/// In this list, later delegates will take precedence over earlier
/// ones, as will entries in the [GlobalMaterialLocalizations]. The
/// entries from the [GlobalMaterialLocalizations] can be overridden
/// by providing a [MaterialLocalizations] instance in this list.
///
/// The default value of this list includes [DefaultMaterialLocalizations]
/// and [GlobalMaterialLocalizations].
///
/// This list must not contain null entries.
final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates;
/// The callback used to resolve the locale list of this
/// application.
///
/// This callback provides a way to override the locale list which can
/// be resolved by the [Localizations] object.
///
/// It should return a list of locales which can be used by this
/// application and which can also be used in the [localeListResolutionCallback]
/// of the [WidgetsApp] or the [CupertinoApp] which creates this
/// [MaterialApp].
final LocaleListResolutionCallback? localeListResolutionCallback;
/// The list of locales which this application should support.
///
/// This list must not contain null entries. It must also contain all
/// the locales which are present in [GlobalMaterialLocalizations].
///
/// If the [locale] property is specified, then it must also be
/// present in this list.
///
/// If the [localizationsDelegates] property is specified, then their
/// locales must also be present in this list.
///
/// The default value is a list containing a single entry for the locale
/// 'en_US'.
final List<Locale> supportedLocales;
/// Whether to display a banner indicating that the application is
/// running in debug mode.
///
/// By default, a debug banner is displayed above the [home] widget
/// when Flutter is in debug mode, i.e. when asserts are enabled.
///
/// This banner will be displayed even if a [debugDefaultTargetPlatformOverride]
/// is set, although the banner will contain the name of the overridden
/// platform.
final bool debugShowCheckedModeBanner;
/// The list of shortcuts defined in this application.
///
/// See [WidgetsApp.shortcuts] for more information.
final List<ShortcutActivator>? shortcuts;
/// The list of actions defined in this application.
///
/// See [WidgetsApp.actions] for more information.
final List<Action<Intent>>? actions;
/// The ID used to identify the restoration scope associated with
/// this application.
///
/// When a [Navigator] is pushed into the [WidgetsApp] created by
/// this application, a new restoration scope is created with the
/// specified ID.
///
/// See [RestorationScope] for more information.
final String? restorationScopeId;
/// The scroll behavior used for this application.
///
/// By default, [ScrollConfiguration.of(context)] will return a
/// [ScrollConfiguration] with [ScrollBehavior] of type
/// [GlowingOverscrollIndicator], which shows an overscroll glow when
/// the scrollable widget (e.g. ListView, SingleChildScrollView, etc.)
/// is scrolled past its edge.
///
/// You can customize the [ScrollBehavior] for this application
/// by providing a [ScrollBehavior] subclass in this property.
final ScrollBehavior? scrollBehavior;
}
从该定义可以看出,MaterialApp 控件的构造函数中包含了许多参数,这些参数主要用于配置应用程序的标题、主题、语言、路由等信息。
属性与方法
MaterialApp 控件拥有丰富的属性和方法,常用的属性如下:
- title :应用程序的标题,显示在