Use same instance of a widget flutter

47 Views Asked by At

I want to use the same widget for a page. To be more precise I'll give an example. So I have a have a header section (a sub-widget) which is used for almost all the pages throughout the app. I want this header to not stack up every time a new page is stacked upon the other. Is there any solution for this?

1

There are 1 best solutions below

2
Punnoose K Thomas On

There are 2 approaches to this :

  1. Sub sections : You can create a generic scaffold and then create parallel containers to show as it's body depending on whatever click conditions you set. The Appbar and other Scaffold items will remain the same without stacking and you can switch between bodies instead of staking the bodies. Just make sure you specify the leading widget correctly and write the function to the leading widget action and the WillPopScope action properly.

  2. Custom Widget Appbar : You can design a Singleton Class for Custom Widgets and then declare a function here that returns an AppBar. Then, instead of pushing to pages, you perform popAndPush between pages and logically control the flow. Every page calls this common Appbar function to show the same app bar and each individual page passes a function to the Appbar dictating what it should do when the leading widget is clicked.