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();
相关推荐
数据猎手小k17 分钟前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小101 小时前
JavaWeb项目-----博客系统
android
风和先行1 小时前
adb 命令查看设备存储占用情况
android·adb
AaVictory.2 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰3 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶3 小时前
Android——网络请求
android
干一行,爱一行3 小时前
android camera data -> surface 显示
android
断墨先生3 小时前
uniapp—android原生插件开发(3Android真机调试)
android·uni-app
无极程序员5 小时前
PHP常量
android·ide·android studio
萌面小侠Plus6 小时前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机