How to refresh text in flutter

Viewed 690

I have this Text

enter image description here

I want when I clicked on add Icon to refresh 30 to 31

this is my code :

Row(
                      children: <Widget>[
                        Expanded(
                          child: Container(
                            height: 30.0,
                            width: 30.0,
                            child: Image.asset('assets/images/trash_can.png'),
                          ),
                        ),
                        Expanded(
                          child: Center(
                            child: Text(
                              mealsNum.toString(),
                              style: TextStyle(
                                fontSize: 20.0
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: GestureDetector(
                            onTap: (){
                              mealsNum++;
                              print(mealsNum);
                            },
                            child: Icon(
                              Icons.add,
                              color: Color(0xffFFD243),
                            ),
                          ),
                        ),
                      ],
                    ),

How can I do this?

4 Answers

use a set state to update

 onTap: (){
    setState((){                                    
mealsNum++;
});

  print(mealsNum);,

You need to add setState in onTap

Row(
  children: <Widget>[
    Expanded(
      child: Container(
        height: 30.0,
        width: 30.0,
        child: Image.asset('assets/images/trash_can.png'),
      ),
    ),
    Expanded(
      child: Center(
        child: Text(
          mealsNum.toString(),
          style: TextStyle(
            fontSize: 20.0
          ),
        ),
      ),
    ),
    Expanded(
      child: GestureDetector(
        onTap: (){
          setState(() {
            mealsNum++;
          });
          print(mealsNum);
        },
        child: Icon(
          Icons.add,
          color: Color(0xffFFD243),
        ),
      ),
    ),
  ],
),

setSate is used for rebuilding the widgets. Add setstate in onTap

Row(
  children: <Widget>[
    Expanded(
      child: Container(
        height: 30.0,
        width: 30.0,
        child: Image.asset('assets/images/trash_can.png'),
      ),
    ),
    Expanded(
      child: Center(
        child: Text(
          mealsNum.toString(),
          style: TextStyle(
            fontSize: 20.0
          ),
        ),
      ),
    ),
    Expanded(
      child: GestureDetector(
        onTap: (){
          setState(() {
            mealsNum++;
          });
          print(mealsNum);
        },
        child: Icon(
          Icons.add,
          color: Color(0xffFFD243),
        ),
      ),
    ),
  ],
),

you should learn state management in flutter here is the link to learn the basic from flutter basic doc after you read this you maybe ask what about global state or more complicated state mangement then you can choose provider or for more advance use flutter_block here is the link for flutter_block library made with example ,have a good luck with state management

Related