How to refresh text in flutter
479
Solution 1
use a set state to update
onTap: (){
setState((){
mealsNum++;
});
print(mealsNum);,
Solution 2
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
Solution 3
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),
),
),
),
],
),
Solution 4
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),
),
),
),
],
),
Author by
Laila Mattar
Updated on December 25, 2022Comments
-
Laila Mattar 11 months
I have this
Text
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?