flutter - 按下按钮时如何在 Flutter 上更改文本样式
问题描述
有谁知道按下按钮时如何更改 Flutter 上的文本样式?
例如,我有这样的代码:
class _scanningState extends State<scan> {
String strText = 'ABCDEFG';
@override
Widget build(BuildContext context) {
return Scaffold(backgroundColor: Colors.blue,
body: new Column(
children: <Widget>[
new Text(strText),
new RaisedButton(
child: new Text('Button'),
onPressed: (){
//Change text style of strText()???
},
)
],
)
);
}
解决方案
class _scanningState extends State<scanning> {
bool pressed = true;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue,
body: new Column(
children: <Widget>[
new Text(strText,
style: pressed
? TextStyle(color: Colors.black)
: TextStyle(color:Colors.green),
),
new RaisedButton(
child: new Text(
'Change color'),
onPressed: () {
setState(() {
pressed = !pressed;
});
},
)
],
));
}
也许您想更改同级文本。概念是一样的。快乐飘飘
推荐阅读
- android - 如何延迟已经在可运行文件中的 Kotlin (Android Studio) 中的 if-else 执行?
- java - 如何删除 TextChangedListener?
- c# - C# HttpClient 自定义标头每个请求
- excel - 通过单击按钮根据计数增加值
- ios - 未调用 iOS 委托方法
- google-cloud-platform - Google BigQuery 的 HLL+ 精度
- javascript - 仅在 Travis CI 上使用扩展运算符构建失败
- php - Laravel 5.8 Show 方法的自定义路由
- php - php SoapClient wsdl SoapFault 异常:[HTTP] 无法连接到主机
- php - 将 PHP 脚本和 html 呈现在一个函数/视图中