0

I'm trying to create a BottomNavigationBar on my home page screen. The bar itself works perfectly fine, but it's the navigation between pages that crashes the application. My main.dart file returns a registration page, which then takes you to the home page. And it's the "loading" between those two pages that is now crashing the application (it stopped working since the "navigation system" was added).

I think the problem comes from the body: screens[currentIndex], but I don't know how to change it. Every other tutos show the nav bar beign made in the main.dart file, but I can't do that...

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final screens = [
    HomeScreen(),
    ProfilScreen(),
    ConversationsScreen(),
  ];

  int currentIndex = 0;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //appBar: AppBar(
          //title: Text("App bar"),
          //centerTitle: true,
        //),
        body: screens[currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentIndex,
        backgroundColor: Colors.black26,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Colors.purpleAccent,
        iconSize: 30,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
          BottomNavigationBarItem(
              icon: Icon(Icons.mail_outline), label: "Message"),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: "Profil"),
        ],
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),

    );
  }
}
Célia
  • 63
  • 1
  • 1
  • 5

1 Answers1

1

You have added home screen in the screens list which makes a continuous loop and hence crashed,

Remove homescreen from the screens list and add another screen or the profile screen just to test

Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • But now, the home page is not really a page anymore... I can't really access it, I only see the bottom navigation bar "on top" of the other 3 pages I have that are accessible through the icons bar. The first page I see is the Profil page yet I should see the Home page, but I only see the navbar of the Home page, like I said on top of the others pages. And no other pages have the navBar coded. – Célia Jul 22 '22 at 13:22
  • You should have a container page with bottom navigation bar and 4 different pages which are added as screens in the container page. – Kaushik Chandru Jul 22 '22 at 13:34