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-06 06:06:11 +01:00
|
|
|
import '../widgets/link.dart';
|
2019-02-08 07:06:48 +01:00
|
|
|
import '../widgets/loading.dart';
|
2019-02-03 07:42:50 +01:00
|
|
|
|
|
|
|
typedef RefreshCallback = Future<void> Function();
|
|
|
|
|
2019-02-08 07:06:48 +01:00
|
|
|
// This is a scaffold for infinite scroll screens
|
2019-02-03 07:42:50 +01:00
|
|
|
class ListScaffold extends StatefulWidget {
|
2019-02-05 13:57:05 +01:00
|
|
|
final Widget title;
|
2019-02-06 06:06:11 +01:00
|
|
|
final IconData trailingIconData;
|
|
|
|
final Function trailingOnTap;
|
2019-02-03 07:42:50 +01:00
|
|
|
final Widget header;
|
|
|
|
final int itemCount;
|
|
|
|
final IndexedWidgetBuilder itemBuilder;
|
|
|
|
final RefreshCallback onRefresh;
|
|
|
|
final RefreshCallback onLoadMore;
|
|
|
|
|
|
|
|
ListScaffold({
|
|
|
|
@required this.title,
|
2019-02-06 06:06:11 +01:00
|
|
|
this.trailingIconData,
|
|
|
|
this.trailingOnTap,
|
2019-02-03 07:42:50 +01:00
|
|
|
this.header,
|
|
|
|
@required this.itemCount,
|
|
|
|
@required this.itemBuilder,
|
|
|
|
@required this.onRefresh,
|
|
|
|
this.onLoadMore,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_ListScaffoldState createState() => _ListScaffoldState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ListScaffoldState extends State<ListScaffold> {
|
|
|
|
bool loading = false;
|
|
|
|
bool loadingMore = false;
|
|
|
|
ScrollController _controller = ScrollController();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_refresh();
|
|
|
|
_controller.addListener(() {
|
|
|
|
if (_controller.offset + 100 > _controller.position.maxScrollExtent &&
|
|
|
|
!_controller.position.outOfRange &&
|
|
|
|
!loading &&
|
|
|
|
!loadingMore) {
|
|
|
|
_loadMore();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refresh() async {
|
2019-02-06 06:06:11 +01:00
|
|
|
print('list scaffold refresh');
|
2019-02-03 07:42:50 +01:00
|
|
|
setState(() {
|
|
|
|
loading = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await widget.onRefresh();
|
|
|
|
} catch (err) {
|
|
|
|
print(err);
|
|
|
|
} 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-02-06 06:06:11 +01:00
|
|
|
print('list scaffold load more');
|
2019-02-03 07:42:50 +01:00
|
|
|
setState(() {
|
|
|
|
loadingMore = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await widget.onLoadMore();
|
|
|
|
} catch (err) {
|
|
|
|
print(err);
|
|
|
|
} finally {
|
2019-02-08 12:44:10 +01:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loadingMore = false;
|
|
|
|
});
|
|
|
|
}
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildItem(BuildContext context, int index) {
|
2019-02-05 13:57:05 +01:00
|
|
|
if (index == 2 * widget.itemCount) {
|
2019-02-03 07:42:50 +01:00
|
|
|
return Loading(more: true);
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:57:05 +01:00
|
|
|
if (index % 2 == 1) {
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(bottom: BorderSide(color: Colors.black12)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return widget.itemBuilder(context, index ~/ 2);
|
2019-02-03 07:42:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSliver(BuildContext context) {
|
|
|
|
if (loading) {
|
|
|
|
return SliverToBoxAdapter(child: Loading(more: false));
|
|
|
|
} else {
|
|
|
|
return SliverList(
|
|
|
|
delegate: SliverChildBuilderDelegate(
|
|
|
|
_buildItem,
|
2019-02-05 13:57:05 +01:00
|
|
|
childCount: 2 * widget.itemCount + 1,
|
2019-02-03 07:42:50 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBody(BuildContext context) {
|
|
|
|
if (loading) {
|
|
|
|
return Loading(more: false);
|
|
|
|
} else {
|
|
|
|
return ListView.builder(
|
|
|
|
controller: _controller,
|
2019-02-05 13:57:05 +01:00
|
|
|
itemCount: 2 * widget.itemCount + 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 = [
|
|
|
|
CupertinoSliverRefreshControl(onRefresh: widget.onRefresh)
|
|
|
|
];
|
|
|
|
if (widget.header != null) {
|
|
|
|
slivers.add(SliverToBoxAdapter(child: widget.header));
|
|
|
|
}
|
|
|
|
slivers.add(_buildSliver(context));
|
|
|
|
|
|
|
|
return CupertinoPageScaffold(
|
2019-02-06 06:06:11 +01:00
|
|
|
navigationBar: CupertinoNavigationBar(
|
|
|
|
middle: widget.title,
|
2019-02-07 12:17:29 +01:00
|
|
|
// trailing: Link(
|
|
|
|
// child: Icon(
|
|
|
|
// widget.trailingIconData,
|
|
|
|
// size: 24,
|
|
|
|
// color: Colors.blueAccent,
|
|
|
|
// ),
|
|
|
|
// beforeRedirect: widget.trailingOnTap,
|
|
|
|
// bgColor: Colors.transparent,
|
|
|
|
// ),
|
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,
|
|
|
|
actions: widget.trailingIconData == null
|
|
|
|
? []
|
|
|
|
: <Widget>[
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(widget.trailingIconData),
|
|
|
|
onPressed: widget.trailingOnTap,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2019-02-03 07:42:50 +01:00
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: widget.onRefresh,
|
|
|
|
child: _buildBody(context),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|