Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern for updating TabController length from child widget

Tags:

flutter

The main page has many children widgets. When searching, SearchWidget is shown, otherwise DashletsWidget is shown.

DashletsWidget has TabController. TabController is kept in the main page, so that active tab is not reset after searching.

DashletsWidget has a dashlet setting pane, which might change number of tabs.

DashletsWidget(ValueNotifier<int> dashletCount, TabController controller) use ValueNotifier to let re-create controller to the parent: . While re-creating, the old TabController cannot be disposed reliably, so let just de-reference without disposing. It is kind-of-work, but so un-natural. Is there a good pattern to update TabController.length.

like image 724
Kyaw Tun Avatar asked Sep 12 '17 15:09

Kyaw Tun


1 Answers

It's fine to let your unused TabController be garbage collected.

Here's another strategy that might feel better: You could have store the information about the number of tabs in a model object that is owned in a State at a higher level of your tree than DashletsWidget, and pass that model object as configuration values to DashletsWidget. If the DashletsWidget is rebuilt and the constructor arguments change, the didUpdateWidget method of DashletsWidgetState will be called and you can use that as an opportunity to replace the TabController. Or alternatively you could use the number of tabs to construct a ValueKey for DashletsWidget and so changing tabs configuration would automatically dispose the existing DashletsWidgetState and replace it with a fresh one.

like image 55
Collin Jackson Avatar answered Sep 29 '22 14:09

Collin Jackson