Home > Enterprise >  Flutter toggle a number by tapping on it
Flutter toggle a number by tapping on it

Time:09-29

Imagine a stock portfolio app with a large headline number at the top.

How can I code a toggle command in such that the number, by tapping on it, would result in a switch between 2 numbers? (it is basically just another number that is of different computation)

Thank you in advance.

CodePudding user response:

I am using int here, you can use bool to switch between true and false

class TeNA extends StatefulWidget {
  const TeNA({super.key});

  @override
  State<TeNA> createState() => _TeNAState();
}

class _TeNAState extends State<TeNA> {
  int number = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            if (number == 0) {
              number = 1;
            } else {
              number = 0;
            }
            setState(() {});
          },
          child: Text("number $number"),
        ),
      ),
    );
  }
}
  • Related