【发布时间】:2019-06-20 07:09:37
【问题描述】:
我正在写一个聊天应用程序,但是我在实现下拉加载更多数据的效果时遇到了问题。
我要达到的效果:
- 用户发送的聊天数据将在 ListView 表单中从上到下显示。
- 当用户下拉 ListView 时,会加载旧的聊天数据,但不会立即显示。当用户滚动 ListView 时,应该显示旧的字符数据。
片段:
class _List3PageState extends State<List3Page> {
List<String> _stringList = [];
int i = 0;
@override
void initState() {
super.initState();
for (int i = 0; i < 5; i++) {
_stringList.add('chat data');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('pull down to load more data'),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: _stringList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
child: Text(_stringList[index]),
);
},
),
),
FlatButton(
onPressed: () {
List<String> oldData = [];
oldData.add('chat old data $i');
i += 1;
oldData.add('chat old data $i');
i += 1;
oldData.add('chat old data $i');
i += 1;
oldData.add('chat old data $i');
i += 1;
_stringList.insertAll(0, oldData);
setState(() {});
},
child: Text('load old chat data')),
],
),
);
}
}
【问题讨论】:
标签: flutter