salah-rashad / flutter_context_menu

Create and display a customizable context menus in your app.
BSD 3-Clause "New" or "Revised" License
21 stars 7 forks source link

Enhance generics #3

Open clragon opened 9 months ago

clragon commented 9 months ago

Hi, great package, love it.

One thing I have noticed is that neither ContextMenu, nor ContextMenuRegion, nor ContextMenuEntry have a generic. This somewhat impairs typing MenuEntry correctly, which does have a generic.

I see that your package has taken inspiration from PopupMenu in the framework, or is at least built very similar. It should be possible to fully type MenuEntry by giving all the mentioned classes a generic <T> as well.

Of course we then run into the question how to type MenuDivider which does not have any value that could have a generic type. Here we can also take inspiration from the framework itself, namely the PopupMenuDivider.

In the framework code, we can see that PopupMenuDivider is simply typed <Never>: https://github.com/flutter/flutter/blob/2e9cb0aa71/packages/flutter/lib/src/material/popup_menu.dart#L102

This should also work for us, as the Divider can never be selected and therefore never be passed into the callback. The Never type will also miracolously conform to the generic T. I have no idea how that works internally, but it will not throw any errors if we use it correctly.