Joe
Joe

Reputation: 431

Trying to use showDialog()/show Pop up on app startup

What I want to achieve: I want to open a pop up explaining my app when it starts.

My approach: As far as I understand it from googling the issue, I should use the showDialog() method. In its most basic form:

showDialog(
    context: context,
    builder: (context) {
      return Text('data');
    });

I tried returning actual dialogs (e.g. AlertDialog) but it doesn't change the behavior so I'm just using Text() with a string as a placeholder for now.

The problem:

No matter where I place the showDialog function, it doesn't work as intended (also see scrennshots below):

Any ideas?

Screenshots:

From initState:

enter image description here

From build method:

enter image description here

From DidChangeAppLifecycleState (the "succesful" variant:

enter image description here

Upvotes: 1

Views: 3614

Answers (4)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63559

Using WidgetsBinding.instance.addPostFrameCallback inside initState perform its inner task after the 1st frame is complete.

addPostFrameCallback Schedule a callback for the end of this frame.

Next issue arise for not having material. You can directly return AlertDialog on builder or wrap with any material widget like Material, Scaffold..

  @override
  void initState() {
    super.initState();
    
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      showDialog(
        context: context,
        builder: (context) {
          return const AlertDialog(
            content: Text('data'),
          );
        },
      );
    });
  }

If you are running version<3 null safety, use WidgetsBinding.instance?.addPostFrameCallback

Upvotes: 2

Joe
Joe

Reputation: 431

Thanks a lot for your answers. I ficed the issue by rewriting with your suggestions; and it works. I tihnk the issue was that I did not have _ or anything else in my WidgetsBinding code. So I did:

WidgetsBinding.instance?.addPostFrameCallback(() {})

instead of

WidgetsBinding.instance?.addPostFrameCallback((_) {})

Upvotes: 1

Ritik Jain
Ritik Jain

Reputation: 51

One of the methods with WidgetsBinding.instance!.addPostFrameCallback() works fine . If you show a normal show dialog with the press of a button too it will produce the same result. Here, you need to wrap the text("data") in a dialog widget such as alertDialog or simpleDialog widget as needed and it will display the dialog within the current scaffold as -

  WidgetsBinding.instance!.addPostFrameCallback((_) async {
      return await showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              content: Text("data"),
            );
          });
    }); 

I tried adding this in the init state and the dialog pops up fine when I restart the app

Upvotes: 1

Gourango Sutradhar
Gourango Sutradhar

Reputation: 1619

Will you please try below code in your init method? I hope this may work.

Future.delayed(Duration.zero, () async {
  myFunction();
});

Upvotes: 2

Related Questions