一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
编写前(每次点击分类控制器重复请求)
编写前(每次点击分类控制器请求一次不在重复请求)
本章目标:
1.解决首页跟分类来回切换重新加载数据的问题。
这个问题是为了解决 来回点击tab不用不断重复刷新
点击tabs.dart,添加下面红色框的代码。
class _TabsState extends State<Tabs> {
int _currentIndex = 0;
PageController _pageController;
@override
void initState() {
super.initState();
this._pageController=new PageController(initialPage:this._currentIndex );
}
List<Widget> _pageList = [
HomePage(),
CategoryPage(),
CartPage(),
UserPage(),
];
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: PageView(
controller: this._pageController,
children: this._pageList,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: this._currentIndex,
onTap: (int index) {
setState(() {
this._currentIndex=index;
this._pageController.jumpToPage(index);
});
},
type:BottomNavigationBarType.fixed ,
fixedColor:Colors.red,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("首页")
),
BottomNavigationBarItem(
icon: Icon(Icons.category),
title: Text("分类")
),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart),
title: Text("购物车")
),
BottomNavigationBarItem(
icon: Icon(Icons.people),
title: Text("我的")
)
]),
);
}
}
如果页面切换需要保持不变就需要实现以下方法改为 true
with AutomaticKeepAliveClientMixin
bool get wantKeepAlive => true;