TextSelectionTheme 迁移
概述
#控制 Material 组件中选中文本外观的 ThemeData
属性已移至其独立的 TextSelectionTheme
中。这些属性包括 cursorColor
、textSelectionColor
和 textSelectionHandleColor
。这些属性的默认值也已更改,以符合 Material Design 规范。
背景
#作为更大型的Material 主题更新的一部分,我们引入了一个新的文本选择主题(Text Selection Theme),用于指定 TextField
和 SelectableText
组件中选中文本的属性。它们取代了 ThemeData
的几个顶级属性,并更新了它们的默认值以符合 Material Design 规范。本文档描述了应用程序如何迁移到此新 API。
迁移指南
#如果您当前正在使用 ThemeData
的以下属性,您需要将它们更新为使用 ThemeData.textSelectionTheme
上的新等效属性
之前 | 之后 |
---|---|
ThemeData.cursorColor | TextSelectionThemeData.cursorColor |
ThemeData.textSelectionColor | TextSelectionThemeData.selectionColor |
ThemeData.textSelectionHandleColor | TextSelectionThemeData.selectionHandleColor |
迁移前的代码
dart
ThemeData(
cursorColor: Colors.red,
textSelectionColor: Colors.green,
textSelectionHandleColor: Colors.blue,
)
迁移后的代码
dart
ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: Colors.red,
selectionColor: Colors.green,
selectionHandleColor: Colors.blue,
)
)
默认值更改
如果您没有明确使用这些属性,但依赖于之前用于文本选择的默认颜色,您可以在 ThemeData
中添加一个新字段,以便您的应用恢复到旧的默认值,如下所示
dart
// Old defaults for a light theme
ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: const Color.fromRGBO(66, 133, 244, 1.0),
selectionColor: const Color(0xff90caf9),
selectionHandleColor: const Color(0xff64b5f6),
)
)
dart
// Old defaults for a dark theme
ThemeData(
textSelectionTheme: TextSelectionThemeData(
cursorColor: const Color.fromRGBO(66, 133, 244, 1.0),
selectionColor: const Color(0xff64ffda),
selectionHandleColor: const Color(0xff1de9b6),
)
)
如果您对新默认值满意,但金文件测试(golden file tests)失败,您可以使用以下命令更新您的主金文件(master golden files)
flutter test --update-goldens
时间线
#已发布版本: 1.23.0-4.0.pre
稳定版本:2.0.0
参考资料
#API 文档
相关 PR