码迷,mamicode.com
首页 > 其他好文 > 详细

ListView的监听

时间:2019-05-14 12:56:25      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:position   als   image   refresh   max   pos   ESS   lex   pixel   

ListView可以配合ScrollController使用:
创建ScrollController _ScrollController=ScrollController();
在initState中监听如下:
void initState() {
_scrollController.addListener((){
if(_scrollController.position.pixels==_scrollController.position.maxScrollExtent){
setState(() {
if(_itemcount<18){_itemcount++;}
});
}
});
super.initState();
}
监听过程中注意:
_scrollController.position.pixels==_scrollController.position.maxScrollExtent  这个是固定写法,请勿更改!意思是监控底部的上拉动作;
setState()操作必不可少,可以及时刷新画面;

销毁:

@override
void dispose() {
// TODO: implement dispose
_scrollController.dispose();
super.dispose();
}


示例:
import ‘package:flutter/material.dart‘;

import ‘dart:ui‘;

void main()=>runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
ScrollController _scrollController=ScrollController();
int _itemcount=3;
@override
void initState() {
// TODO: implement initState
_scrollController.addListener((){
if(_scrollController.position.pixels==_scrollController.position.maxScrollExtent){
Future.delayed(Duration(seconds: 1));
setState(() {
if(_itemcount<18){_itemcount++;}
});
}
});
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(‘RefreshIndicator‘),),
body: RefreshIndicator(
onRefresh: ()async{
await Future.delayed(Duration(seconds: 1));
setState(() {
if(_itemcount>=4){_itemcount--;}
});
},
child: ListView.builder(
controller: _scrollController,
itemCount: _itemcount,
itemBuilder: (context,index){
return Container(
width: window.physicalSize.width,
height: 200,
child: Image.network(‘http://www.ecobentech.com/images/$index.jpg‘,fit: BoxFit.fitWidth,),
);
},
)
),
);
}
}

ListView的监听

标签:position   als   image   refresh   max   pos   ESS   lex   pixel   

原文地址:https://www.cnblogs.com/braveheart007/p/10861387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!