6

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

1
  • Alert is just an Widget(a class). You just called an constructor to create an instance of Alert. So you have to pass this instance to a function named showDialog to actually render the widget. Commented Jan 16, 2020 at 5:16

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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) {}.

Comments

0

Pass the context in the function call in onTap:

onTap:(context) {Alert(context: context, title:'Hi');}, 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.