TextSelectionTheme 迁移
概述
#用于控制 Material Widget 中选中文字外观的 ThemeData
属性已移至其自身的 TextSelectionTheme
中。这些属性包括 cursorColor
、textSelectionColor
和 textSelectionHandleColor
。这些属性的默认值也已更改,以匹配 Material Design 规范。
背景
#作为更大范围的 Material 主题更新 的一部分,我们引入了一个新的 Text Selection Theme,用于指定 TextField
和 SelectableText
Widget 中选中文字的属性。这些属性取代了 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),
)
)
如果您对新的默认值感到满意,但黄金文件测试失败,您可以使用以下命令更新您的主黄金文件。
flutter test --update-goldens
时间线
#已于版本:1.23.0-4.0.pre 登录
稳定版本:2.0.0
参考资料
#API 文档
相关 PR