文章已同步至掘金:https://juejin.cn/post/6844903956234141704
欢迎访问😃,有任何问题都可留言评论哦~
Flutter 控件中的 MaterialApp
作为App的顶层主页入口,一般是用来配置主题、语言、页面跳转等…
MaterialApp
属性
参数 | 类型 |
---|---|
navigatorKey(导航键) | GlobalKey |
home(主页) | Widget |
routes(路由) | Map<String, WidgetBuilder> |
initialRoute(初始路由) | String |
onGenerateRoute(生成路由) | RouteFactory |
onUnknownRoute(未知路由) | RouteFactory |
navigatorObservers(导航观察器) | List |
builder(建造者) | TransitionBuilder |
title(标题) | String |
onGenerateTitle(生成标题) | GenerateAppTitle |
color(颜色) | Color |
theme(主题) | ThemeData |
locale(地点) | Locale |
localizationsDelegates(本地化委托) | Iterable<LocalizationsDelegate> |
localeResolutionCallback(区域分辨回调) | LocaleResolutionCallback |
supportedLocales(支持区域) | Iterable |
debugShowMaterialGrid(调试显示材质网格) | bool |
showPerformanceOverlay(显示性能叠加) | bool |
checkerboardRasterCacheImages(棋盘格光栅缓存图像) | bool |
checkerboardOffscreenLayers(棋盘格层) | bool |
showSemanticsDebugger(显示语义调试器) | bool |
debugShowCheckedModeBanner(调试显示检查模式横幅) | bool |
源码示例
构造函数如下:
属性解释
1.navigatorKey
navigatorKey.currentState
相当于 Navigator.of(context)
使用:
2.home
进入程序后显示的第一个页面,传入的是一个Widget
,但实际上这个Widget
需要包裹一个Scaffold
以显示该程序使用Material Design
风格
使用:
3.routes
声明程序中有哪个通过Navigation.of(context).pushNamed
跳转的路由
参数以键值对的形式传递 key:路由名字 value:对应的Widget
使用:
4.initialRoute
初始路由,当用户进入程序时,自动打开对应的路由。
使用:
5.onGenerateRoute
当通过Navigation.of(context).pushNamed
跳转路由时, 在routes
查找不到时,会调用该方法
使用:
6.onUnknownRoute
效果跟onGenerateRoute
一样 调用顺序为onGenerateRoute
--> onUnknownRoute
7.navigatorObservers
路由观察器,当调用Navigator的相关方法时,会回调相关的操作
使用:
8.builder
当构建一个Widget
前调用 一般做字体大小,方向,主题颜色等配置
使用:
9.title
该标题出现在 :
- Android:任务管理器的程序快照之上
- IOS: 程序切换管理器中
使用:
10.onGenerateTitle
跟上面的tiitle
一样,但含有一个context
参数 用于做本地化
使用:
11.color
该颜色为Android中程序切换中应用图标背景的颜色
使用:
12.theme
应用程序的主题,各种的定制颜色都可以设置,用于程序主题切换
使用:
13.locale
当前区域,如果为null
则使用系统区域 一般用于语言切换
使用:
14.localizationsDelegates
本地化委托,用于更改Flutter Widget
默认的提示语,按钮text
等
使用:
15.localeResolutionCallback
当传入的是不支持的语种,可以根据这个回调,返回相近,并且支持的语种
使用:
16.supportedLocales
传入支持的语种数组
17.debugShowMaterialGrid
debug模式下是否显示材质网格,传入bool
类型,一般不适用
18.showPerformanceOverlay
当为true
时应用程序顶部覆盖一层GPU
和UI
曲线图,可即时查看当前流畅度情况
19.checkerboardRasterCacheImages
当为true
时,打开光栅缓存图像的棋盘格
20.checkerboardOffscreenLayers
当为true
时,打开呈现到屏幕位图的层的棋盘格
21.showSemanticsDebugger
当为true
时,打开Widget
边框,类似Android开发者模式中显示布局边界
22.debugShowCheckedModeBanner
当为true
时,在debug
模式下显示右上角的debug
字样的横幅,false
即为不显示
评论区