I have a list of ingredients that I want to clear when user go back and redo the same function. How can I achieve that? Until now, when user redo the operations, old results still show on Result page.
This is my main page button:
ElevatedButton(onPressed: (){
if (index == 1){
multiply();
Navigator.of(context).pop();
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => result(newList)));
}else {
divide();
Navigator.of(context).pop();
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => result(newList)));
};
}, child: Text('Calculate'.toUpperCase(),style: TextStyle(fontWeight: FontWeight.bold),),
style: ElevatedButton.styleFrom(
primary: Colors.green),
),],
and this is the Result page with a list to clear:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepOrange,
title: Text('Result'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.only(top: 15.0),
child: ListView.builder(itemBuilder: (contetx ,index) {
return Column(
children:[
Padding(
padding: const EdgeInsets.only(top: 8.0, left: 15, right: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(child: Text(widget.newList[index].name,
style: TextStyle(fontWeight: FontWeight.bold),),
margin: EdgeInsets.only(left: 5.0),),
Container(child: Text(widget.newList[index].quantity,
style: TextStyle(fontSize: 18),)),
],
),
),
Divider(thickness: 1,indent: 20,endIndent: 20,),
],
);
},
itemCount: widget.newList.length,
),
),
);
}
}
CodePudding user response:
You can achieve that by multiple ways, 1- The following way, creates a new list to be passed, and immediately clears the original list
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => result([...newList])));
newList.clear();
2- wait until Result
screen is popped
ElevatedButton(onPressed: () async {
....
await Navigator.of(context).push(
MaterialPageRoute(builder: (context) => result(newList)));
newList.clear();