页面过渡效果由 ZoomPageTransitionsBuilder 替换
概述
#为了确保库遵循最新的 OEM 行为,默认的页面过渡构建器现在将所有平台(iOS 和 macOS 除外)的默认值从 FadeUpwardsPageTransitionsBuilder
更改为 ZoomPageTransitionsBuilder
。
背景
#FadeUpwardsPageTransitionsBuilder
(随 Flutter 首次发布提供)定义了与 Android O 提供的页面过渡效果类似的页面过渡。根据 Flutter 的弃用政策,此页面过渡构建器最终将在 Android 上被弃用。
ZoomPageTransitionsBuilder
是 Android、Linux 和 Windows 的新页面过渡构建器,它定义了一种与 Android Q 和 R 提供的页面过渡效果类似的页面过渡。
根据Flutter 仓库的风格指南,框架将遵循最新的 OEM 行为。所有使用 FadeUpwardsPageTransitionsBuilder
的页面过渡构建器都已切换到 ZoomPageTransitionsBuilder
。当当前 TargetPlatform
在 ThemeData.pageTransitionsTheme
中没有定义 PageTransitionsBuilder
时,将使用 ZoomPageTransitionsBuilder
作为默认值。
变更说明
#在 PageTransitionsTheme._defaultBuilders
中定义的 PageTransitionsBuilder
已从 FadeUpwardsPageTransitionsBuilder
更改为 ZoomPageTransitionsBuilder
,适用于 TargetPlatform.android
、TargetPlatform.linux
和 TargetPlatform.windows
。
迁移指南
#如果您想切换回之前的页面过渡构建器(FadeUpwardsPageTransitionsBuilder
),您应该为目标平台显式定义构建器。
迁移前的代码
MaterialApp(
theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple)),
)
迁移后的代码
MaterialApp(
theme: ThemeData(
pageTransitionsTheme: const PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(), // Apply this to every platforms you need.
},
),
),
)
如果您想将相同的页面过渡构建器应用于所有平台
MaterialApp(
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: Map<TargetPlatform, PageTransitionsBuilder>.fromIterable(
TargetPlatform.values,
value: (dynamic _) => const FadeUpwardsPageTransitionsBuilder(),
),
),
),
)
测试迁移
#如果您在使用新过渡效果时尝试查找 widget 但因“元素过多”而失败,并看到类似以下的错误
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following StateError was thrown running a test:
Bad state: Too many elements
When the exception was thrown, this was the stack:
#0 Iterable.single (dart:core/iterable.dart:656:24)
#1 WidgetController.widget (package:flutter_test/src/controller.dart:69:30)
#2 main.<anonymous closure> (file:///path/to/your/test.dart:1:2)
您应该通过为 Finder
使用 descendant
范围并指定 widget 类型来迁移您的测试。下面是 DataTable
测试的示例
迁移前测试
final Finder finder = find.widgetWithIcon(Transform, Icons.arrow_upward);
迁移后测试
final Finder finder = find.descendant(
of: find.byType(DataTable),
matching: find.widgetWithIcon(Transform, Icons.arrow_upward),
);
通常需要迁移 finder 范围的 widget 包括:Transform
、FadeTransition
、ScaleTransition
和 ColoredBox
。
时间线
#包含于版本:2.13.0-1.0.pre
稳定版本:3.0.0
参考资料
#API 文档
相关问题
相关 PR