4

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!

2
  • can you share complete code in which you're calling this RaisedButton ? Commented Dec 14, 2019 at 11:11
  • where did you define this variable? bool pressGeoON = false; bool cmbscritta = false; Commented Dec 14, 2019 at 11:21

4 Answers 4

14

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;
              });
            }
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

6

For this bool pressGeoON = false; bool cmbscritta = false; should be global variable of your widget and it will start working.

3 Comments

wowww correct :) can explain how? not write after @ovverride Widget build(BuildContext context) { }
invoking setState() will call build method of your widget and the values of bool pressGeoON = false; bool cmbscritta = false; will get reset. so these variable should be out of your build method. so that their values can be persisted.
Print log in your build method's first line and press button you will see that when you call setState() the build method will also be called.
3

You should simplify your onPressed as:

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

and make sure your widget class is StatefulWidget not StatelessWidget

Comments

0

Simple You can make One variable

var name = "safal"

After that make one method

void changeName(){
 setState(() {
 name = "Sahil";
 });
}

Set text in your Text

Text(
 name,
 style: const TextStyle(
  fontSize: 14,
  fontFamily:
  "Roboto Font Family",
  fontWeight: FontWeight.w400,
  color: grey_color),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.