Reputation: 6769
I have a statefull widget W1
which calls a stateless widget W2
.
W2
has onTap
functionality. I want to show an alert dialog in W2's onTap()
.
onTap:() {Alert(context: context, title:'Hi');},
I dont get any error, but no alert is shown on tap. I tried passing context as a parameter to W2
but I still dont see any dialog box.
What is the right way to show a dialog box from W2
?
I am using rflutter_alert package Link
Thanks
Upvotes: 6
Views: 17182
Reputation: 6769
Adding .show()
in end solved it.
onTap:() {Alert(context: context, title:'Hi').show();}
Its clearly documented in rflutter_alert package, but I somehow missed it.
Upvotes: 2
Reputation: 3216
Pass the context in the function call in onTap:
onTap:(context) {Alert(context: context, title:'Hi');},
Upvotes: 0
Reputation: 22417
You have to wrap your Alert(context: context, title:'Hi');
with showDialog(context: context, builder: (BuildContext context) => Alert(context: context, title:'Hi'));
Here is the cookbook sample:
Future<void> _neverSatisfied() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Anyway, for your question about how to pass context
, If you are creating a Stateless
or Stateful
widget you dont need to pass the context
, you can get it from build(BuildContext context) {}
.
Upvotes: 1