[英]How can I pass arguments from one tab to another?
My application has two tabs.我的应用程序有两个选项卡。 One contains the Chapter and second tab contains the Lessons.第一个包含章节,第二个选项卡包含课程。 The number of lessons on second tab is dynamic and depends on from which Chapter it is building.第二个选项卡上的课程数量是动态的,取决于它正在构建的章节。 I could move from First Tab to Second using the DefaultTabController.of(context).animateTo(1) But how can I pass the chapter index to second tab like the arguments in Navigator.push ?我可以使用DefaultTabController.of(context).animateTo(1)从 First Tab 移动到 Second 但是如何像 Navigator.push 中的参数一样将章节索引传递给第二个选项卡?
A possible solution could arrive with the provider package.可能的解决方案可以随提供程序包一起提供。 Do not place it at the top of your widget tree if you don't need it, you'll avoid potential useless rebuilds.如果你不需要它,不要把它放在你的小部件树的顶部,你会避免潜在的无用重建。
Probably you want to do this:可能你想这样做:
class Data {
String something;
}
And then at some point in your widget tree, above the page with the tabs, you use:然后在小部件树中的某个点,在带有选项卡的页面上方,您使用:
Provider(
create: (_) => Data(),
child: YourWidgetWithTabs()
)
Childs will be able to call Provider.of<Data>(context)
to get the data. Childs 将能够调用Provider.of<Data>(context)
来获取数据。 It's usually better to use Consumer which gives you more control on rebuilds and consequently it improves performance.通常最好使用消费者,它可以让您更好地控制重建,从而提高性能。
You will crate a proper Data
class (not like mine, it just gives the idea).你将创建一个合适的Data
类(不像我的,它只是给出了想法)。
The general point is to have a "data holder" at the top of the tree that you use to read/write data.一般的观点是在用于读/写数据的树的顶部有一个“数据持有者”。 If you also need to listen to changes made to the data source, use ChangeNotifierProvider
.如果您还需要监听对数据源所做的更改,请使用ChangeNotifierProvider
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.