Flutter开发之CupertinoApp

Flutter开发之CupertinoApp

最近由于使用Flutter编程更多,使用Flutter更顺手,相对于其他前端框架来说,Flutter在跨平台、响应式UI、自绘引擎、即插即用的组件和庞大的社区生态支持方面有更大的优势;Flutter拥有更低的学习成本,更高的开发效率和比SwiftUI稍低的渲染性能。

在使用Flutter开发iOS风格的应用时,不可避免的使用到的组件就是CupertinoApp,它一般是应用的入口组件。

一、CupertinoApp基础

CupertinoApp在Flutter中时继承自StatefulWidget,为状态可变的组件,初始化方法为:

dart 复制代码
const CupertinoApp({
    super.key,
    this.navigatorKey,
    this.home,
    this.theme,
    Map<String, Widget Function(BuildContext)> this.routes = const <String, WidgetBuilder>{},
    this.initialRoute,
    this.onGenerateRoute,
    this.onGenerateInitialRoutes,
    this.onUnknownRoute,
    List<NavigatorObserver> this.navigatorObservers = const <NavigatorObserver>[],
    this.builder,
    this.title = '',
    this.onGenerateTitle,
    this.color,
    this.locale,
    this.localizationsDelegates,
    this.localeListResolutionCallback,
    this.localeResolutionCallback,
    this.supportedLocales = const <Locale>[Locale('en', 'US')],
    this.showPerformanceOverlay = false,
    this.checkerboardRasterCacheImages = false,
    this.checkerboardOffscreenLayers = false,
    this.showSemanticsDebugger = false,
    this.debugShowCheckedModeBanner = true,
    this.shortcuts,
    this.actions,
    this.restorationScopeId,
    this.scrollBehavior,
    @Deprecated(
      'Remove this parameter as it is now ignored. '
      'CupertinoApp 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,
  }) : routeInformationProvider = null,
       routeInformationParser = null,
       routerDelegate = null,
       backButtonDispatcher = null,
       routerConfig = null;

const CupertinoApp.router({
    super.key,
    this.routeInformationProvider,
    this.routeInformationParser,
    this.routerDelegate,
    this.backButtonDispatcher,
    this.routerConfig,
    this.theme,
    this.builder,
    this.title = '',
    this.onGenerateTitle,
    this.color,
    this.locale,
    this.localizationsDelegates,
    this.localeListResolutionCallback,
    this.localeResolutionCallback,
    this.supportedLocales = const <Locale>[Locale('en', 'US')],
    this.showPerformanceOverlay = false,
    this.checkerboardRasterCacheImages = false,
    this.checkerboardOffscreenLayers = false,
    this.showSemanticsDebugger = false,
    this.debugShowCheckedModeBanner = true,
    this.shortcuts,
    this.actions,
    this.restorationScopeId,
    this.scrollBehavior,
    @Deprecated(
      'Remove this parameter as it is now ignored. '
      'CupertinoApp 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(routerDelegate != null || routerConfig != null),
       navigatorObservers = null,
       navigatorKey = null,
       onGenerateRoute = null,
       home = null,
       onGenerateInitialRoutes = null,
       onUnknownRoute = null,
       routes = null,
       initialRoute = null;

组件中可以设置主题、路由、国际化等基础功能,已经满足了绝大部分应用开发的需求。

二、CupertinoApp中的属性

dart 复制代码
/// 全局的导航组件key
final GlobalKey<NavigatorState>? navigatorKey;
/// 入口页面
final Widget? home;
/// iOS风格主题,可设置对应的主题参数控制页面展示
final CupertinoThemeData? theme;
/// 顶级路由表,通过字符串定位路由
final Map<String, WidgetBuilder>? routes;
/// 初始路由
final String? initialRoute;
/// 路由封装
final RouteFactory? onGenerateRoute;
final InitialRouteListFactory? onGenerateInitialRoutes;
final RouteFactory? onUnknownRoute;
/// 导航监听
final List<NavigatorObserver>? navigatorObservers;
/// 路由状态变化
final RouteInformationProvider? routeInformationProvider;
/// 路由状态解析
final RouteInformationParser<Object>? routeInformationParser;
/// 路由代理
final RouterDelegate<Object>? routerDelegate;
/// 返回按钮调度,控制返回按钮优先级
final BackButtonDispatcher? backButtonDispatcher;
/// 路由配置,与路由代理互斥
final RouterConfig<Object>? routerConfig;
/// 路由过渡动画
final TransitionBuilder? builder;
/// 标题
final String title;
final GenerateAppTitle? onGenerateTitle;
/// 颜色
final Color? color;
/// 本地语言
final Locale? locale;
/// 国际化代理
final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates;
/// 国际化解析回调
final LocaleListResolutionCallback? localeListResolutionCallback;
final LocaleResolutionCallback? localeResolutionCallback;
/// 支持的语言
final Iterable<Locale> supportedLocales;
/// 是否打开性能监测
final bool showPerformanceOverlay;
/// 是否打开栅格化缓存图片
final bool checkerboardRasterCacheImages;
/// 是否打开离屏渲染
final bool checkerboardOffscreenLayers;
/// 调试信息展示
final bool showSemanticsDebugger;
final bool debugShowCheckedModeBanner;
/// 组件快捷方式,可以在子组件之间共享
final Map<ShortcutActivator, Intent>? shortcuts;
/// 触发快捷方式,可以在子组件之间共享
final Map<Type, Action<Intent>>? actions;
/// 恢复id,与状态管理相关
final String? restorationScopeId;
/// 滚动行为
final ScrollBehavior? scrollBehavior;
/// MediaQuery相关,状态开关,可用于拦截
final bool useInheritedMediaQuery;
/// iOS风格Hero动画控制器
static HeroController createCupertinoHeroController() =>
      HeroController();
相关推荐
Clockwiseee22 分钟前
PHP之伪协议
android·开发语言·php
小林爱28 分钟前
【Compose multiplatform教程08】【组件】Text组件
android·java·前端·ui·前端框架·kotlin·android studio
小何开发2 小时前
Android Studio 安装教程
android·ide·android studio
开发者阿伟2 小时前
Android Jetpack LiveData源码解析
android·android jetpack
weixin_438150992 小时前
广州大彩串口屏安卓/linux触摸屏四路CVBS输入实现同时显示!
android·单片机
CheungChunChiu3 小时前
Android10 rk3399 以太网接入流程分析
android·framework·以太网·eth·net·netd
木头没有瓜3 小时前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp
键盘侠0073 小时前
springboot 上传图片 转存成webp
android·spring boot·okhttp
江上清风山间明月4 小时前
flutter bottomSheet 控件详解
android·flutter·底部导航·bottomsheet
Crossoads6 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言