【问题标题】:how to navigate using BottomNavigationBar and also with ElevatedButton press in Flutter如何在 Flutter 中使用 BottomNavigationBar 和 ElevatedButton 进行导航
【发布时间】:2021-08-21 14:09:41
【问题描述】:

我在 Flutter 中使用BottomNavigationBar,我想要的是通过正常点击BottomNavigationBarItem 在页面之间导航。同时,导航到其他页面但在同一个BottomNavigationBarItem。让我通过我找到的这个例子来解释更多 Here

说我的BottomNavigationBar有两个BottomNavigationBarItemCallMessageCall 可以导航(例如使用Elevatedbotton 点击)到Page1Page2,而Message 可以导航(通过Elevatedbotton 点击)到Page3page4

像这样:

  • 打电话

    -第1页

    -第2页

  • 留言

    -第3页

    -第4页

This solution 解决了我 50% 的问题,我现在可以做的是从 Call 导航到 page1page2 始终保持 BottomNavigationBar 活着,现在它仍然是第二部分,即正在单击另一个 BottomNavigationBarItem 以导航到消息

【问题讨论】:

  • 所以你说呼叫屏幕可以导航到其他 2 个屏幕,消息屏幕可以导航到另外 2 个屏幕,但底部导航栏会一直存在!对吗?
  • 没错,比如我们在Page1,bottomNav会在Call处着色(因为Page1是Call的子树)

标签: flutter flutter-navigation


【解决方案1】:

你试试这个方法。


class App extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  int _selectedIndex = 0;
  final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
  Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case 'Site':
        return MaterialPageRoute(builder: (context) => SiteScreen());
     
      default:
        return MaterialPageRoute(builder: (context) => Home());
    }
  }

  void _onItemTapped(int index) {
    switch (index) {
      case 1:
        _navigatorKey.currentState!
            .pushNamedAndRemoveUntil("Site", (route) => false);
        break;
      default:
        _navigatorKey.currentState!
            .pushNamedAndRemoveUntil("Home", (route) => false);
    }
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        toolbarHeight: 0,
      ),
      body: Navigator(key: _navigatorKey, onGenerateRoute: generateRoute),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.favorite_border),
            label: 'Site',
          ),
         
        ],
        showSelectedLabels: true,
        showUnselectedLabels: false,
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.white,
        unselectedItemColor: Color(0xFF9e9e9e),
        iconSize: 20,
        backgroundColor: KBlackColor,
        onTap: _onItemTapped,
      ),
    );
  }
}

class Home extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            child: Column(children: [
                TextButton(
                    onPressed: () {
                        MaterialPageRoute(builder: (context) => SubHome())
                     },
                    child: Text('Click'),
                )
            ]),
        );
    }
}

class SubHome extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            child: Column(children: [
                Text('SubHome')
            ]),
        );
    } 
}

【讨论】:

  • 感谢您的评论,但您的代码仅允许在 BottomNavigationBarItem 之间导航,在您的示例中,它们是 homesite,我已经可以做到了。以您为例,我想要从home 导航到另一个名为page1 的页面,例如通过单击ElevatedBottomBottomNavigationBar 仍将显示。
  • 是的。在主页你可以导航到page1BottomNavigationBar 是显示Home
  • 我该怎么做,因为给定的答案仍然不完整,你可以编辑你的答案以达到那个吗?
  • 我已经更新了。你可以检查一下。如果您需要支持,可以联系 FB (facebook.com/linh.nq236)
  • 这很有帮助,谢谢兄弟,编码愉快
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-17
  • 2020-11-06
  • 2022-07-19
  • 2021-06-17
  • 2020-09-18
  • 2021-02-27
相关资源
最近更新 更多