转自https://blog.csdn.net/u011272795/article/details/82719640
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: MyHomePage(),
));
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List list = new List(); //列表要展示的数据
ScrollController _scrollController = ScrollController(); //listview的控制器
int _page = 1; //加载的页数
bool isLoading = false; //是否正在加载数据
@override
void initState() {
// TODO: implement initState
super.initState();
getData();
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
print('滑动到了最底部');
_getMore();
}
});
}
/**
* 初始化list数据 加延时模仿网络请求
*/
Future getData() async {
await Future.delayed(Duration(seconds: 2), () {
setState(() {
list = List.generate(15, (i) => '哈喽,我是原始数据 $i');
});
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text('ssss'),
),
body: RefreshIndicator(
onRefresh: _onRefresh,
child: ListView.builder(
itemBuilder: _renderRow,
itemCount: list.length + 1,
controller: _scrollController,
),
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
Widget _renderRow(BuildContext context, int index) {
if (index < list.length) {
return ListTile(
title: Text(list[index]),
);
}
return _getMoreWidget();
}
/**
* 下拉刷新方法,为list重新赋值
*/
Future<Null> _onRefresh() async {
if(!isLoading){
setState(() {
isLoading = true;
});
await Future.delayed(Duration(seconds: 2), () {
print('refresh');
setState(() {
list = List.generate(20, (i) => '哈喽,我是新刷新的 $i');
isLoading = false;
});
});
}
}
/**
* 上拉加载更多
*/
Future _getMore() async {
if (!isLoading) {
setState(() {
isLoading = true;
});
await Future.delayed(Duration(seconds: 1), () {
print('加载更多');
setState(() {
list.addAll(List.generate(5, (i) => '第$_page次上拉来的数据'));
_page++;
isLoading = false;
});
});
}
}
/**
* 加载更多时显示的组件,给用户提示
*/
Widget _getMoreWidget() {
return Center(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'加载中...',
style: TextStyle(fontSize: 16.0),
),
CircularProgressIndicator(
strokeWidth: 1.0,
)
],
),
),
);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_scrollController.dispose();
}
}