简体   繁体   English

如何导航到 flutter 中的特定选项卡?

[英]How to navigate to a specific tab in flutter?

I am new to flutter and in my app, I want to navigate to the tab on the previous screen, from which I navigated forward in the first place.我是 flutter 的新手,在我的应用程序中,我想导航到上一个屏幕上的选项卡,我首先从该选项卡向前导航。

Please see the attached video for clear visualization.请参阅随附的视频以获得清晰的可视化效果。

Please note that instead of popping the top screen on the back button I am again pushing the previous activity.请注意,我没有在后退按钮上弹出顶部屏幕,而是再次推送上一个活动。 (Due to some separate issues i have to do so) see video (由于一些单独的问题我必须这样做)看视频

if it helps,如果有帮助,

I can explain what is actually happening.我可以解释实际发生的事情。

I have 5 tabs, which are working out like filters.我有 5 个选项卡,它们像过滤器一样工作。 I am displaying a list of data in the "All" tab and the rest of the tabs eg Lunch, Prayer, etc are basically filtering the list and displaying the content there.我在“全部”选项卡中显示数据列表,而午餐、祈祷等选项卡的 rest 基本上是过滤列表并在那里显示内容。

Now each listed tile when clicked will take to the particularly detailed section (next screen).现在,单击每个列出的图块都会转到特别详细的部分(下一个屏幕)。 And when I navigate back to the main screen, it should open the same tab it once navigated from当我导航回主屏幕时,它应该打开它曾经导航过的同一个选项卡

I have used the initial index in TabBarView, where I can pass the current index of tab.我在 TabBarView 中使用了初始索引,我可以在其中传递选项卡的当前索引。 But it gives a null error Idk why但它给出了 null 错误 Idk 为什么

Here is my code of class in which tabs are present and content:这是我的代码 class,其中包含选项卡和内容:

 class SalesManListDemo extends StatefulWidget {
  const SalesManListDemo({
    Key key,
  }) : super(key: key);

  

    @override
  State<SalesManListDemo> createState() => _SalesManListDemoState();
}

class _SalesManListDemoState extends State<SalesManListDemo>
    with SingleTickerProviderStateMixin {
  bool isSearching = false;
  TabController _tabController;


  final List<Tab> myTabs = <Tab>[
    const Tab(
      text: 'All',
    ),
    const Tab(
      text: 'Active',
    ),
    const Tab(
      text: 'InActive',
    ),
    const Tab(
      text: 'Lunch',
    ),
    const Tab(
      text: 'Prayer',
    ),
  ];
    @override
  void initState() {
    _tabController = TabController(length: myTabs.length, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

TabBar(
            controller: _tabController,
            labelColor: const Color(0xff2454f4),
            labelStyle: TextStyle(
              decoration: TextDecoration.underline,
              fontSize: 18.sp,
              fontWeight: FontWeight.w700,
            ),
            unselectedLabelStyle: TextStyle(
              color: Colors.grey,
              decoration: TextDecoration.none,
              fontSize: 18.sp,
              fontWeight: FontWeight.w700,
            ),
            isScrollable: true,
            indicatorColor: Colors.transparent,
            unselectedLabelColor: const Color(0xffa3a3a3),
            tabs: myTabs,
          ),
          SizedBox(
            height: 8.h,
          ),
          Expanded(
            child: TabBarView(controller: _tabController, children: [
              // All employees List View
              SalesmanList(
                SalesmanDataa: SalesmanData,
                list_length: SalesmanData.length,
              ),
              SalesmanList(
                SalesmanDataa: activeemp,
                list_length: activeemp.length,
              ),
              SalesmanList(
                SalesmanDataa: inActiveEmployees,
                list_length: inActiveEmployees.length,
              ),
              SalesmanList(
                SalesmanDataa: atLunchEmployees,
                list_length: atLunchEmployees.length,
              ),
              SalesmanList(
                SalesmanDataa: atPrayerEmployees,
                list_length: atPrayerEmployees.length,
              ),
            ]),

And this is the code of another class from where I navigate back on the tabs screen:这是另一个 class 的代码,我从这里导航回选项卡屏幕:

leading: IconButton(
        onPressed: () {
          Navigator.pushReplacement(context,
              MaterialPageRoute(builder: (context) => SalesManListDemo()));
        },

Please try CupertinoTabScaffold .请尝试CupertinoTabScaffold In this state, you keep the previous state in memory and when you return, you can continue from the old state.在这个state中,你将之前的state保留在memory中,返回时可以从旧的state继续。

The tab bar logic of applications such as instagram and twitter is like this. instagram、twitter等应用的tab bar逻辑是这样的。

A tabBar and a tabBuilder are required.需要一个tabBar和一个tabBuilder The CupertinoTabScaffold will automatically listen to the provided CupertinoTabBar's tap callbacks to change the active tab. CupertinoTabScaffold将自动侦听提供的 CupertinoTabBar 的点击回调以更改活动选项卡。

A controller can be used to provide an initially selected tab index and manage subsequent tab changes. controller 可用于提供最初选择的选项卡索引并管理后续的选项卡更改。 If a controller is not specified, the scaffold will create its own CupertinoTabController and manage it internally.如果未指定 controller,脚手架将创建自己的CupertinoTabController并在内部进行管理。 Otherwise it's up to the owner of controller to call dispose on it after finish using it.否则,由 controller 的所有者在使用完后对其调用 dispose。

For Example:例如:

CupertinoTabScaffold( tabBar: CupertinoTabBar(
items: const <BottomNavigationBarItem> [
  // ...
],), tabBuilder: (BuildContext context, int index) {
return CupertinoTabView(
  builder: (BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text('Page 1 of tab $index'),
      ),child: Center(
        child: CupertinoButton(
          child: const Text('Next page'),
          onPressed: () {
            Navigator.of(context).push(
              CupertinoPageRoute<void>(
                builder: (BuildContext context) {
                  return CupertinoPageScaffold(
                    navigationBar: CupertinoNavigationBar(
                      middle: Text('Page 2 of tab $index'),
                    ),
                    child: Center(
                      child: CupertinoButton(
                        child: const Text('Back'),
                        onPressed: () { Navigator.of(context).pop(); },
                      ),
                    ),
                  );
                },
              ),);
          },
        ),
      ),
    );
  },
);

}, ) }, )

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

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