简体   繁体   English

如何在Flutter的Tabbar中动态打开一个tab?

[英]How to dynamically open a tab in Tabbar in Flutter?

So basically I have a widget which I Navigate to:所以基本上我有一个导航到的小部件:

Navigator.of(context).pushNamed(Purchases.route, arguments: {"index": index});

The Purchases widget is simply an empty Widget with nothing except two tabs using TabBar, TabBarView, and TabController . Purchases 小部件只是一个空的小部件,除了使用TabBar, TabBarView, and TabController的两个选项卡之外什么都没有。 The tabs are for pending and delivered .这些选项卡用于pending and delivered The idea is, when I navigate to the widget, the correct tab is the initial tab that is open.这个想法是,当我导航到小部件时,正确的选项卡是打开的初始选项卡。

For example: When I click pending on the previous widget, the navigator navigates to the Purchases widget and the pending tab is the initial tab that is open.例如:当我在前一个小部件上单击pending时,导航器导航到“购买”小部件,待处理选项卡是打开的初始选项卡。 When I click delivered on the previous widget, the navigator navigates to the Purchases widget and the delivered tab is the initial tab that is open.当我单击上一个小部件上的delivered时,导航器导航到购买小部件,交付的选项卡是打开的初始选项卡。

I pass the index of the tab that I want opened and use it in the initState method and it works, however only partially .我传递了要打开的选项卡的索引,并在initState方法中使用它,它可以工作,但只是部分工作

void initState() {
    super.initState();
    _controller = TabController(length: 2, vsync: this);
    Future.delayed(Duration.zero, () {
      final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
      int index = args["index"];
      _controller.animateTo(index);
    });
}

This implementation works, however the tab isn't really initially open, its only animated after the tab at index 0 is opened.此实现有效,但是该选项卡最初并未真正打开,它仅在打开索引 0 处的选项卡后才具有动画效果。 This is bad performance since I plan on making API calls whenever the tabs open and if the user didn't open/care for the tab at index 0, then its a useless API call.这是一个糟糕的性能,因为我计划在标签打开时进行 API 调用,如果用户没有打开/关心索引 0 处的标签,那么它是一个无用的 API 调用。

I have tried:我努力了:

void initState() {
    super.initState();
    final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
    int index = args["index"];
    _controller = TabController(length: 2, vsync: this, initialIndex: index);
}

This doesn't work because context isn't available for use and results in an error.这不起作用,因为上下文不可用并导致错误。

How could I properly get this to work?我怎样才能正确地让它工作? Thank You!谢谢你!

So I did a little digging and found this to be the solution:所以我做了一点挖掘,发现这是解决方案:

void didChangeDependencies() {
    super.didChangeDependencies();
    final args = ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
    int index = args["index"];
    _controller = TabController(length: 2, vsync: this, initialIndex: index);
}

I guess initState wasn't the correct approach.我猜 initState 不是正确的方法。 didChangeDependencies does get called after every build whereas initState doesn't. didChangeDependencies在每次构建后都会被调用,而initState不会。 Something to consider.需要考虑的事情。

Setting an initialIndex to the TabController is the right approach.TabController设置一个initialIndex是正确的方法。

In order to have access to the context , you need call your API inside a separate function and then call it inside initState .为了访问context ,您需要在单独的 function 中调用 API,然后在initState中调用它。

void apiCall(){
  //Call here your API (context accessible).
}
@override
initState((){
  ...
  apiCall();
  ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM