Célia
Célia

Reputation: 93

Flutter navigation with the bottomNavigationBar between pages crash the app

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

    );
  }
}

Upvotes: 0

Views: 309

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17762

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

Upvotes: 1

Related Questions