After that you insert this to the first page:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Page'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'This is a list page. It contains some information.',
style: TextStyle(fontSize: 18),
),
SizedBox(height: 20),
// Display the registered items
Expanded(
child: ListView.builder(
itemCount: itemList.length,
itemBuilder: (context, index) {
return ListTile(title: Text(itemList[index]));
},
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
// Update the list with the result from the second page
if (result != null) {
setState(() {
itemList.add(result);
});
}
},
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16, horizontal: 32),
side: BorderSide(color: Colors.blue),
),
child: Text(
'Go to Registration Page',
style: TextStyle(fontSize: 18),
),
),
],
),
);
}
}
You can pass back a `dynamic result` when you are popping the context and then call the `setState((){})` when the value is `true` otherwise just leave the state as it is.
I have pasted some code snippets for your reference.
handleClear() async {
try {
var delete = await deleteLoanWarning(
context,
'Clear Notifications?',
'Are you sure you want to clear notifications. This action cannot be undone',
);
if (delete.toString() == 'true') {
//call setState here to rebuild your state.
}
} catch (error) {
print('error clearing notifications' + error.toString());
}
}
Future<bool> deleteLoanWarning(BuildContext context, String title, String msg) async {
return await showDialog<bool>(
context: context,
child: new AlertDialog(
title: new Text(
title,
style: new TextStyle(fontWeight: fontWeight, color: CustomColors.continueButton),
textAlign: TextAlign.center,
),
content: new Text(
msg,
textAlign: TextAlign.justify,
),
actions: <Widget>[
new Container(
decoration: boxDecoration(),
child: new MaterialButton(
child: new Text('NO',),
onPressed: () {
Navigator.of(context).pop(false);
},
),
),
new Container(
decoration: boxDecoration(),
child: new MaterialButton(
child: new Text('YES', ),
onPressed: () {
Navigator.of(context).pop(true);
},
),
),
],
),
) ??
false;
}
Regards,
Mahi