i'hve a flutter project, when i tried to make a invinite listview but scroll controller not work, no error. also refresh indicator not work to, but if primary false on listview refresh work.
for information, my listview not work if primary set to false, because this listview inside listview on main.dart code
List<dynamic> list = [];
ScrollController _scrollController = ScrollController();
void initState() {
super.initState();
_loadData();
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent - 50) {
_loadData();
}
});
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
Future<dynamic> _loadData() {
var offset = list.length;
return Api().postData("resto",
body: <String, String>{"offset": "$offset"}).then((dynamic res) {
if (res["error"]) throw new Exception(res["msg"]);
setState(() {
list.addAll(res['data']);
});
});
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
FlatButton(
child: Text(
"Filter Popularity",
style: TextStyle(color: Colors.red),
),
onPressed: () {},
),
Padding(
padding: const EdgeInsets.all(8.0),
child: list.length == 0 ? _shimmer() : _listResto()),
],
);
}
Widget _listResto() {
return ListView.builder(
controller: _scrollController,
primary: false,
shrinkWrap: true,
itemCount: list == null ? 0 : list.length,
itemBuilder: (context, i) {
Solution 1: Orlay Garcia Duconge
add physics: const AlwaysScrollableScrollPhysics()
to RefreshIndicator
body: RefreshIndicator(
onRefresh: _refreshList,
child: ListView.builder(
controller: _scrollController,
physics: const AlwaysScrollableScrollPhysics(),
itemCount: items.length,
itemBuilder: (context, index) => ListTile(
title: Text('Item ${items[index]}'),
),
),
)