简体   繁体   English

如何在 flutter 的新屏幕上隐藏底部导航栏?

[英]how to hide Bottom Navigation Bar on new screen in flutter?

Just like here when I click on the timer, the Bottom navigation bar was disappeared.就像这里当我点击计时器时,底部导航栏消失了。 I want to implement the same thing on flutter.我想在 flutter 上实现同样的事情。 Whenever I click on Bottom Navigation Bar Item, for the new screen the Bottom Navigation Bar should not appear.每当我单击底部导航栏项目时,对于新屏幕,底部导航栏不应出现。

期望的输出

Here is my code.这是我的代码。 My Bottom Navigation Bar has four items and I want to hide the bottom navigation bar when I route to a new screen.我的底部导航栏有四个项目,当我路由到新屏幕时,我想隐藏底部导航栏。

class MyFeedScreen extends StatefulWidget {
     @override
  _MyFeedScreenState createState() => _MyFeedScreenState();
}

class _MyFeedScreenState extends State<MyFeedScreen> {
  int _bottomNavIndex = 0;

  Widget pageCaller(int index) {
    switch (index) {
      case 0:
        {
          return Category();
        }
      case 1:
        {
          return Feed();
        }
      case 3:
        {
          return Settings();
        }
    }
  }

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    return Scaffold(
      body: pageCaller(_bottomNavIndex),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: klogoBlue,
        selectedItemColor: Color(0xfff5f5f5),
        unselectedItemColor: Color(0xfff5f5f5),
        selectedFontSize: 12.0,
        type: BottomNavigationBarType.fixed,
        currentIndex: _bottomNavIndex,
        onTap: (index) {
          setState(() {
            _bottomNavIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Padding(
              padding:
                  EdgeInsets.only(top: SizeConfig.blockSizeVertical * 0.60),
              child: Icon(Icons.category),
            ),
            title: Padding(
              padding: EdgeInsets.symmetric(
                  vertical: SizeConfig.blockSizeVertical * 0.60),
              child: Text('Category'),
            ),
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding:
                  EdgeInsets.only(top: SizeConfig.blockSizeVertical * 0.60),
              child: Icon(FontAwesomeIcons.newspaper),
            ),
            title: Padding(
              padding: EdgeInsets.symmetric(
                  vertical: SizeConfig.blockSizeVertical * 0.60),
              child: Text('My Feed'),
            ),
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding:
                  EdgeInsets.only(top: SizeConfig.blockSizeVertical * 0.60),
              child: Icon(Icons.refresh),
            ),
            title: Padding(
              padding: EdgeInsets.symmetric(
                  vertical: SizeConfig.blockSizeVertical * 0.60),
              child: Text('Refresh'),
            ),
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding:
                  EdgeInsets.only(top: SizeConfig.blockSizeVertical * 0.60),
              child: Icon(Icons.settings),
            ),
            title: Padding(
              padding: EdgeInsets.symmetric(
                  vertical: SizeConfig.blockSizeVertical * 0.60),
              child: Text('Settings'),
            ),
          ),
        ],
      ),
    );
  }
}

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can check onTap index and do Navigator.push for specific button您可以检查onTap index并对特定按钮执行Navigator.push

code snippet代码片段

void _onItemTapped(int index) {

    if (index != 2) {
      setState(() {
        _bottomNavIndex = index;
      });

      print(_bottomNavIndex);
    } else {
      Navigator.of(context).push(
        MaterialPageRoute(builder: (context) => Settings()),
      );
    }

  }

  Widget pageCaller(int index) {
    switch (index) {
      case 0:
        {
          return Category();
        }
      case 1:
        {
          return Feed();
        }
    }
  }

working demo工作演示

在此处输入图像描述

full code完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _bottomNavIndex = 0;

  void _onItemTapped(int index) {

    if (index != 2) {
      setState(() {
        _bottomNavIndex = index;
      });

      print(_bottomNavIndex);
    } else {
      Navigator.of(context).push(
        MaterialPageRoute(builder: (context) => Settings()),
      );
    }

  }

  Widget pageCaller(int index) {
    switch (index) {
      case 0:
        {
          return Category();
        }
      case 1:
        {
          return Feed();
        }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home'),
      ),
      body: Center(
        child: pageCaller(_bottomNavIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text('Category'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            title: Text('Feed'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            title: Text('Settings'),
          ),
        ],
        currentIndex: _bottomNavIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

class Category extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("Category"),
      ),
    );
  }
}

class Feed extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("Feed"),
      ),
    );
  }
}

class Settings extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Settings'),
      ),
      body: Center(
        child: Text("Settings"),
      ),
    );
  }
}

You can try this method你可以试试这个方法

    Navigator.of(context, rootNavigator: true).push(MaterialPageRoute(
              builder: (_) => NewScreen(),
            ),
          );

If we are using multiple routes in single app and want to use top level routes this code is working thanks.如果我们在单个应用程序中使用多个路由并且想要使用顶级路由,则此代码可以正常工作,谢谢。

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

相关问题 Flutter:如何在永久底部导航栏中导航到新屏幕 package - Flutter: How can I Navigate to new screen in Persistent Bottom Navigation Bar package 如何使用提供程序 flutter 隐藏底部导航栏 - How to Hide bottom navigation bar using provider flutter 如何在颤动中隐藏android的底部导航栏 - How to hide android's bottom navigation bar in flutter Flutter 如何在下一页隐藏 Cupertino 底部导航栏 - Flutter how to hide Cupertino bottom navigation bar at next page 如何使用底部导航栏导航到特定屏幕 - How can you navigate to a specific screen in flutter with the bottom navigation bar 在flutter中如何使底部导航栏在访问前不加载屏幕 - How to make bottom navigation bar not to load the screen before visiting in flutter 如何通过传递选定的索引在 flutter 底部导航栏上打开屏幕 - How to open a screen on flutter bottom navigation bar by passing selected index 如果购物车为空,则隐藏底部导航栏 flutter - hide the bottom navigation bar if cart is empty flutter 在 Flutter 滚动时隐藏底部导航栏 - Hide Bottom Navigation bar on Scroll in Flutter 底部导航栏隐藏屏幕 - Flutter - Bottom Navigation Bar hides the screen - Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM