2022-10-02 10:57:34 +02:00
|
|
|
import 'package:antd_mobile/antd_mobile.dart';
|
2019-02-03 07:42:50 +01:00
|
|
|
import 'package:flutter/cupertino.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';
|
2022-09-17 14:35:45 +02:00
|
|
|
import 'package:git_touch/widgets/empty.dart';
|
2021-06-13 19:23:16 +02:00
|
|
|
import 'package:git_touch/widgets/error_reload.dart';
|
|
|
|
import 'package:git_touch/widgets/loading.dart';
|
2022-09-17 14:35:45 +02:00
|
|
|
|
2021-06-13 20:13:11 +02:00
|
|
|
export 'package:git_touch/utils/utils.dart';
|
2019-02-03 07:42:50 +01:00
|
|
|
|
2019-02-08 07:06:48 +01:00
|
|
|
// This is a scaffold for infinite scroll screens
|
2019-09-25 11:06:36 +02:00
|
|
|
class ListStatefulScaffold<T, K> extends StatefulWidget {
|
2022-09-06 18:28:12 +02:00
|
|
|
const ListStatefulScaffold({
|
2021-05-16 09:16:35 +02:00
|
|
|
required this.title,
|
|
|
|
required this.fetch,
|
2021-06-14 05:28:12 +02:00
|
|
|
required this.itemBuilder,
|
2019-10-03 04:01:54 +02:00
|
|
|
this.actionBuilder,
|
2019-10-02 08:58:11 +02:00
|
|
|
});
|
2022-09-21 18:28:21 +02:00
|
|
|
final Widget title;
|
|
|
|
final Widget Function()? actionBuilder;
|
|
|
|
final Widget Function(T payload) itemBuilder;
|
|
|
|
final Future<ListPayload<T, K>> Function(K? cursor) fetch;
|
2019-02-03 07:42:50 +01:00
|
|
|
|
|
|
|
@override
|
2022-10-03 19:05:29 +02:00
|
|
|
State<ListStatefulScaffold<T, K>> createState() =>
|
2019-09-25 11:06:36 +02:00
|
|
|
_ListStatefulScaffoldState();
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:06:36 +02:00
|
|
|
class _ListStatefulScaffoldState<T, K>
|
2021-05-30 17:55:57 +02:00
|
|
|
extends State<ListStatefulScaffold<T, K>> {
|
2019-02-03 07:42:50 +01:00
|
|
|
bool loading = false;
|
|
|
|
bool loadingMore = false;
|
2019-02-08 13:52:10 +01:00
|
|
|
String error = '';
|
|
|
|
|
2019-02-09 06:29:44 +01:00
|
|
|
List<T> items = [];
|
2021-05-16 09:16:35 +02:00
|
|
|
K? cursor;
|
|
|
|
bool? hasMore;
|
2019-02-09 06:29:44 +01:00
|
|
|
|
2022-09-06 18:28:12 +02:00
|
|
|
final ScrollController _controller = ScrollController();
|
2019-02-03 07:42:50 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_refresh();
|
2021-06-13 19:23:16 +02:00
|
|
|
_controller.addListener(() {
|
|
|
|
if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
|
|
|
|
!_controller.position.outOfRange &&
|
|
|
|
!loading &&
|
|
|
|
!loadingMore &&
|
|
|
|
hasMore != false) {
|
|
|
|
_loadMore();
|
|
|
|
}
|
|
|
|
});
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 09:09:26 +01:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_controller.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2021-06-13 19:23:16 +02:00
|
|
|
Future<void> _refresh() async {
|
2019-10-13 05:01:29 +02:00
|
|
|
// Fimber.d('list scaffold refresh');
|
2019-02-03 07:42:50 +01:00
|
|
|
setState(() {
|
2019-02-08 13:52:10 +01:00
|
|
|
error = '';
|
2019-02-03 07:42:50 +01:00
|
|
|
loading = true;
|
|
|
|
});
|
|
|
|
try {
|
2022-09-24 07:41:46 +02:00
|
|
|
final p = await widget.fetch(null);
|
2021-06-13 19:23:16 +02:00
|
|
|
items = p.items.toList();
|
|
|
|
cursor = p.cursor;
|
|
|
|
hasMore = p.hasMore;
|
2019-02-03 07:42:50 +01:00
|
|
|
} catch (err) {
|
2019-02-08 13:52:10 +01:00
|
|
|
error = err.toString();
|
2022-09-06 18:28:12 +02:00
|
|
|
rethrow;
|
2019-02-03 07:42:50 +01:00
|
|
|
} finally {
|
2019-02-06 14:35:52 +01:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _loadMore() async {
|
2019-10-13 05:01:29 +02:00
|
|
|
// Fimber.d('list scaffold load more');
|
2019-02-03 07:42:50 +01:00
|
|
|
setState(() {
|
|
|
|
loadingMore = true;
|
|
|
|
});
|
|
|
|
try {
|
2022-09-24 07:41:46 +02:00
|
|
|
final p = await widget.fetch(cursor);
|
2021-06-13 19:23:16 +02:00
|
|
|
items.addAll(p.items);
|
|
|
|
cursor = p.cursor;
|
|
|
|
hasMore = p.hasMore;
|
2019-02-03 07:42:50 +01:00
|
|
|
} catch (err) {
|
2019-03-10 09:09:26 +01:00
|
|
|
error = err.toString();
|
2022-09-06 18:28:12 +02:00
|
|
|
rethrow;
|
2019-02-03 07:42:50 +01:00
|
|
|
} finally {
|
2019-02-08 12:44:10 +01:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loadingMore = false;
|
|
|
|
});
|
|
|
|
}
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:06:36 +02:00
|
|
|
Widget _buildCupertinoSliver() {
|
2019-02-08 13:52:10 +01:00
|
|
|
if (error.isNotEmpty) {
|
2019-02-09 06:29:44 +01:00
|
|
|
return SliverToBoxAdapter(
|
2019-02-21 11:43:41 +01:00
|
|
|
child: ErrorReload(text: error, onTap: _refresh),
|
2019-02-09 06:29:44 +01:00
|
|
|
);
|
2019-03-02 09:51:28 +01:00
|
|
|
} else if (loading && items.isEmpty) {
|
2022-09-06 18:28:12 +02:00
|
|
|
return const 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 {
|
2022-10-02 10:57:34 +02:00
|
|
|
return AntSliverList(
|
|
|
|
count: items.length + 1,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
if (index == items.length) {
|
|
|
|
if (hasMore != false) {
|
|
|
|
return const Loading(more: true);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return widget.itemBuilder(items[index]);
|
|
|
|
},
|
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,
|
2022-09-17 14:35:45 +02:00
|
|
|
body: CupertinoScrollbar(
|
|
|
|
child: CustomScrollView(
|
|
|
|
controller: _controller,
|
|
|
|
slivers: [
|
|
|
|
CupertinoSliverRefreshControl(onRefresh: _refresh),
|
|
|
|
_buildCupertinoSliver(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2021-06-13 18:59:06 +02:00
|
|
|
action: widget.actionBuilder?.call(),
|
2019-09-25 11:06:36 +02:00
|
|
|
);
|
|
|
|
}
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|