flutter - Flutter 2.0 如何将提升按钮主题更改为看起来像 RaisedButton
问题描述
由于在 Flutter 2.0 中 RaisedButton 已更改为 ElevatedButton 我尝试为按钮创建一个默认主题,但我不知道该怎么做。
这是我放在 main() 中的内容
elevatedButtonTheme: ElevatedButtonThemeData(
style: ButtonStyle(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20))),
),
我收到以下错误
The argument type 'RoundedRectangleBorder' can't be assigned to the parameter type 'MaterialStateProperty<OutlinedBorder>'.
我不知道如何使用 MaterialStateProperty 参数,因为它没有给我任何关于如何使用它的线索。我阅读了文档,但没有关于此的示例。
有人知道吗?
解决方案
您可以参考此文档以ButtonStyle
了解MaterialStateProperty
要使用哪些字段。
例如,要定义形状,可以使用以下代码:
shape: MaterialStateProperty.resolveWith<OutlinedBorder>((_) {
return RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)))
);
}),
完整示例:
ElevatedButtonTheme(
data: ElevatedButtonThemeData(
style: ButtonStyle(
side: MaterialStateProperty.resolveWith<BorderSide>(
(states) => BorderSide(color: borderColor ?? Colors.black)),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(states) => Colors.white),
shape: MaterialStateProperty.resolveWith<OutlinedBorder>((_) {
return RoundedRectangleBorder(borderRadius: BorderRadius.circular(20));
}),
textStyle: MaterialStateProperty.resolveWith<TextStyle>(
(states) => TextStyle(color: Colors.red)),
),
),
child: ElevatedButton(onPressed: () {}, child: Text('label')),
);
推荐阅读
- java - 如何在我创建的目录中创建文本文件(Android 外部存储)
- java - 如何从存储为文件的 Parse-server 加载图像并使用 Java 将其显示到 Android-Studio 中的自定义 ListView?
- python - 斐波那契数列/数动态规划
- reactjs - state' 未在 App.js 文件中定义 no-undef
- c# - 使用 Raycast 代替 Gaze Pointer
- javascript - Promise 之后的触发事件
- react-native - 如何更改设备位置?
- python - 如何从 json 创建嵌套 json 并用单个键替换不同的键值
- javascript - 这是使用面向对象方法在 Javascript 中编写这个小型计算器的最佳方法吗?
- shell - 用另一个类似的替换行的脚本