2019-02-08 07:01:25 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import '../providers/settings.dart';
|
2019-02-08 07:06:48 +01:00
|
|
|
import '../widgets/loading.dart';
|
|
|
|
import '../widgets/link.dart';
|
2019-02-08 13:52:10 +01:00
|
|
|
import '../widgets/error_reload.dart';
|
2019-02-08 07:01:25 +01:00
|
|
|
|
|
|
|
class LongListPayload<T, K> {
|
|
|
|
T header;
|
|
|
|
int totalCount;
|
|
|
|
String cursor;
|
|
|
|
List<K> leadingItems;
|
|
|
|
List<K> trailingItems;
|
|
|
|
|
|
|
|
LongListPayload({
|
|
|
|
this.header,
|
|
|
|
this.totalCount,
|
|
|
|
this.cursor,
|
|
|
|
this.leadingItems,
|
|
|
|
this.trailingItems,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a scaffold for issue and pull request
|
|
|
|
// Since the list could be very long, and some users may only want to to check trailing items
|
|
|
|
// We should load leading and trailing items at first fetching, and do load more in the middle
|
|
|
|
// e.g. https://github.com/reactjs/rfcs/pull/68
|
|
|
|
class LongListScaffold<T, K> extends StatefulWidget {
|
|
|
|
final Widget title;
|
2019-02-10 11:50:40 +01:00
|
|
|
final Widget Function(T headerPayload) trailingBuilder;
|
2019-02-08 07:01:25 +01:00
|
|
|
final Widget Function(T headerPayload) headerBuilder;
|
|
|
|
final Widget Function(K itemPayload) itemBuilder;
|
|
|
|
final Future<LongListPayload<T, K>> Function() onRefresh;
|
|
|
|
final Future<LongListPayload<T, K>> Function(String cursor) onLoadMore;
|
|
|
|
|
|
|
|
LongListScaffold({
|
|
|
|
@required this.title,
|
2019-02-10 11:50:40 +01:00
|
|
|
this.trailingBuilder,
|
2019-02-08 07:01:25 +01:00
|
|
|
@required this.headerBuilder,
|
|
|
|
@required this.itemBuilder,
|
|
|
|
@required this.onRefresh,
|
|
|
|
@required this.onLoadMore,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LongListScaffoldState createState() => _LongListScaffoldState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LongListScaffoldState<T, K> extends State<LongListScaffold<T, K>> {
|
|
|
|
bool loading;
|
|
|
|
bool loadingMore = false;
|
2019-02-08 13:52:10 +01:00
|
|
|
String error = '';
|
|
|
|
|
2019-02-08 07:01:25 +01:00
|
|
|
LongListPayload<T, K> payload;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refresh() async {
|
2019-02-08 16:20:28 +01:00
|
|
|
// print('long list scaffold refresh');
|
2019-02-08 07:01:25 +01:00
|
|
|
setState(() {
|
2019-02-08 13:52:10 +01:00
|
|
|
error = '';
|
2019-02-08 07:01:25 +01:00
|
|
|
loading = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
payload = await widget.onRefresh();
|
2019-02-08 13:52:10 +01:00
|
|
|
} catch (err) {
|
2019-02-08 16:41:58 +01:00
|
|
|
error = err.toString();
|
2019-02-19 08:25:15 +01:00
|
|
|
throw err;
|
2019-02-08 07:01:25 +01:00
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _loadMore() async {
|
2019-02-08 16:20:28 +01:00
|
|
|
// print('long list scaffold load more');
|
2019-02-08 07:01:25 +01:00
|
|
|
setState(() {
|
|
|
|
loadingMore = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
var _payload = await widget.onLoadMore(payload.cursor);
|
|
|
|
payload.totalCount = _payload.totalCount;
|
|
|
|
payload.cursor = _payload.cursor;
|
|
|
|
payload.leadingItems.addAll(_payload.leadingItems);
|
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loadingMore = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildItem(BuildContext context, int index) {
|
|
|
|
if (index % 2 == 1) {
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(bottom: BorderSide(color: Colors.black12)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
int realIndex = index ~/ 2;
|
|
|
|
|
|
|
|
if (realIndex < payload.leadingItems.length) {
|
|
|
|
return widget.itemBuilder(payload.leadingItems[realIndex]);
|
|
|
|
} else if (realIndex == payload.leadingItems.length) {
|
|
|
|
var count = payload.totalCount -
|
|
|
|
payload.leadingItems.length +
|
|
|
|
payload.trailingItems.length;
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.all(16),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
image: DecorationImage(
|
|
|
|
image: ExactAssetImage('images/progressive-disclosure-line.png',
|
|
|
|
scale: 2),
|
|
|
|
repeat: ImageRepeat.repeatX,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: Center(
|
|
|
|
child: Link(
|
2019-02-21 11:43:41 +01:00
|
|
|
onTap: _loadMore,
|
2019-02-08 07:01:25 +01:00
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.all(10),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border.all(color: Colors.black12),
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Text('$count hidden items',
|
|
|
|
style: TextStyle(color: Colors.black87, fontSize: 15)),
|
|
|
|
Padding(padding: EdgeInsets.only(top: 4)),
|
|
|
|
loadingMore
|
|
|
|
? CupertinoActivityIndicator()
|
|
|
|
: Text(
|
|
|
|
'Load more...',
|
|
|
|
style:
|
|
|
|
TextStyle(color: Colors.blueAccent, fontSize: 16),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return widget.itemBuilder(
|
|
|
|
payload.trailingItems[realIndex - payload.leadingItems.length - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get _itemCount {
|
|
|
|
int count = payload.leadingItems.length + payload.trailingItems.length;
|
|
|
|
if (payload.totalCount > count) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return 2 * count; // including bottom border
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSliver() {
|
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-08 13:52:10 +01:00
|
|
|
} else if (loading) {
|
2019-02-08 07:01:25 +01:00
|
|
|
return SliverToBoxAdapter(child: Loading(more: false));
|
|
|
|
} else {
|
|
|
|
return SliverList(
|
|
|
|
delegate:
|
|
|
|
SliverChildBuilderDelegate(_buildItem, childCount: _itemCount),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
switch (SettingsProvider.of(context).theme) {
|
|
|
|
case ThemeMap.cupertino:
|
|
|
|
List<Widget> slivers = [
|
|
|
|
CupertinoSliverRefreshControl(onRefresh: widget.onRefresh)
|
|
|
|
];
|
|
|
|
if (payload != null) {
|
|
|
|
slivers.add(
|
|
|
|
SliverToBoxAdapter(child: widget.headerBuilder(payload.header)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
slivers.add(_buildSliver());
|
|
|
|
|
|
|
|
return CupertinoPageScaffold(
|
|
|
|
navigationBar: CupertinoNavigationBar(
|
|
|
|
middle: widget.title,
|
2019-02-10 11:50:40 +01:00
|
|
|
trailing:
|
|
|
|
payload == null ? null : widget.trailingBuilder(payload.header),
|
2019-02-08 07:01:25 +01:00
|
|
|
),
|
|
|
|
child: SafeArea(
|
|
|
|
child: CustomScrollView(slivers: slivers),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
default:
|
2019-02-10 12:15:50 +01:00
|
|
|
List<Widget> slivers = [];
|
2019-02-10 11:50:40 +01:00
|
|
|
if (payload != null) {
|
2019-02-10 12:15:50 +01:00
|
|
|
slivers.add(
|
|
|
|
SliverToBoxAdapter(child: widget.headerBuilder(payload.header)),
|
|
|
|
);
|
2019-02-10 11:50:40 +01:00
|
|
|
}
|
2019-02-10 12:15:50 +01:00
|
|
|
slivers.add(_buildSliver());
|
|
|
|
|
2019-02-08 07:01:25 +01:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: widget.title,
|
2019-02-20 09:31:22 +01:00
|
|
|
actions: payload == null
|
|
|
|
? null
|
|
|
|
: [widget.trailingBuilder(payload.header)],
|
2019-02-08 07:01:25 +01:00
|
|
|
),
|
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: widget.onRefresh,
|
2019-02-10 12:15:50 +01:00
|
|
|
child: CustomScrollView(slivers: slivers),
|
2019-02-08 07:01:25 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|