Flutter 如何在下一页隐藏 Cupertino 底部导航栏

问题描述

我目前正在处理一个需要使用 Cupertino 小部件构建的项目。一切都很好,直到我尝试不在下一页显示底部导航栏,但底部导航栏仍然从上一页向前显示。下面是我的示例代码

class PageOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
      tabBar: CupertinoTabBar(
        items: [
          BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.person),label: 'Person'),BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.mail),label: 'Mail'),BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.flag),label: 'Flag'),],),tabBuilder: (context,index) {
        return CupertinoTabView(
          routes: {
            'p2': (context) => PageTwo(),},builder: (context) {
            return CupertinopageScaffold(
                backgroundColor: Colors.white,child: Center(
                  child: Column(
                    children: [
                
                      ElevatedButton(
                        onpressed: () {
                          Navigator.pushNamed(context,'p2');
                        },child: Text('Next Page'),));
          },);
      },);
  }
}

class PageTwo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Container(
        color: Colors.white,child: Column(children: [
          Text('Page 2'),ElevatedButton(
            onpressed: () {
              Navigator.pop(context);
            },child: Text('Back'),]),);
  }
}

解决方法

找到解决方案。

第一个解决方案。

只需重复

Navigator.pushNamed(context,'p2');

Navigator.of(context,rootNavigator: true).pushNamed('p2');

第二种解决方案

如果不需要使用,请从 tabBuilder 中删除 CupertinoTabView

  tabBuilder: (context,index) {
        return CupertinoPageScaffold(
          backgroundColor: Colors.white,child: Center(
            child: Column(
              children: [
                SizedBox(
                  height: 50.0,),ElevatedButton(
                  onPressed: () {
                    Navigator.pushNamed(context,'p2');
                  },child: Text('Next Page'),],);
      },

重要:必须为类 CupertinoApp 设置路由

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...