I am completely new to flutter and am trying to create an alert popup that
appears when the user clicks on a button.
This is different than the post already on stack overflow which asks for an alert dialog, so please do not vote down this question.
I want the popup to appear for 3 seconds and then close by itself.
The problem I am facing is that the popup does not appear at all when the button is clicked.
The following is what I have thus far:
class ExitGameRoute extends StatelessWidget {
const ExitGameRoute({super.key});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => (
context: context,
builder: (context) => const AlertDialog(
title: Text('Exiting Game Alert'),
content: Text('Exiting Build Up Domino...'),
// return const AlertDialog(
// title: Text('Exiting Build Up Domino...')
// );
),
Future.delayed(const Duration(seconds: 3), () {
Navigator.of(context).pop(true);
})
),
child: const Text('Exit Game',
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold)));
}
}
Please help me understand where I am going wrong. Thanks!
2
Answers
You are not using
showDialog
function inside youronPressed
call back.The code above works properly use this.
Button
Alert Dialog