2019-02-03 07:42:50 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import '../providers/settings.dart';
|
2019-02-08 13:52:10 +01:00
|
|
|
import '../widgets/error_reload.dart';
|
2019-02-08 07:06:48 +01:00
|
|
|
import '../widgets/loading.dart';
|
2019-02-10 05:16:52 +01:00
|
|
|
import '../widgets/empty.dart';
|
2019-02-03 07:42:50 +01:00
|
|
|
|
2019-02-09 06:29:44 +01:00
|
|
|
class ListPayload<T, K> {
|
|
|
|
K cursor;
|
|
|
|
List<T> items;
|
2019-02-09 07:20:21 +01:00
|
|
|
bool hasMore;
|
2019-02-09 06:29:44 +01:00
|
|
|
|
2019-02-09 07:20:21 +01:00
|
|
|
ListPayload({this.items, this.cursor, this.hasMore});
|
2019-02-09 06:29:44 +01:00
|
|
|
}
|
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-02-09 06:29:44 +01:00
|
|
|
class ListScaffold<T, K> extends StatefulWidget {
|
2019-02-05 13:57:05 +01:00
|
|
|
final Widget title;
|
2019-03-10 09:09:26 +01:00
|
|
|
final Widget Function({Function({bool force}) refresh}) trailingBuiler;
|
2019-02-09 06:29:44 +01:00
|
|
|
final Widget Function(T payload) itemBuilder;
|
|
|
|
final Future<ListPayload<T, K>> Function() onRefresh;
|
|
|
|
final Future<ListPayload<T, K>> Function(K cursor) onLoadMore;
|
2019-02-03 07:42:50 +01:00
|
|
|
|
|
|
|
ListScaffold({
|
|
|
|
@required this.title,
|
|
|
|
@required this.itemBuilder,
|
|
|
|
@required this.onRefresh,
|
2019-02-09 06:29:44 +01:00
|
|
|
@required this.onLoadMore,
|
2019-03-10 09:09:26 +01:00
|
|
|
this.trailingBuiler,
|
2019-02-03 07:42:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
2019-03-10 09:09:26 +01:00
|
|
|
_ListScaffoldState<T, K> createState() => _ListScaffoldState();
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 06:29:44 +01:00
|
|
|
class _ListScaffoldState<T, K> extends State<ListScaffold<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 = [];
|
|
|
|
K cursor;
|
2019-02-09 07:20:21 +01:00
|
|
|
bool hasMore;
|
2019-02-09 06:29:44 +01:00
|
|
|
|
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() {
|
|
|
|
// print(_controller.position.maxScrollExtent - _controller.offset);
|
|
|
|
if (_controller.position.maxScrollExtent - _controller.offset < 100 &&
|
|
|
|
!_controller.position.outOfRange &&
|
|
|
|
!loading &&
|
|
|
|
!loadingMore &&
|
|
|
|
hasMore) {
|
|
|
|
_loadMore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: if items not enough, fetch next page
|
|
|
|
// This should be triggered after build
|
|
|
|
void _makeSureItemsFill() {
|
|
|
|
Future.delayed(Duration(milliseconds: 300)).then((_) {
|
|
|
|
onScroll();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-10 09:09:26 +01:00
|
|
|
Future<void> _refresh({bool force = false}) async {
|
2019-03-10 10:18:38 +01:00
|
|
|
// print('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;
|
2019-03-10 09:09:26 +01:00
|
|
|
if (force) {
|
|
|
|
items = [];
|
|
|
|
}
|
2019-02-03 07:42:50 +01:00
|
|
|
});
|
|
|
|
try {
|
2019-02-09 06:29:44 +01:00
|
|
|
var _payload = await widget.onRefresh();
|
|
|
|
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) {
|
2019-02-08 13:52:10 +01:00
|
|
|
error = err.toString();
|
2019-02-19 08:25:15 +01:00
|
|
|
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 {
|
2019-02-10 12:15:50 +01:00
|
|
|
// print('list scaffold load more');
|
2019-02-03 07:42:50 +01:00
|
|
|
setState(() {
|
|
|
|
loadingMore = true;
|
|
|
|
});
|
|
|
|
try {
|
2019-02-09 06:29:44 +01:00
|
|
|
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) {
|
2019-02-09 06:29:44 +01:00
|
|
|
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) {
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(bottom: BorderSide(color: Colors.black12)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-09 06:29:44 +01:00
|
|
|
return widget.itemBuilder(items[index ~/ 2]);
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSliver(BuildContext context) {
|
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) {
|
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,
|
2019-02-09 06:29:44 +01:00
|
|
|
childCount: 2 * items.length + 1,
|
2019-02-03 07:42:50 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBody(BuildContext context) {
|
2019-02-08 13:52:10 +01:00
|
|
|
if (error.isNotEmpty) {
|
2019-02-21 11:43:41 +01:00
|
|
|
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 {
|
|
|
|
return ListView.builder(
|
2019-03-10 09:09:26 +01:00
|
|
|
// shrinkWrap: true,
|
2019-02-03 07:42:50 +01:00
|
|
|
controller: _controller,
|
2019-02-09 06:29:44 +01:00
|
|
|
itemCount: 2 * items.length + 1,
|
2019-02-03 07:42:50 +01:00
|
|
|
itemBuilder: _buildItem,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2019-02-07 07:35:19 +01:00
|
|
|
switch (SettingsProvider.of(context).theme) {
|
|
|
|
case ThemeMap.cupertino:
|
2019-02-03 07:42:50 +01:00
|
|
|
List<Widget> slivers = [
|
2019-03-02 09:51:28 +01:00
|
|
|
CupertinoSliverRefreshControl(onRefresh: _refresh)
|
2019-02-03 07:42:50 +01:00
|
|
|
];
|
2019-02-09 06:29:44 +01:00
|
|
|
// if (widget.header != null) {
|
|
|
|
// slivers.add(SliverToBoxAdapter(child: widget.header));
|
|
|
|
// }
|
2019-02-03 07:42:50 +01:00
|
|
|
slivers.add(_buildSliver(context));
|
|
|
|
|
|
|
|
return CupertinoPageScaffold(
|
2019-02-06 06:06:11 +01:00
|
|
|
navigationBar: CupertinoNavigationBar(
|
|
|
|
middle: widget.title,
|
2019-03-10 09:09:26 +01:00
|
|
|
trailing: widget.trailingBuiler == null
|
|
|
|
? null
|
|
|
|
: widget.trailingBuiler(refresh: _refresh),
|
2019-02-06 06:06:11 +01:00
|
|
|
),
|
2019-02-03 07:42:50 +01:00
|
|
|
child: SafeArea(
|
|
|
|
child: CustomScrollView(
|
|
|
|
controller: _controller,
|
|
|
|
slivers: slivers,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return Scaffold(
|
2019-02-06 06:06:11 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: widget.title,
|
2019-03-10 09:09:26 +01:00
|
|
|
actions: widget.trailingBuiler == null
|
|
|
|
? null
|
|
|
|
: [widget.trailingBuiler(refresh: _refresh)],
|
2019-02-06 06:06:11 +01:00
|
|
|
),
|
2019-02-03 07:42:50 +01:00
|
|
|
body: RefreshIndicator(
|
2019-03-02 09:51:28 +01:00
|
|
|
onRefresh: _refresh,
|
2019-02-03 07:42:50 +01:00
|
|
|
child: _buildBody(context),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|