Skip to content

Instantly share code, notes, and snippets.

@mikaelj
Last active October 7, 2019 16:20
Show Gist options
  • Save mikaelj/ca604bdec6fa8dbdd9d23bf7b9b74647 to your computer and use it in GitHub Desktop.
Save mikaelj/ca604bdec6fa8dbdd9d23bf7b9b74647 to your computer and use it in GitHub Desktop.
return BlocListener<MyBloc, MyState>(
listener: (context, state) {
if (state is QuestionState && state.attempts == 0) {
final attempts = state.attempts;
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return BlocBuilder<MyBloc, MyState>(
builder: (context, state) {
final bloc = BlocProvider.of<MyBloc>(context);
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
title: Text(
"($attempts) Title"),
content: Text(
"($attempts) Message"),
actions: <Widget>[
FlatButton(
child: Text("Cantel"),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("Retry"),
onPressed: () {
// RetryEvent will be mapped to QuestionState in MyBloc
bloc.dispatch(RetryEvent(
attempts: attempts + 1));
},
),
],
)
);
},
);
}
);
}
},
);
@mikaelj
Copy link
Author

mikaelj commented Oct 7, 2019

@felangel Works great and turns out wasn't far away from what I had - I think moving things out to widgets (like you did) did the trick. Makes (seemingly) complex code more readable. Yeah, be great to have that as a recipe for others!

Then once the user has the basics (with your snippet), they can easily do more complex stuff like using a FutureBuilder for changing text/actions after a delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment