The standard way to show a dialog box: https://api.flutter.dev/flutter/material/showDialog.html
Instead of a button, I want to show a dialog when a variable in my provider changes from 1 to 0. My view layer widget is already using a Consumer to watch the provider variables, so how do I just trigger the dialog when the value changes?
Future<void> showStreamDialog({required BuildContext context, required String title, required String msg}) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(msg),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('Ok'),
onPressed: () {
context.pop();
},
),
],
);
}
);
}
You could use
ref.listento listen to the provider and callshowDialogwhen the change you want occurs. Here's a simple example of a widget that does that while wrapping a widget subtree:The
_dialogOpenis there to prevent multiple versions of the dialog from being pushed onto the navigator. It can be removed if you're sure your provider cannot continue to change values and trigger the listener again while the dialog is open.