问题描述
在我的应用程序中,我使用要显示的大量页面、要渲染的图像等等,滑动时发生的情况是我的滑动动画会在我的页面在滑动中呈现时交错,我想尽量减少这种影响。
我想让页面默认显示某个小部件(最好是带有 bg 颜色的单个容器,或微光,或我可以选择的任何占位符),然后在滑动动画完成时显示其中的数据后台会出现正在计算中。像这样,我不会在滑动时看到卡顿的动画。
除了必须手动使用流构建器来监听 pageView 控制器之外,还有什么方法可以做到这一点吗?
解决方法
给Pages添加一个延迟,等于PageView的动画时长。
1.创建一个变量,
final duration = Duration(milliseconds: 600);
2.给Page()添加延迟,
Future.delayed(duration,() => Page() );
3.动画到不同的页面,
_pageController.animateToPage(
1,duration: duration,curve: Curves.easeInOut,);
,
你可以试试这个,preload_page_view,屏幕外的浏览量会被预加载~
[编辑] 另一种选择,如果你只把上下文当 index == onchangedIndex ,比如,
int _currentIndex = 0;
PageView.builder(
onPageChanged: (index){
//Even you can use here Future.delay to delay a second after onPageChanged,if loading your content is too quick
setState(() {
_currentIndex = index;
});
},itemBuilder: (context,index){
return _currentIndex == index ? //your content : Container(/*with background color*/);
}