本文介绍了用于从子窗口小部件更新TabController长度的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主页上有许多子窗口小部件.搜索时,显示SearchWidget,否则显示DashletsWidget.

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

DashletsWidget具有TabController. TabController保留在主页中,因此搜索后不会重置活动选项卡.

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

DashletsWidget具有一个dashlet设置窗格,该窗格可能会更改选项卡的数量.

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

DashletsWidget(ValueNotifier<int> dashletCount, TabController controller)使用ValueNotifier让其为父代重新创建controller.在重新创建时,旧的TabController不能可靠地处置,因此,请仅取消引用而不进行处置.这是一种工作,但是非常不自然.是否有一个很好的模式来更新TabController.length.

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.

推荐答案

最好将未使用的TabController进行垃圾回收.

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

这是另一种感觉更好的策略:您可以将有关选项卡数量的信息存储在State所拥有的模型对象中,位于树的级别比DashletsWidget高,并传递该模型对象作为DashletsWidget的配置值.如果重建DashletsWidget且更改构造函数参数,则将调用DashletsWidgetStatedidUpdateWidget方法,您可以借此机会替换TabController.或者,您也可以使用多个制表符来为DashletsWidget构造一个ValueKey,因此更改制表符配置将自动dispose现有的DashletsWidgetState并将其替换为新的DashletsWidgetState.

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.

这篇关于用于从子窗口小部件更新TabController长度的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 17:42