git-touch-android-ios-app/lib/scaffolds/list_stateful.dart

220 lines
5.2 KiB
Dart
Raw Normal View History

2019-02-03 07:42:50 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart';
import 'package:git_touch/models/theme.dart';
2019-10-02 08:58:11 +02:00
import 'package:git_touch/scaffolds/common.dart';
2019-09-07 11:08:24 +02:00
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
import '../widgets/error_reload.dart';
import '../widgets/loading.dart';
2019-02-10 05:16:52 +01:00
import '../widgets/empty.dart';
2019-02-03 07:42:50 +01:00
class ListPayload<T, K> {
K cursor;
List<T> items;
2019-02-09 07:20:21 +01:00
bool hasMore;
2020-01-29 07:38:27 +01:00
ListPayload({
@required this.items,
@required this.cursor,
@required this.hasMore,
});
}
2019-02-03 07:42:50 +01:00
// This is a scaffold for infinite scroll screens
2019-09-25 11:06:36 +02:00
class ListStatefulScaffold<T, K> extends StatefulWidget {
2019-02-05 13:57:05 +01:00
final Widget title;
2019-10-03 04:01:54 +02:00
final Widget Function() actionBuilder;
final Widget Function(T payload) itemBuilder;
final Future<ListPayload<T, K>> Function(K cursor) onLoadMore;
2019-02-03 07:42:50 +01:00
2019-09-25 11:06:36 +02:00
ListStatefulScaffold({
2019-02-03 07:42:50 +01:00
@required this.title,
@required this.itemBuilder,
@required this.onLoadMore,
2019-10-03 04:01:54 +02:00
this.actionBuilder,
2019-10-02 08:58:11 +02:00
});
2019-02-03 07:42:50 +01:00
@override
2019-09-25 11:06:36 +02:00
_ListStatefulScaffoldState<T, K> createState() =>
_ListStatefulScaffoldState();
2019-02-03 07:42:50 +01:00
}
2019-09-25 11:06:36 +02:00
class _ListStatefulScaffoldState<T, K>
extends State<ListStatefulScaffold<T, K>> {
2019-02-03 07:42:50 +01:00
bool loading = false;
bool loadingMore = false;
String error = '';
List<T> items = [];
K cursor;
2019-02-09 07:20:21 +01:00
bool hasMore;
2019-02-03 07:42:50 +01:00
ScrollController _controller = ScrollController();
@override
void initState() {
super.initState();
_refresh();
2019-03-10 10:18:38 +01:00
_controller.addListener(onScroll);
2019-02-03 07:42:50 +01:00
}
2019-03-10 09:09:26 +01:00
@override
void dispose() {
_controller.dispose();
super.dispose();
}
2019-03-10 10:18:38 +01:00
void onScroll() {
// Fimber.d(_controller.position.maxScrollExtent - _controller.offset);
2019-03-10 10:18:38 +01:00
if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
!_controller.position.outOfRange &&
!loading &&
!loadingMore &&
hasMore) {
_loadMore();
}
}
2020-10-04 15:42:54 +02:00
// if items not enough, fetch next page
2019-03-10 10:18:38 +01:00
// This should be triggered after build
2020-10-04 15:42:54 +02:00
// TODO: disabled
2019-03-10 10:18:38 +01:00
void _makeSureItemsFill() {
2020-10-04 15:42:54 +02:00
// Future.delayed(Duration(milliseconds: 300)).then((_) {
// onScroll();
// });
2019-03-10 10:18:38 +01:00
}
2019-03-10 09:09:26 +01:00
Future<void> _refresh({bool force = false}) async {
// Fimber.d('list scaffold refresh');
2019-02-03 07:42:50 +01:00
setState(() {
error = '';
2019-02-03 07:42:50 +01:00
loading = true;
2019-03-10 09:09:26 +01:00
if (force) {
items = [];
}
2019-02-03 07:42:50 +01:00
});
try {
2020-10-04 16:10:05 +02:00
var _payload = await widget.onLoadMore(null);
items = _payload.items;
cursor = _payload.cursor;
2019-02-09 07:20:21 +01:00
hasMore = _payload.hasMore;
2019-02-03 07:42:50 +01:00
} catch (err) {
error = err.toString();
throw err;
2019-02-03 07:42:50 +01:00
} finally {
2019-02-06 14:35:52 +01:00
if (mounted) {
setState(() {
loading = false;
});
2019-03-10 10:18:38 +01:00
_makeSureItemsFill();
2019-02-06 14:35:52 +01:00
}
2019-02-03 07:42:50 +01:00
}
}
Future<void> _loadMore() async {
// Fimber.d('list scaffold load more');
2019-02-03 07:42:50 +01:00
setState(() {
loadingMore = true;
});
try {
var _payload = await widget.onLoadMore(cursor);
items.addAll(_payload.items);
cursor = _payload.cursor;
2019-02-09 07:20:21 +01:00
hasMore = _payload.hasMore;
2019-02-03 07:42:50 +01:00
} catch (err) {
2019-03-10 09:09:26 +01:00
error = err.toString();
throw err;
2019-02-03 07:42:50 +01:00
} finally {
2019-02-08 12:44:10 +01:00
if (mounted) {
setState(() {
loadingMore = false;
});
2019-03-10 10:18:38 +01:00
_makeSureItemsFill();
2019-02-08 12:44:10 +01:00
}
2019-02-03 07:42:50 +01:00
}
}
Widget _buildItem(BuildContext context, int index) {
if (index == 2 * items.length) {
2019-02-09 07:20:21 +01:00
if (hasMore) {
return Loading(more: true);
} else {
return Container();
}
2019-02-03 07:42:50 +01:00
}
2019-02-05 13:57:05 +01:00
if (index % 2 == 1) {
2019-10-02 10:09:54 +02:00
return CommonStyle.border;
2019-02-05 13:57:05 +01:00
}
return widget.itemBuilder(items[index ~/ 2]);
2019-02-03 07:42:50 +01:00
}
2019-09-25 11:06:36 +02:00
Widget _buildCupertinoSliver() {
if (error.isNotEmpty) {
return SliverToBoxAdapter(
child: ErrorReload(text: error, onTap: _refresh),
);
2019-03-02 09:51:28 +01:00
} else if (loading && items.isEmpty) {
2019-02-03 07:42:50 +01:00
return SliverToBoxAdapter(child: Loading(more: false));
2019-02-10 05:16:52 +01:00
} else if (items.isEmpty) {
return SliverToBoxAdapter(child: EmptyWidget());
2019-02-03 07:42:50 +01:00
} else {
return SliverList(
delegate: SliverChildBuilderDelegate(
_buildItem,
childCount: 2 * items.length + 1,
2019-02-03 07:42:50 +01:00
),
);
}
}
2019-09-25 11:06:36 +02:00
Widget _buildMaterial() {
if (error.isNotEmpty) {
return ErrorReload(text: error, onTap: _refresh);
2019-03-02 09:51:28 +01:00
} else if (loading && items.isEmpty) {
2019-02-03 07:42:50 +01:00
return Loading(more: false);
2019-02-10 05:16:52 +01:00
} else if (items.isEmpty) {
return EmptyWidget();
2019-02-03 07:42:50 +01:00
} else {
2019-09-28 15:40:42 +02:00
return Scrollbar(
child: ListView.builder(
controller: _controller,
itemCount: 2 * items.length + 1,
itemBuilder: _buildItem,
),
2019-02-03 07:42:50 +01:00
);
}
}
2019-09-25 11:06:36 +02:00
Widget _buildBody() {
switch (Provider.of<ThemeModel>(context).theme) {
2019-09-19 15:10:50 +02:00
case AppThemeType.cupertino:
2019-09-28 15:40:42 +02:00
return CupertinoScrollbar(
child: CustomScrollView(
controller: _controller,
slivers: [
CupertinoSliverRefreshControl(onRefresh: _refresh),
_buildCupertinoSliver(),
],
),
2019-02-03 07:42:50 +01:00
);
default:
2019-09-25 11:06:36 +02:00
return RefreshIndicator(
onRefresh: _refresh,
child: _buildMaterial(),
2019-02-03 07:42:50 +01:00
);
}
}
2019-09-25 11:06:36 +02:00
@override
Widget build(BuildContext context) {
return CommonScaffold(
title: widget.title,
body: _buildBody(),
2019-10-03 04:01:54 +02:00
action: widget.actionBuilder == null ? null : widget.actionBuilder(),
2019-09-25 11:06:36 +02:00
);
}
2019-02-03 07:42:50 +01:00
}