簡體   English   中英

使用標簽點擊方法時如何隱藏底部導航標簽欄

[英]how to hide the bottom navigation tab bar in flutter when using on tab tapped method

我在顫動中使用底部導航標簽欄,如果我們點擊標簽,它會導航到另一個屏幕。這里我使用標簽點擊方法並將當前索引分配給索引,但是當我點擊圖標時,它會導航屏幕帶有底部導航選項卡,我想在單擊選項卡時隱藏該選項卡`

 int _currentIndex = 0;

最終列表_screens = [

new HomeTab(),
new Loginpage(),
new HomeTab(),
new CartTab(),
new HomeTab(),

];

return Scaffold(


  body:
  _screens[_currentIndex],

  bottomNavigationBar: isBottomBarVisible ?  new BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      unselectedItemColor: Colors.grey,
      selectedItemColor: Colors.orange,
      onTap: onTabTapped, // new
    currentIndex: _currentIndex,
      items: [
        new BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text("Home")),
        new BottomNavigationBarItem(icon: new Icon(Icons.search), title: new Text("Search")),
        new BottomNavigationBarItem(icon: new Icon(Icons.notifications_none), title: new Text("Notifications")),
        new BottomNavigationBarItem(icon: new Icon(Icons.shopping_basket), title: new Text("Bag")),
        new BottomNavigationBarItem(icon: new Icon(Icons.person_outline), title: new Text("Account"))
      ]): Container(

  ),

);

}

 void onTabTapped(int index) {
setState(() {
  _currentIndex = index;
});

}

一種方法是使用布爾標志來跟蹤底部欄的可見性,然后使用此標志將其添加或不添加到小部件樹:

    bool isBottomBarVisible = true;

...
...

    bottomNavigationBar: isBottomBarVisible ? new BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      unselectedItemColor: Colors.grey,
      selectedItemColor: Colors.orange,
      onTap: onTabTapped, // new
    currentIndex: _currentIndex,
      items: [
        new BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text("Home")),
        new BottomNavigationBarItem(icon: new Icon(Icons.search), title: new Text("Search")),
        new BottomNavigationBarItem(icon: new Icon(Icons.notifications_none), title: new Text("Notifications")),
        new BottomNavigationBarItem(icon: new Icon(Icons.shopping_basket), title: new Text("Bag")),
        new BottomNavigationBarItem(icon: new Icon(Icons.person_outline), title: new Text("Account"))
      ]) : Container(),

如果isBottomBarVisible為真,則將BottomNavigationBar添加到小部件樹中,否則添加的小部件是一個空Container 因此,您可以通過在onTabTapped方法時將isBottomBarVisible設置為 true 來處理BottomNavigationBar的可見性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM