Skip to content

Instantly share code, notes, and snippets.

@putraxor
Created March 22, 2018 14:27
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save putraxor/6437465b7cdc3b5134c39b8a978801f7 to your computer and use it in GitHub Desktop.
Save putraxor/6437465b7cdc3b5134c39b8a978801f7 to your computer and use it in GitHub Desktop.
Flutter infinite scrolling
import 'dart:async';
import 'package:flutter/material.dart';
class InfiniteScroll extends StatefulWidget {
@override
_InfiniteScrollState createState() => new _InfiniteScrollState();
}
class _InfiniteScrollState extends State<InfiniteScroll> {
List<String> _data = [];
Future<List<String>> _future;
int _currentPage = 0, _limit = 10;
ScrollController _controller =
ScrollController(initialScrollOffset: 0.0, keepScrollOffset: true);
///constructor
_InfiniteScrollState() {
_controller.addListener(() {
var isEnd = _controller.offset == _controller.position.maxScrollExtent;
if (isEnd)
setState(() {
_future = loadData();
});
});
_future = loadData();
}
///Mimic load data
Future<List<String>> loadData() async {
for (var i = _currentPage; i < _currentPage + _limit; i++) {
_data.add('Data item - $i');
}
_currentPage += _limit;
return _data;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text('Infinite Scrolling'),
),
body: FutureBuilder(
future: _future,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<String> loaded = snapshot.data;
return ListView.builder(
itemCount: loaded.length,
controller: _controller,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(loaded[index]),
onTap: () {},
);
},
);
}
return LinearProgressIndicator();
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment