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();
相关推荐
QING6182 小时前
【LeakCanary】的实现原理与调优技巧
android·性能优化·app
QING6182 小时前
Android LruCache 与 DiskLruCache 深度解析
android·kotlin·app
依旧风轻4 小时前
深入理解 rsync daemon 模式(守护进程)
linux·ios·rsync·daemon·sqi
alexhilton4 小时前
Compose Multiplatform支持热重载(Hot Reload)了
android·kotlin·android jetpack
若丶相见4 小时前
Jetpack Compose 和 Android View 之间的对应关系
android
好奇的菜鸟5 小时前
Scoop + Kotlin 极简开发环境搭建指南
android·开发语言·kotlin
leluckys5 小时前
swift-oc和swift block和代理
开发语言·ios·swift
吃饭了呀呀呀5 小时前
🐳 《Android》 安卓开发教程 -体检 必填校验质控 弹窗
android
fuyinghaha7 小时前
android 14.0 工厂模式 测试音频的一些问题(高通)
android·音视频
测试萧十一郎7 小时前
APP测试中ios和androis的区别,有哪些注意点
自动化测试·软件测试·功能测试·macos·ios·职场和发展·cocoa