如何路由自定义底部导航器?

问题描述 投票:0回答:1

如果没有路由,如何路由自定义导航器?我尝试了许多替代方法,但无法同时按下两个按钮。仅主页有效。

代码如下:

    BottomNavigationBarItem _bottomIcons(IconData icon) {
    return BottomNavigationBarItem(icon: Icon(icon), title: Text(""));
  }

  @override
  Widget build(BuildContext context) {
    width = MediaQuery.of(context).size.width;
    return Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: LightColor.purple,
          showSelectedLabels: false,
          showUnselectedLabels: false,
          selectedItemColor: Colors.white,
          unselectedItemColor: LightColor.extraDarkPurple,
          type: BottomNavigationBarType.fixed,
          currentIndex: 1,
          items: [
            _bottomIcons(Icons.home),
            _bottomIcons(Icons.arrow_back_ios),

          ],
          onTap: (index) {
            Navigator.pushReplacement(
              context,
              MaterialPageRoute(
                builder: (context) => HomePage(),
              ),
            );
          },
        ),
android android-studio flutter dart flutter-layout
1个回答
0
投票

您可以尝试使用基于索引值的if条件来使用路由导航到两个不同的页面。尝试下面提到的代码。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyButton(),
  ));
}

class MyButton extends StatefulWidget {
  @override
  MyButtonState createState() {
    return MyButtonState();
  }
}

class MyButtonState extends State<MyButton> {
   BottomNavigationBarItem _bottomIcons(IconData icon) {
    return BottomNavigationBarItem(icon: Icon(icon), title: Text(""));
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          backgroundColor: Colors.purple,
          showSelectedLabels: false,
          showUnselectedLabels: false,
          selectedItemColor: Colors.white,
          unselectedItemColor: Colors.blueAccent,
          type: BottomNavigationBarType.fixed,
          currentIndex: 1,
          items: [
            _bottomIcons(Icons.home),
            _bottomIcons(Icons.arrow_back_ios),
          ],
          onTap: (index) {
            if(index == 0){
            Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => HomePage(),
          ),
        );
            }
            else if(index == 1){
              Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => SecondPage(),
          ),
        );
            }
          },
        ),
      );
      }
}

祝您编程愉快!

© www.soinside.com 2019 - 2024. All rights reserved.