I have a page, a widget an a provider.
The page has some elements in it but there is a widget that I need to use for other pages so that’s why it’s separated into another widget. This widget is just a dialog, so it’s a class without any state:
class Difficulty {
showModal() {
var settingsController = context.watch<SettingsController>(); // this is what I'd need
return showPlatformDialog(
...
So in my page, I call Difficulty().showPlatformDialog()
, but I’d need the context from previous page or create a new one.
What’s the best approach?
- Pass it to
Difficulty()
- Pass it to
showModal()
- Create a
StatefulBuilder
or something else with its own context
2
Answers
You just need to pass the
BuildContext
as a parameter toshowPlatformDialog()
.Example:
And call the function from the widget as
To answer your query, You can pass the
context
in any way like passing it to the function or creating a stateless/stateful widget.I prefer the third approach though
Or you can use the global level navigation key that you can use context anywhere on the project and you don’t have to pass the context.
You can checkout the my answer here I explained there how to use it.
https://stackoverflow.com/a/71922909/10936691