我不止一次读到不建议使用 statefullWidget。BottomNavigationBar
在中切换页面的示例body
。没有statefullWidget
这个例子怎么办?使用块?还是仅适用于业务逻辑?
class HomeScreenTest extends StatefulWidget {
@override
_HomeScreenTestState createState() => _HomeScreenTestState();
}
class _HomeScreenTestState extends State<HomeScreenTest> {
int _currentIndex = 0;
final pages = [
Page1(),
Page2(),
Page3(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: pages[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
title: Text('pag1'),
),
BottomNavigationBarItem(
title: Text('page'),
),
BottomNavigationBarItem(
title: Text('page3'),
),
],
),
);
}
}