Thanks in advance for any help you can give!
I’m currently developing in Flutter 3.0 using VS Code. I’m admittedly not the best at Flutter and have only been using it off and on for a few months now in between other project tasks for my job.
The problem I’m having is that I don’t quite understand how to get Stateful widgets to cooperate and communicate with each other on the same page. In my specific example, I am making a "dashboard" of sorts. For this page/screen of the app, the "Dashboard" widget serves as the foundation for the other widgets on this page. There are two sections: a left handed panel that is a Menu widget; and a right handed panel that defaults to "Option 1" widget. Within the menu, I have used a gesture detector to properly obtain touch/click events and can trigger prints to the console (let alone, change the background highlight color behind each of the options). What I’m trying to do is, whenever I click on an Option widget from the menu, the panel on the right displays a different, related widget that contains its own set of widgets and data. Even though I most definitely have the click events registering and working, no matter what I do, I can’t seem to get the "Menu" widget to change what is displayed in the other panel. I’m not looking for someone to code it for me (even though some miraculous exact solution would probably ease my headache at this point). What I’m asking for is an example or method of how to display/load widgets on my right hand panel when I click the different options from the Menu widget. Rather than code I’ve provided a very basic dev-art graphic depicting the situation. If anyone who wishes to help needs code, please respond with what parts you might like to see and I’ll share what I can. Thanks
I have tried Googling for the answer but nothing I’ve found helps, including passing information using riverpod, and including creating instances of the widgets I need to populate to the screen and sharing them and the state information with the containing Dashboard widget.
2
Answers
If I understood your problem correctly, you are struggling with states of the right handed dashbord panel. what you want to do is, define a string called "state" in the menu panel on the left, then in the onTap property of the GestureDetector, you want to setState this "state" to for example "Option1" or "Option2". Then If these widgets are in the same build method you can use this state to return different styles of pages. if they are different calsses, you want to pass this state variable to the other class and use it like this: "widget.state" and then you can use that to return different styles of pages. like this code below:
You could even use just plain flutter state management for this, which I would recommend to do so in order to better understand how it works, but if you want to use riverpod for this you could try and adjust the following script.
Basically with riverpod you can avoid Stateful widgets altogether since the providers are in charge of reloading the state.
ref.watch(dashboardProvider)
in particular makes sure to rebuildInfoPanel()
every time the value ofdashboardProvider
changes.Do mind that this is not the best way to do this, since each of the Info1/2/3() widgets, as well as InfoPanel(), will be created from scratch every time a change is triggered. For what I suspect you’re trying to build, I would take a look at flutter’s Drawer or Tabs.
In any case here’s an example: