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

feat: Add menu builder to allow injecting cubits/blocs from upper context #66

Open
jtdLab opened this issue Feb 17, 2025 · 1 comment
Open

Comments

@jtdLab
Copy link

jtdLab commented Feb 17, 2025

final cubit = context.read<MyCubit>();

 return PullDownButton(
      menuBuilder: (child) => BlocProvider.value(value: cubit, child: child),
...
    );
  }
@notDmDrl
Copy link
Owner

Hi @jtdLab

In such cases, I am unsure what the benefit of menuBuilder would be. Can you provide a more elaborate example of what you are trying to achieve?


Speculation zone

I am not familiar with the bloc and my memory of provider is a bit skewed, but I assume you want to pass MyCubit down to a menu's route so that it can be accessed inside the menu items.

It is not needed IMO since the context provided by PullDownButton.itemBuilder is the context of the widget itself, not of the menu's route. So even if you could theoretically pass it with the menuBuilder, it would not have the desired effect.

You're better off just context.read/watch() directly in the required menu item's parameters.

Something like this has worked out pretty nicely for me with riverpod (adapted to bloc):

Widget build(BuildContext context {
  final filter = context.watch<MyCubit>();

  // inside itemBuilder 
  (context) => [
    for (final year in _someFuncThatReturnsAllowedYears())
      PullDownMenuItem.selectable(
        title: MaterialLocalizations.of(context).formatYear(DateTime(year)),
        selected: year == filter.year,
        onTap: () => context.read<MyCubit>().updateYear(year),
      ),
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants