0

How can we reuse same widget with the same state, just building it once, and rebuilding only on changes at app state model (obviously currently it is being rebuilt each time when I trigger the Navigator)?

Navigator.of(context).push( MaterialPageRoute( builder: (context) => SchedulerView(), ), );

SchedulerView is the target widget.

Thank you.

  • Maybe there is a way to create some kind of page/widget which we can call from any other widget without a rebuild? Seems like Navigator necessarily calls build method.. – Maria94 Nov 05 '20 at 10:32

1 Answers1

0

I'm guessing that your issue is not the rebuild (which is a call of the build() function triggered by the framework) but the creation of another SchedulerView instance because the Navigator calls the constructor SchedulerView()...?

If so, can you achieve your objective by instantiating only once and then using it by reference?

var schedulerView = SchedulerView();
...
Navigator.of(context).push( MaterialPageRoute( builder: (_) => schedulerView));
rgisi
  • 858
  • 6
  • 21
  • Yes, I want to avoid creating another instance of SchedulerView. I tried initiating a variable to create an instance this way, but each time I trigger Navigator, in the log I can see it creates another instance anyway (initstate method is being executed each time I use schedulerView via Navigator).. – Maria94 Nov 05 '20 at 15:41
  • Hmmm, I'm getting the feeling that you try to do something that shouldn't be done this way :-) Maybe this thread helps for inspiration? https://stackoverflow.com/questions/49439047/how-to-preserve-widget-states-in-flutter-when-navigating-using-bottomnavigation Or, if not, can you post some more of your code or give some more context about what exactly you're trying to achieve? – rgisi Nov 07 '20 at 18:37