小编典典

按下按钮时如何在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()???
               },
              )
             ],
            )
          );
         }

阅读 987

收藏
2020-08-13

共1个答案

小编典典

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;
                });
              },
            )
          ],
        ));
  }

也许您想更改兄弟文本。概念是相同的。快乐扑

2020-08-13