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();
相关推荐
coooliang15 分钟前
【Android】ViewPager的使用
android
xvch2 小时前
Kotlin 2.1.0 入门教程(二十五)类型擦除
android·kotlin
OKXLIN6 小时前
IOS UITextField 无法隐藏键盘问题
ios·objective-c
simplepeng11 小时前
我的天,我真是和androidx的字体加载杠上了
android
小猫猫猫◍˃ᵕ˂◍12 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
AL.千灯学长14 小时前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
CYRUS_STUDIO14 小时前
使用 AndroidNativeEmu 调用 JNI 函数
android·逆向·汇编语言
梦否14 小时前
【Android】类加载器&热修复-随记
android
徒步青云14 小时前
Java内存模型
android
今阳15 小时前
鸿蒙开发笔记-6-装饰器之@Require装饰器,@Reusable装饰器
android·app·harmonyos