简体   繁体   English

如何将参数从一个选项卡传递到另一个选项卡?

[英]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.

相关问题 如何将一个选项卡中的浮动操作按钮显示/隐藏到另一个选项卡? - How can I show/hide a floatingActionButton in one tab to another? 如何在 flutter 中将 controller 数据从一个小部件传递到另一个小部件 - How can I pass the controller data from one widget to another in flutter 如何将数据从一个导出函数传递到另一个导出函数? - How can I pass data from one export function to another export function? 我如何将数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass Data from one widget to another widget in flutter? 如何将 flutter textediting controller 值从一个无状态小部件传递到另一个无状态小部件 - How can I pass flutter textediting controller value from one stateless widget to another stateless widget 我如何将 QuerySnapshot 数据从一个小部件传递到 flutter 中的另一个小部件? - How i can pass QuerySnapshot data from one widget to another widget in flutter? 如何在 flutter 中将数据从一个应用程序传递到另一个应用程序? - How can I pass data from one app to another app in flutter? 如何从 Flutter 中的另一页访问 Arguments - Getx - How Can I Access Arguments From Another Page In Flutter - Getx 如何将类型从一个 class 传递到另一个? - How do I `pass` the Type from one class to another? 如何将数据从一个页面传递到另一个页面? - How do I pass data from one page to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM