Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

StatefulWidget (setState not work did't show )

import 'package:flutter/material.dart';
class CounterScreen extends StatefulWidget
{
  const CounterScreen({Key? key}) : super(key: key);
  

  @override
  State<CounterScreen> createState() {
    
    return _CounterScreenState();
  }
}


class _CounterScreenState extends State<CounterScreen> {
int number=1;

   @override
  Widget build(BuildContext context) {
  
  return Scaffold(
    appBar: AppBar(
      title: Text(
        "Counter"
      ),
    ),
    body: Center(

      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,

        children: [

          TextButton(onPressed: (){ number--;} , child: Text("Minus",)),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 10),
            child: Text("$number",style: TextStyle(color: Colors.black,fontSize: 30,),),
          ),
          TextButton(onPressed: (){ number++;print(number);}, child: Text("Plus",)),

        ],
      ),
    ),
  );
  }

}

>Solution :

you have to use setstate where you want to update the UI. so this line should be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TextButton(onPressed: (){ number--;} , child: Text("Minus",)),

Like this

TextButton(
        onPressed: () {
          setState(() {
            number--;
          });
        },
        child: const Text(
          "Minus",
        ));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading