Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If PullDownMenuItem's onTap is a showDialog function, nothing happens when clicking it. #1

Closed
LathDevers opened this issue Jun 10, 2022 · 5 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@LathDevers
Copy link

PullDownButton(
        itemBuilder: (ctx) => [
          PullDownMenuItem(
            title: 'Compress',
            icon: CupertinoIcons.archivebox,
            onTap: () => showDialog(context: context, builder: (context) => AlertDialog(content: Container())),
          ),
        ],
        position: PullDownMenuPosition.under,
        buttonBuilder: (context, showMenu) => CupertinoButton(
          onPressed: showMenu,
          padding: EdgeInsets.zero,
          child: Icon(CupertinoIcons.ellipsis_circle),
        ),
      ),
@LathDevers LathDevers changed the title If PullDownMenuItem's onTap is showDialog function, nothing happens when clicking it. If PullDownMenuItem's onTap is a showDialog function, nothing happens when clicking it. Jun 10, 2022
@notDmDrl notDmDrl added the bug Something isn't working label Jun 10, 2022
@notDmDrl
Copy link
Owner

notDmDrl commented Jun 11, 2022

Hi @LathDevers, thanks for submitting issue

I am able to reproduce this issue with PullDownButton as well as with PopupMenuButton, so I'd say this behaviour is expected.

Both of menu buttons first call a onTap function and then immediately fire Navigator.pop(), which in this case show and immediately hide dialog as well as not pop menu since navigation stack was changed. Relevant stackoverflow issue

As a working solution, while not perfect, consider this:

PullDownButton(
  itemBuilder: (ctx) => [
    PullDownMenuItem(
      title: 'Compress',
      icon: CupertinoIcons.archivebox,
      onTap: () async {
        await Future<void>.delayed(const Duration(milliseconds: 1));
        await showDialog<void>(
          context: context,
          builder: (context) => AlertDialog(content: Container()),
        );
      },
    ),
  ],
  position: PullDownMenuPosition.under,
  buttonBuilder: (context, showMenu) => CupertinoButton(
    onPressed: showMenu,
    padding: EdgeInsets.zero,
    child: const Icon(CupertinoIcons.ellipsis_circle),
  ),
)

Although I will try to come up with a better solution in a couple of weeks when I have more time (or at least update examples/documentation with example that i provided).

@notDmDrl notDmDrl added documentation Improvements or additions to documentation enhancement New feature or request and removed bug Something isn't working labels Jun 11, 2022
@LathDevers
Copy link
Author

Hi @LathDevers, thanks for submitting issue

I am able to reproduce this issue with PullDownButton as well as with PopupMenuButton, so I'd say this behaviour is expected.

Both of menu buttons first call a onTap function and then immediately fire Navigator.pop(), which in this case show and immediately hide dialog as well as not pop menu since navigation was stack changed. Relevant stackoverflow issue

As a working solution, while not perfect, consider this:

PullDownButton(
  itemBuilder: (ctx) => [
    PullDownMenuItem(
      title: 'Compress',
      icon: CupertinoIcons.archivebox,
      onTap: () async {
        await Future<void>.delayed(const Duration(milliseconds: 1));
        await showDialog<void>(
          context: context,
          builder: (context) => AlertDialog(content: Container()),
        );
      },
    ),
  ],
  position: PullDownMenuPosition.under,
  buttonBuilder: (context, showMenu) => CupertinoButton(
    onPressed: showMenu,
    padding: EdgeInsets.zero,
    child: const Icon(CupertinoIcons.ellipsis_circle),
  ),
)

Although I will try to come up with a better solution in a couple of weeks when I have more time (or at least update examples/documentation with example that i provided).

Thanks, that's a perfectly fine workaround for now. 😉

@rikbrown
Copy link

The above workaround works for me with a CupertinoAlertDialog too, thanks!

@notDmDrl
Copy link
Owner

notDmDrl commented Jun 29, 2022

@LathDevers, @rikbrown,

I have just published an update which should have fixed this issue (at least from my testing it did).

Now its pretty straightforward, just remove await Future<void>.delayed(const Duration(milliseconds: 1)):

PullDownButton(
  itemBuilder: (ctx) => [
    PullDownMenuItem(
      title: 'Compress',
      icon: CupertinoIcons.archivebox,
      onTap: () => showDialog<void>(
        context: context,
        builder: (context) => AlertDialog(content: Container()),
      ),
    ),
  ],
  position: PullDownMenuPosition.under,
  buttonBuilder: (context, showMenu) => CupertinoButton(
    onPressed: showMenu,
    padding: EdgeInsets.zero,
    child: const Icon(CupertinoIcons.ellipsis_circle),
  ),
)

@LathDevers
Copy link
Author

I have just published an update which should have fixed this issue (at least from my testing it did).

...

@notDmDrl thanks! It works for me now 👍🏻
Tested on Android 12 (physical device) and iOS 15.5 (simulator and physical device)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants