我正试图在我的Ffltter应用程序中添加一个侧边菜单和一个底部导航栏.我已经将代码合并到main_creen.dart文件中.
当我运行代码时,底部导航栏工作正常,但如果我点击侧边菜单项,我会收到以下错误:
_AssertionError ('package:flutter/src/material/bottom_navigation_bar.dart': Failed assertion: line 251 pos 15: '0 <= currentIndex && currentIndex < items.length': is not true.)个
以下是main_creen.dart代码:
class MainScreenState extends State<MainScreen> {
final auth = FirebaseAuth.instance;
int _pageIndex = 0;
final List<Widget> appScreens = [
const CompanyDashboardScreen(),
const TransactionDetailScreen(true),
const AppointmentCalendarScreen(),
const UserProfileScreen(),
const ChatScreen(),
const CompanyScreen(),
const UserProfileScreen(),
];
Future<void> signOut() async {
await auth.signOut();
}
void onItemTapped(int index) {
setState(() {
_pageIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CustomAppBar(),
body: Container(child: appScreens[_pageIndex]),
drawer: Drawer(
child: (ListView(
padding: EdgeInsets.zero,
children: [
const UserAccountsDrawerHeader(
accountName: Text('Billy Bob Baker'),
accountEmail: Text('billy.bob.baker@gmail.com'),
),
Container(
child: Column(
children: <Widget>[
ListTile(
title: const Text('Add Company'),
selected: _pageIndex == 5,
onTap: () {
// Update the state of the app
onItemTapped(5);
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: const Text('Add User'),
selected: _pageIndex == 6,
onTap: () {
// Update the state of the app
onItemTapped(6);
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: const Text('Log Out'),
selected: _pageIndex == 7,
onTap: () {
// Update the state of the app
signOut();
// Then close the drawer
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const LoginScreen()));
},
),
],
),
),
],
)),
),
bottomNavigationBar: BottomNavigationBar( <<< ERROR HERE
backgroundColor: Colors.blueAccent,
selectedIconTheme: const IconThemeData(color: Colors.white),
selectedItemColor: Colors.white,
unselectedItemColor: Colors.black45,
type: BottomNavigationBarType.fixed,
currentIndex: _pageIndex,
onTap: (value) {
setState(() {
_pageIndex = value;
});
},
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
BottomNavigationBarItem(
icon: Icon(Icons.add_business_outlined), label: "Trxn"),
BottomNavigationBarItem(
icon: Icon(Icons.calendar_today), label: "Calendar"),
BottomNavigationBarItem(icon: Icon(Icons.people), label: "User"),
BottomNavigationBarItem(icon: Icon(Icons.chat), label: 'Chat'),
],
),
);
}
// Added this for BottomNavigationBar sync
void setIndex(int index) {
if (mounted) setState(() => _pageIndex = index);
}
}
如何在不相互干扰的情况下组合这两种导航方法?
谢谢