Код: Выделить всё
void _scrollListener() {
// Check if we are at the bottom of the list and loading isn't in progress
if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent && !_isLoading) {
_loadMoreItems();
}
}
Future _loadMoreItems() async {
setState(() {
_isLoading = true;
});
// Simulate a network request (wait 2 seconds)
await Future.delayed(Duration(seconds: 2));
// Fetch more items
final newItems = List.generate(10, (index) => _items.length + index);
setState(() {
_isLoading = false;
_items.addAll(newItems);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Lazy Loading ListView')),
body: ListView.builder(
controller: _scrollController,
itemCount: _items.length + 1, // +1 for the loading indicator
itemBuilder: (context, index) {
if (index == _items.length) {
// Show loading indicator at the bottom when more items are being fetched
return _isLoading
? Center(child: CircularProgressIndicator())
: SizedBox.shrink();
}
return ListTile(title: Text('Item ${_items[index]}'));
},
),
);
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... oading-lis