简体   繁体   中英

Hide the bottom navigation bar when clicking on one bottom navigation bar item (and loading related page) - Flutter

I have a bottom navigation bar defined separately in a file, containing 5 items, and loading 5 pages (which doesn't have defined a bottom navigation bar). It should be present on 4 of the loaded pages but should disappear on the 5th page (CHAT) 在此处输入图像描述 . All the solutions I found online refers to hiding the bar while scrolling up or down, I think I'm quite close to the expected result but I didn't sort it out yet... The code for that "controller" that generates the BN-Bar is below, a screenshot with the bottom as well. Thank you.

class BottomNavigationBarController extends StatefulWidget {
  @override
  _BottomNavigationBarControllerState createState() =>
      _BottomNavigationBarControllerState();
}

class _BottomNavigationBarControllerState
    extends State<BottomNavigationBarController> {
  final List<Widget> pages = [
    MyHome(
      key: PageStorageKey('Page1'),
    ),
    MyStats(
      key: PageStorageKey('Page2'),
    ),
    MyCategories(
      key: PageStorageKey('Page3'),
    ),
    MyPeopleList(
      key: PageStorageKey('Page4'),
    ),
    MyChat(
      key: PageStorageKey('Page5'),
    ),
  ];

  final PageStorageBucket bucket = PageStorageBucket();

  int _selectedIndex = 0;

  Widget _bottomNavigationBar(int selectedIndex) => BottomNavigationBar(
        onTap: (int index) => setState(() => _selectedIndex = index),
        currentIndex: selectedIndex,
        type: BottomNavigationBarType.fixed,
        //
        iconSize: 24,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text(
              'HOME',
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.insert_chart),
            title: Text(
              'STATS',
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.view_list),
            title: Text(
              'INVENTORY',
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.group),
            title: Text(
              'PEOPLE',
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.forum),
            title: Text(
              'CHAT',
            ),
          ),
        ],
      );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
      body: PageStorage(
        child: pages[_selectedIndex],
        bucket: bucket,
      ),
    );
  }
}

I did it with an IF/Else in the bottom:

@override
  Widget build(BuildContext context) {
    if (_selectedIndex < 4) {
      return Scaffold(
        bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
        body: PageStorage(
          child: pages[_selectedIndex],
          bucket: bucket,
        ),
      );
    } else {
      return Scaffold(
        body: PageStorage(
          child: pages[_selectedIndex],
          bucket: bucket,
        ),
      );
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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