Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a text style on Flutter when button pressed

Tags:

flutter

Is anyone know how to change a text style on Flutter when the button pressed?

As example, i have a code like this :

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()???
               },
              )
             ],
            )
          );
         }
like image 203
Arif Wibowo Avatar asked Aug 18 '18 01:08

Arif Wibowo


1 Answers

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

Maybe you want to change the sibling text. The concept is the same. Happy Flutter

like image 73
BINAY THAPA MAGAR Avatar answered Oct 21 '22 12:10

BINAY THAPA MAGAR