1
0
mirror of https://github.com/git-touch/git-touch synced 2025-01-27 22:29:20 +01:00

fix: remove loading when reach to end

This commit is contained in:
Rongjian Zhang 2019-02-09 14:20:21 +08:00
parent 66c040e523
commit 6933c04472
5 changed files with 19 additions and 14 deletions

View File

@ -8,9 +8,9 @@ import '../widgets/loading.dart';
class ListPayload<T, K> { class ListPayload<T, K> {
K cursor; K cursor;
List<T> items; List<T> items;
bool end; bool hasMore;
ListPayload({this.items, this.cursor, this.end}); ListPayload({this.items, this.cursor, this.hasMore});
} }
// This is a scaffold for infinite scroll screens // This is a scaffold for infinite scroll screens
@ -42,7 +42,7 @@ class _ListScaffoldState<T, K> extends State<ListScaffold<T, K>> {
List<T> items = []; List<T> items = [];
K cursor; K cursor;
bool end; bool hasMore;
ScrollController _controller = ScrollController(); ScrollController _controller = ScrollController();
@ -54,7 +54,8 @@ class _ListScaffoldState<T, K> extends State<ListScaffold<T, K>> {
if (_controller.offset + 100 > _controller.position.maxScrollExtent && if (_controller.offset + 100 > _controller.position.maxScrollExtent &&
!_controller.position.outOfRange && !_controller.position.outOfRange &&
!loading && !loading &&
!loadingMore) { !loadingMore &&
hasMore) {
_loadMore(); _loadMore();
} }
}); });
@ -70,7 +71,7 @@ class _ListScaffoldState<T, K> extends State<ListScaffold<T, K>> {
var _payload = await widget.onRefresh(); var _payload = await widget.onRefresh();
items = _payload.items; items = _payload.items;
cursor = _payload.cursor; cursor = _payload.cursor;
end = _payload.end; hasMore = _payload.hasMore;
} catch (err) { } catch (err) {
// print(err); // print(err);
error = err.toString(); error = err.toString();
@ -92,7 +93,7 @@ class _ListScaffoldState<T, K> extends State<ListScaffold<T, K>> {
var _payload = await widget.onLoadMore(cursor); var _payload = await widget.onLoadMore(cursor);
items.addAll(_payload.items); items.addAll(_payload.items);
cursor = _payload.cursor; cursor = _payload.cursor;
end = _payload.end; hasMore = _payload.hasMore;
} catch (err) { } catch (err) {
print(err); print(err);
} finally { } finally {
@ -106,7 +107,11 @@ class _ListScaffoldState<T, K> extends State<ListScaffold<T, K>> {
Widget _buildItem(BuildContext context, int index) { Widget _buildItem(BuildContext context, int index) {
if (index == 2 * items.length) { if (index == 2 * items.length) {
return Loading(more: true); if (hasMore) {
return Loading(more: true);
} else {
return Container();
}
} }
if (index % 2 == 1) { if (index % 2 == 1) {

View File

@ -14,8 +14,8 @@ class NewsScreenState extends State<NewsScreen> {
Future<List<Event>> fetchEvents(int page) async { Future<List<Event>> fetchEvents(int page) async {
var settings = SettingsProvider.of(context); var settings = SettingsProvider.of(context);
var login = settings.activeLogin; var login = settings.activeLogin;
List data = await settings List data = await settings.getWithCredentials(
.getWithCredentials('/users/$login/received_events?page=$page'); '/users/$login/received_events?page=$page&per_page=$pageSize');
// print(data); // print(data);
return data.map<Event>((item) => Event.fromJSON(item)).toList(); return data.map<Event>((item) => Event.fromJSON(item)).toList();
} }
@ -31,12 +31,12 @@ class NewsScreenState extends State<NewsScreen> {
var page = 1; var page = 1;
var items = await fetchEvents(page); var items = await fetchEvents(page);
return ListPayload( return ListPayload(
cursor: page + 1, end: items.length < 30, items: items); cursor: page + 1, hasMore: items.length == pageSize, items: items);
}, },
onLoadMore: (page) async { onLoadMore: (page) async {
var items = await fetchEvents(page); var items = await fetchEvents(page);
return ListPayload( return ListPayload(
cursor: page + 1, end: items.length < 30, items: items); cursor: page + 1, hasMore: items.length == pageSize, items: items);
}, },
); );
} }

View File

@ -43,7 +43,7 @@ class _ReposScreenState extends State<ReposScreen> {
return ListPayload( return ListPayload(
cursor: repo["pageInfo"]["endCursor"], cursor: repo["pageInfo"]["endCursor"],
items: repo["nodes"], items: repo["nodes"],
end: repo['pageInfo']['hasNextPage'], hasMore: repo['pageInfo']['hasNextPage'],
); );
} }

View File

@ -42,7 +42,7 @@ class _UsersScreenState extends State<UsersScreen> {
return ListPayload( return ListPayload(
cursor: repo["pageInfo"]["endCursor"], cursor: repo["pageInfo"]["endCursor"],
end: repo['pageInfo']['hasNextPage'], hasMore: repo['pageInfo']['hasNextPage'],
items: repo["nodes"], items: repo["nodes"],
); );
} }

View File

@ -155,7 +155,7 @@ class Palette {
static const gray = Color(0xff959da5); static const gray = Color(0xff959da5);
} }
final pageSize = 20; final pageSize = 30;
final graphqlChunk1 = ''' final graphqlChunk1 = '''
title title