【发布时间】:2020-06-12 16:18:15
【问题描述】:
这里我有 bottomNavigationBar 在页面之间移动,所以当主页加载时所有类都将加载并运行 initState ,但在 initState 我有http请求获取数据;所以当我第一次显示主页时所有的http请求都会运行...... 只有当用户滑动到该页面时,我才需要运行该页面的 http 请求
class Home extends StatefulWidget {
const Home({Key key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: IndexedStack(
index: _currentIndex,
children: [
Partner(),
Partners(),
],
),
),
bottomNavigationBar: BottomNavigationBar(
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
title: Text('Partner'),
),
BottomNavigationBarItem(
title: Text('Partners'),
),
],
),
);
}
}
【问题讨论】: