GlobalKey _scrollTextKey = new GlobalKey();
Timer _scrollTextTimer;
ScrollController _controller;
int index=0;
@override
void initState() {
// TODO: implement initState
super.initState();
_controller = new ScrollController(initialScrollOffset: 0);
if(!_controller.hasClients){//防止要滚动的文本控件需不显示时(如没有拿到数据) ScrollController没有能滚动的对象,导致_controller.animateTo()报空指针
WidgetsBinding.instance.addPostFrameCallback((callback){
_scrollTextTimer= Timer.periodic(new Duration(seconds: 2), (timer){
if(_gonggaoKey.currentContext==null){
index+=44;
}else{
index+=_scrollTextKey.currentContext.size.height.toInt();
_controller.animateTo((index).toDouble(), duration: new Duration(seconds: 2), curve: Curves.easeOutSine);
//滚动到底部从头开始
if((index-_scrollTextKey .currentContext.size.height.toInt()).toDouble()>_controller.position.maxScrollExtent){
_controller.jumpTo(_controller.position.minScrollExtent);
index=0;
}
}
});
});
}
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_controller.dispose();
_scrollTextTimer?.cancel();
_scrollTextTimer = null;
}
_scollTextView(){
return ListView.builder(
key:_scrollTextKey,
scrollDirection: Axis.vertical, // 纵向滚动
controller: _controller, // 滚动的controller
itemExtent: ScreenUtil().setHeight(72),
physics: new NeverScrollableScrollPhysics(),
itemCount: 3,
itemBuilder: (context, index) {
return Container(
alignment: Alignment.centerLeft,
child: WTextView('滚动文本',style: getTextStyle(fontSize: ScreenUtil().setSp(24)),),
);
},
)
}
版权声明:本文为androidhyf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。