Как направить пользовательский нижний навигатор? - PullRequest
0 голосов
/ 24 апреля 2020

Как направить пользовательский навигатор, если маршрутизации нет? Я пробовал много вариантов, но я не могу направить обе кнопки. Работала только HomePage.

Код ниже:

    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(),
              ),
            );
          },
        ),

1 Ответ

0 голосов
/ 24 апреля 2020

Вы можете попробовать использовать условие 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(),
          ),
        );
            }
          },
        ),
      );
      }
}

Счастливого кодирования !!!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...