Flutter - 使用底部导航栏图标的多页面导航

标签 flutter dart flutter-bottomnavigation materialpageroute

我正在尝试使用底部导航栏中的图标导航到我的应用程序中的不同页面。我已经尝试了很多教程,但似乎无法找到实现这一目标的最佳方法。我已经创建了我的主页(下面的代码)和 2 个附加页面,收件箱和登录,都返回简单的脚手架。
首先,我想知道这是否是我想要实现的最佳方式,其次,如何更改我的代码以允许我根据点击的图标导航到不同的页面。我知道下面的代码没有执行,我只是想展示我的尝试。
我的代码:

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  _onTap(int index) {
    Navigator.of(context)
        .push(MaterialPageRoute<Null>(builder: (BuildContext context) {
      return _children[_currentIndex];
    }));}

  final List<Widget> _children = [
    HomePage(),
    InboxPage(),
    SignInPage()
  ];

  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
      SizeConfig().init(context);
      return Scaffold(
        appBar: PreferredSize(preferredSize: Size(double.infinity, 75),
          child: AppBar(
              elevation: 0.0,
              centerTitle: false,
              title: Column(
                children: <Widget>[
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      currentDate,
                      textAlign: TextAlign.left,
                      style: TextStyle(
                          color: titleTextColor,
                          fontWeight: subTitleFontWeight,
                          fontFamily: titleFontFamily,
                          fontSize: subTitleFontSize),
                    ),
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      'Some text here',
                      style: TextStyle(
                          color: titleTextColor,
                          fontWeight: titleTextFontWeight,
                          fontFamily: titleFontFamily,
                          fontSize: titleFontSize),
                    ),
                  ),
                ],
              ),
              backgroundColor: kPrimaryColor,
              shape: titleBarRounding
          ),
        ),
        body: BodyOne(),
        bottomNavigationBar: BottomNavigationBar(
            currentIndex: _currentIndex,
            type: BottomNavigationBarType.fixed,
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text('Home'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.mail),
                title: Text('Inbox'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.account_circle),
                title: Text('Account'),


              )
            ],
          onTap: () => _onTap(_currentIndex),
        ),);
    }
  }
提前致谢。

最佳答案

您所在的屏幕不能是您要导航到的屏幕的一部分,而且您无需每次更改时都推送新屏幕 selectedPage ,这是它的外观示例:

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int selectedPage = 0;

  final _pageOptions = [
    HomeScreen(),
    InboxScreen(),
    SignInScreen()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: _pageOptions[selectedPage],
        bottomNavigationBar: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home, size: 30), title: Text('Home')),
            BottomNavigationBarItem(icon: Icon(Icons.mail, size: 30), title: Text('Inbox')),
            BottomNavigationBarItem(icon: Icon(Icons.account_circle, size: 30), title: Text('Account')),
          ],
          selectedItemColor: Colors.green,
          elevation: 5.0,
          unselectedItemColor: Colors.green[900],
          currentIndex: selectedPage,
          backgroundColor: Colors.white,
          onTap: (index){
            setState(() {
              selectedPage = index;
            });
          },
        )
    );
  }
}
如果您需要更多解释,请告诉我。

关于Flutter - 使用底部导航栏图标的多页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63290506/

相关文章:

flutter - flutter 如何在BottomNavigationBar中禁用幻灯片动画

flutter - 在Flutter中对JSON列表进行排序

Flutter - 构建游戏的基础知识

ios - 如何在列表底部显示一个按钮?

firebase - SetState为String会返回null,使用Flutter进行Firestore

flutter - 如何获得 flutter 的future.wait响应?

Flutter - flutter-web 中的条件库导入

flutter - 如何使用flutter table_calendar构建具有不同颜色的多个标记?

flutter - 如何设置 BottomNavigationBarItem 标签的样式

flutter - 如何在不影响后台内容的情况下弯曲底部导航栏角? flutter