我有下面底部栏的演示代码,但是当我点击第二个选项卡时,相关屏幕没有改变。 这是非常基本的示例,应该更改选项卡,这可能是一个愚蠢的错误,因为我只是颤振的初学者。
class TabScreen extends StatefulWidget {
const TabScreen({super.key});
@override
State<TabScreen> createState() => _TabScreenState();
}
class _TabScreenState extends State<TabScreen> {
@override
Widget build(BuildContext context) {
int currentIndex = 0;
final List<Widget> screens = [const Text('A'), const Text('B')];
return Scaffold(
body: screens[currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentIndex,
onTap: (index) {
setState(() {
currentIndex = index;
});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}
它应该显示第二个选项卡屏幕。
int currentIndex = 0;
应在 _TabScreenState
而不是 build
方法中定义。