按下按钮时如何在Flutter上更改文本样式

Ari*_*owo 7 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()???
               },
              )
             ],
            )
          );
         }
Run Code Online (Sandbox Code Playgroud)

BIN*_*GAR 11

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;
                });
              },
            )
          ],
        ));
  }
Run Code Online (Sandbox Code Playgroud)

也许你想改变兄弟文本.这个概念是一样的.快乐的颤振


die*_*per 5

我建议您阅读有关状态小工具的信息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;
                    });
                  },
                )
              ],
            ));
      }
Run Code Online (Sandbox Code Playgroud)