![](/img/trans.png)
[英]How can I change the textbutton color in the flutter showAboutDialog?
[英]How can I change the background color of a textbutton in flutter?
我正在尝试将我的FlatButton
迁移到TextButton
。 因为自从我升级了 flutter 版本后, FlatButtons
已被弃用。 我目前正在努力调整背景颜色。
旧按钮:
FlatButton(
height: height,
onPressed: onPressed,
shape: baseButtonBorder,
color: Colors.red,
child: Text(label, style: TextStyle(color: fontColor, fontWeight: boldLabel ? FontWeight.bold : FontWeight.normal)),
)`
新按钮:
TextButton(
onPressed: onPressed,
style: ButtonStyle(backgroundColor: Colors.red), // <-- Does not work
child: Text(label, style: TextStyle(color: fontColor, fontWeight: boldLabel ? FontWeight.bold : FontWeight.normal)),
),
平面按钮没有color
属性,所以我尝试使用style
属性并添加一个ButtonStyle
。 dart 说:
The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color>'.
如何像以前对 FlatButton 所做的那样使用红色设置TextButton
的FlatButton
? 我需要用红色创建MaterialStateProperty<Color>
吗?
backgroundColor
属性是MaterialStateProperty<Color?>
类型。 您可以查看Flutter 文档。
所以你必须使用MaterialStateProperty
class 来应用颜色。 一个简单的例子:
TextButton(
child: Text('test'),
style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.red)),
onPressed: () {},
),
对于寻找更清晰、更简单的方法的人,您可以使用TextButton.styleFrom()
。 例子:
TextButton(
child: Text('Example'),
onPressed: () {},
style: TextButton.styleFrom(backgroundColor: Colors.red),
)
您可以在 styleFrom 中自定义几乎任何您想要的东西。 这也适用于其他按钮,例如ElevatedButton
和OutlinedButton
。
试试这个方法
TextButton(
onPressed: () {},
child: Container(
padding: EdgeInsets.fromLTRB(30, 10, 30, 10),
color: Colors.red,
child: Text(""),
),
)
向Textbutton
添加背景颜色的最简单方法
TextButton(
style: TextButton.styleFrom(backgroundColor: Colors.red),
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.