【问题标题】:How to move to a new pages using bottomnavigationbar in flutter?如何在颤动中使用底部导航栏移动到新页面?
【发布时间】:2022-01-22 15:47:15
【问题描述】:

在颤振中使用底部导航栏中的三个图标时,我想移动到三个新页面(根据我下面的代码)。我尝试了几种方法,但代码不起作用,也无法按照我的要求弄清楚。

在这种情况下,最重要的是使用现有代码并使用 bottomNavigationBar 更改移动到新页面所需的更改。

如果有人知道如何使用 bottomNavigationBar(使用我现有的代码)导航到新页面,请告诉我。

提前谢谢你。

代码:

class dashboard extends StatefulWidget {
  @override
  _dashboardState createState() => _dashboardState();
}

// ignore: camel_case_types
class _dashboardState extends State<dashboard> {
  int currentIndex = 1;

  changeIndex(index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final authService = Provider.of<AuthService>(context);
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 80.0, right: 250),
              child: Center(
                child: Container(
                  width: 200.0,
                  height: 20.0,
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(15.0)),
                  child: (const Text(
                    'Hello',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontWeight: FontWeight.bold, color: Colors.black),
                  )),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 300.0, top: 1.0),
              child: IconButton(
                icon: new Icon(Icons.account_circle, size: 30.0),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => HomePage(),
                    ),
                  );
                },
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 300.0, top: 5.0),
              child: IconButton(
                icon: const Icon(
                  Icons.notifications,
                  size: 25.0,
                ),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => Notifications(),
                    ),
                  );
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 0.0),
              child: Center(
                child: Container(
                  width: 390,
                  height: 450,
                  decoration: BoxDecoration(
                      color: Colors.green.shade100,
                      borderRadius: BorderRadius.circular(10.0)),
                ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(onPressed: () async {
        await authService.signOut();
      }),
      //  : _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.green[100],
        items: const [
          BottomNavigationBarItem(
            //I want to navigate to a new page Library();
            icon: Icon(Icons.book_online),
            label: 'Library',
          ),
          BottomNavigationBarItem(
            //I want to navigate to a new page Store();
            icon: Icon(Icons.read_more),
            label: 'Store',
          ),
          BottomNavigationBarItem(
            //I want to navigate to a new page Profile();
            icon: Icon(Icons.account_circle),
            label: 'Profile',
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

  • stackoverflow.com/a/70343804/13912777 请检查这个答案。
  • 我检查了答案,它奏效了。但是现在导航到相关页面后,我无法从新页面移回上一页(仪表板)。这有什么原因吗?我尝试使用其他方式移至其他页面,并且能够移回上一页。但是现在我在使用底部导航栏移动到新页面后无法返回上一页。有什么原因吗?
  • 谢谢。它现在起作用了。我使用 if (index == 2) { Navigator.of(context).push( new MaterialPageRoute( builder: (context) =&gt; HomePage(), ), ); } 而不是 if (index == 2) { Navigator.of(context).pushReplacement( new MaterialPageRoute( builder: (context) =&gt; HomePage(), ), ); } 它现在按要求工作。非常感谢!

标签: flutter dart flutter-bottomnavigation flutter-routes


【解决方案1】:

首先,您需要在按下底部导航时声明页面目的地

  final List<Widget> _children = [
    Library(),
    Store(),
    Profile(),
  ];

然后将_children 作为主体放在Scaffold 上。

 return Scaffold(
      body:_children[currentIndex]
 )

因为您有仪表板类,您需要单独的类来包含 dashboard 并将其添加到 _children(请记住,索引从 0 开始,如数组)。之后,创建一个函数来处理按下选项卡时的事件

  void onTabTapped(int index) {
      setState(() {currentIndex = index;});
  }

在底部导航的属性上添加onTabTapped

    bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.green[100],
        onTap: onTabTapped,
        items: const [
          BottomNavigationBarItem(
            //I want to navigate to a new page Library();
            icon: Icon(Icons.book_online),
            label: 'Library',
          ),
          BottomNavigationBarItem(
            //I want to navigate to a new page Store();
            icon: Icon(Icons.read_more),
            label: 'Store',
          ),
          BottomNavigationBarItem(
            //I want to navigate to a new page Profile();
            icon: Icon(Icons.account_circle),
            label: 'Profile',
          ),
        ],
      ),

【讨论】:

  • 我怎样才能将_children 作为正文放在Scaffold 上?你能分解一下吗?
  • 好的,我会编辑答案
猜你喜欢
  • 2022-01-15
  • 1970-01-01
  • 2022-11-29
  • 2021-08-19
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 2022-09-27
  • 2019-12-15
相关资源
最近更新 更多