在页面之间使用底部导航条进行抖动导航会导致应用程序崩溃



我想在我的主页屏幕上创建一个底部导航栏。这个栏本身工作得很好,但是页面之间的导航会使应用程序崩溃。我的主。Dart文件返回一个注册页面,然后将您带到主页。它是"加载"在这两个页面之间,现在正在崩溃的应用程序(它停止工作,因为"导航系统";增加了)。

我认为问题来自于body: screens[currentIndex],但我不知道如何改变它。每一个教程都显示导航栏是从主界面开始制作的。

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

你已经在屏幕列表中添加了主屏幕,这会导致连续循环并因此崩溃,

从屏幕列表中删除主屏幕,并添加另一个屏幕或配置文件屏幕,只是为了测试

最新更新