Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter change text when button pressed

Tags:

flutter

dart

yo guys i'll try to change text at button when clicked on...

my code :

         bool pressGeoON = false;
         bool cmbscritta = false;
           RaisedButton(
                  shape: new RoundedRectangleBorder(
                      borderRadius: new BorderRadius.circular(18.0),
                      side: BorderSide(color: Colors.red)),
                  color: pressGeoON ? Colors.blue: Colors.red,
                  textColor: Colors.white,
                  child:  cmbscritta ? Text("GeoOn"): Text("GeoOFF"),
                  //    style: TextStyle(fontSize: 14)

                  onPressed: () {
                    setState(() => pressGeoON = !pressGeoON);
                    setState(() => cmbscritta = !cmbscritta);
                  },
                )

No advice from dart Analisys but not work...help!

like image 758
Daniele Angelini Avatar asked Nov 30 '22 13:11

Daniele Angelini


1 Answers

your class must be stateful to change state of activity

also the variable must be declared globally

class MyClass extends StatefulWidget {
  @override
  _MyClassState createState() => _MyClassState();
}

class _MyClassState extends State<MyClass> {
  bool pressGeoON = false;
  bool cmbscritta = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          shape: new RoundedRectangleBorder(
              borderRadius: new BorderRadius.circular(18.0),
              side: BorderSide(color: Colors.red)),
          color: pressGeoON ? Colors.blue : Colors.red,
          textColor: Colors.white,
          child: cmbscritta ? Text("GeoOn") : Text("GeoOFF"),
          //    style: TextStyle(fontSize: 14)
            onPressed: () {
              setState(() {
                pressGeoON = !pressGeoON;
                cmbscritta = !cmbscritta;
              });
            }
        ),
      ),
    );
  }
}
like image 70
Jay Gadariya Avatar answered Dec 03 '22 03:12

Jay Gadariya