首页 > 解决方案 > 按下按钮时如何在 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()???
               },
              )
             ],
            )
          );
         }

标签: flutter

解决方案


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

也许您想更改同级文本。概念是一样的。快乐飘飘


推荐阅读