【问题标题】:A mixed mode Flutter Bottom Navigation bar一个混合模式的 Flutter 底部导航栏
【发布时间】:2022-01-29 03:51:31
【问题描述】:

我想在我的应用程序中包含一个底部导航栏,它将显示在某些页面上,但不会显示在其他页面上。 据我了解,Flutter BottomNavigationBar 小部件重新渲染了 Scaffold Body 属性,实际上并没有路由到另一个页面,而是始终显示导航栏。有人会对我如何拥有如下所示的底部导航有一些建议,其中 Tab 1 和 Tab 3 路由到显示导航栏的屏幕,而 Tab 2 和 Tab 3 是不显示导航栏的屏幕?

Bottom Navigation Bar

【问题讨论】:

  • 我有同样的问题,然后我尝试了一件事情,我在每个屏幕中使用一个脚手架,并在参数中传递索引,当用户单击底部导航项时,我照常导航这意味着您必须使用一个通用屏幕在您的应用中,或者您可以尝试密钥我不知道如何使用它,但它可以工作

标签: flutter flutter-layout


【解决方案1】:

我相信您想在选项卡 1 和 3 中显示底部导航栏,同时将其隐藏在 2 和 4 中,对吧?我假设您有一个变量来跟踪所选索引,我们称之为int _selectedIndex = 0。在 bottomNavigation 属性中,您可以像下面那样显示而不显示它

bottomNavigationBar: _selectedIndex.isEven ? null: BottomNavigationBar()

这里它检查 _selectedIndex 值(在这种情况下为 0,1,2,3),如果是奇数,则显示页面和导航(标签 1 和 3),如果是偶数(标签 2 和 4)然后你提供一个null 从而不显示导航。唯一关心的是如何在没有导航的情况下在两个选项卡之外导航,但这应该可以解决您的直接问题

【讨论】:

    【解决方案2】:

    也许这会对你有所帮助:

    import 'package:flutter/material.dart';
    
    class HomePage extends StatefulWidget {
      const HomePage({Key? key}) : super(key: key);
    
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      int pageIndex = 0;
    
      final pages = [
        const Page1(),
        const Page2(),
        const Page3(),
        const Page4(),
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: const Color(0xffC4DFCB),
          appBar: AppBar(
            leading: Icon(
              Icons.menu,
              color: Theme.of(context).primaryColor,
            ),
            title: Text(
              "Bottom Navigation bar",
              style: TextStyle(
                color: Theme.of(context).primaryColor,
                fontSize: 25,
                fontWeight: FontWeight.w600,
              ),
            ),
            centerTitle: true,
            backgroundColor: Colors.white,
          ),
          body: pages[pageIndex],
          bottomNavigationBar: buildMyNavBar(context),
        );
      }
    
      Container buildMyNavBar(BuildContext context) {
        return Container(
          height: 60,
          decoration: BoxDecoration(
            color: Theme.of(context).primaryColor,
            borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(20),
              topRight: Radius.circular(20),
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              IconButton(
                enableFeedback: false,
                onPressed: () {
                  setState(() {
                    pageIndex = 0;
                  });
                },
                icon: pageIndex == 0
                    ? const Icon(
                  Icons.home_filled,
                  color: Colors.white,
                  size: 35,
                )
                    : const Icon(
                  Icons.home_outlined,
                  color: Colors.white,
                  size: 35,
                ),
              ),
              IconButton(
                enableFeedback: false,
                onPressed: () {
                  setState(() {
                    pageIndex = 1;
                  });
                },
                icon: pageIndex == 1
                    ? const Icon(
                  Icons.work_rounded,
                  color: Colors.white,
                  size: 35,
                )
                    : const Icon(
                  Icons.work_outline_outlined,
                  color: Colors.white,
                  size: 35,
                ),
              ),
              IconButton(
                enableFeedback: false,
                onPressed: () {
                  setState(() {
                    pageIndex = 2;
                  });
                },
                icon: pageIndex == 2
                    ? const Icon(
                  Icons.widgets_rounded,
                  color: Colors.white,
                  size: 35,
                )
                    : const Icon(
                  Icons.widgets_outlined,
                  color: Colors.white,
                  size: 35,
                ),
              ),
              IconButton(
                enableFeedback: false,
                onPressed: () {
                  setState(() {
                    pageIndex = 3;
                  });
                },
                icon: pageIndex == 3
                    ? const Icon(
                  Icons.person,
                  color: Colors.white,
                  size: 35,
                )
                    : const Icon(
                  Icons.person_outline,
                  color: Colors.white,
                  size: 35,
                ),
              ),
            ],
          ),
        );
      }
    }
    
    class Page1 extends StatelessWidget {
      const Page1({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: const Color(0xffC4DFCB),
          child: Center(
            child: Text(
              "Page Number 1",
              style: TextStyle(
                color: Colors.green[900],
                fontSize: 45,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        );
      }
    }
    
    class Page2 extends StatelessWidget {
      const Page2({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: const Color(0xffC4DFCB),
          child: Center(
            child: Text(
              "Page Number 2",
              style: TextStyle(
                color: Colors.green[900],
                fontSize: 45,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        );
      }
    }
    
    class Page3 extends StatelessWidget {
      const Page3({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: const Color(0xffC4DFCB),
          child: Center(
            child: Text(
              "Page Number 3",
              style: TextStyle(
                color: Colors.green[900],
                fontSize: 45,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        );
      }
    }
    
    class Page4 extends StatelessWidget {
      const Page4({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          color: const Color(0xffC4DFCB),
          child: Center(
            child: Text(
              "Page Number 4",
              style: TextStyle(
                color: Colors.green[900],
                fontSize: 45,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2020-02-07
      • 2020-07-30
      • 2020-02-11
      相关资源
      最近更新 更多