有人知道如何在按下按钮时更改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()???
},
)
],
)
);
}
答案 0 :(得分:10)
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;
});
},
)
],
));
}
也许您想更改兄弟文本。概念是相同的。快乐扑动
答案 1 :(得分:4)
我建议您阅读有关有状态小部件https://flutter.io/tutorials/interactive/#stateful-stateless
的信息 class _scanningState extends State<scanning> {
bool pressed = false;
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',
style: pressed
? TextStyle(fontSize: 30.0)
: TextStyle(fontSize: 10.0),
),
onPressed: () {
//Change text style of strText()???
setState(() {
pressed = !pressed;
});
},
)
],
));
}