2021年3月21日星期日

How to use bottomNavigationBar in a separate file?

I have the following code that is working properly:

import 'package:flutter/material.dart';    import 'screen_curiosities.dart';  import 'screen_movies.dart';  import 'screen_releases.dart';    import '../utils/side_menu.dart';  import '../utils/bottom_menu.dart';    class Home extends StatefulWidget {    @override    _HomeState createState() => _HomeState();  }    class _HomeState extends State<Home> {      int selectedIndex = 0;      List screens = [      ScreenMovies(),      ScreenReleases(),      ScreenCuriosities()    ];      void onClicked(int index) {      setState(() {        selectedIndex = index;      });    }      @override    Widget build(BuildContext context) {        return Scaffold(        appBar: AppBar(          title: Text('Movies'),          backgroundColor: Colors.black,        ),        body: Center(          child: screens.elementAt(selectedIndex),        ),        drawer: SideMenu(),        bottomNavigationBar: BottomNavigationBar(          items:[            BottomNavigationBarItem(              icon: Icon(Icons.home),              label: 'Movies',            ),            BottomNavigationBarItem(              icon: Icon(Icons.new_releases),              label: 'Releases',            ),            BottomNavigationBarItem(              icon: Icon(Icons.question_answer),              label: 'Curiosities',            )          ],          currentIndex: selectedIndex,          onTap: onClicked,          selectedItemColor: Colors.red[800],          backgroundColor: Colors.black,          unselectedItemColor: Colors.white,        )      );    }    }  

Now I am trying to separate the Widget BottomNavigationBar to another file and call him on the property "bottomNavigationBar" from Scaffold. It would be like this:

bottomNavigationBar: BottomMenu()  

I did it with the Widget Drawer and it worked, but when I tried the same thing with bottomNavigationBar it wasn't successful.

When I try to use the variable selectedIndex in the new Widget it is always undefined.

I tried many things, but I couldn't solve this. Is there any way to use the Widget bottomNavigationBar in a separated file?

https://stackoverflow.com/questions/66739156/how-to-use-bottomnavigationbar-in-a-separate-file March 22, 2021 at 09:05AM

没有评论:

发表评论