如何在Flutter App中管理选项卡之间的状态 [英] How to Manage state between tabs in Flutter App

查看:155
本文介绍了如何在Flutter App中管理选项卡之间的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用TabBar,需要在第一个选项卡中获取Input并根据其他选项卡上的计算结果显示

输出。

I am using TabBar in my application where I need to get Input in first tab and show
output based on some calculation on other tab.

我能够在第一个选项卡中获得输入,但是当我单击第二个选项卡时,我的状态将会丢失。我不确定是否必须在根级别将其设置为有状态的小部件。

I am able to get input in first tab but when I click on second tab, my state is lost. I am not sure if I have to make it a Stateful widget at root level.

我在Mobile App中相对较新。有人可以在这里帮我吗?

I am relatively new in Mobile App. Can someone help me out here?

编辑:

现在我能够实现我上面所说的内容,但是当我尝试从第一个选项卡访问项目列表时,它在第二个选项卡中引发空异常吗?

Now I am able to achieve what I said above, but when I am trying to access the list of items from first tab, it throws null exception in second tab?

我使用 AutomaticKeepAliveClientMixin解决了上述问题 strong>,如下面的答案所述。

I solved the above problem using AutomaticKeepAliveClientMixin as described in the answer below.

推荐答案

我想您可能会想到的是 AutomaticKeepAliveClientMixin 。我总是将其添加到 TabBarView 的屏幕小部件中。这样,当用户在一个选项卡中的字段中输入数据并导航到其他选项卡并返回数据时,数据仍然如您所愿。
然后,当用户执行保存之类的操作时,我将处理状态管理(BLoC等)。

I think what you may be after is AutomaticKeepAliveClientMixin. I always add that to my screen widgets within TabBarView. This way when a user enters data into fields in one tab and navigates to other tabs and back the data is still there as you would expect. Then when the user performs an action like "save" I deal with state management (BLoC etc).

这篇关于如何在Flutter App中管理选项卡之间的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆