2019-02-03 08:50:17 +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';
|
2019-02-08 13:52:10 +01:00
|
|
|
import '../widgets/error_reload.dart';
|
2019-02-03 08:50:17 +01:00
|
|
|
|
2019-02-08 12:34:07 +01:00
|
|
|
// This is a scaffold for pull to refresh
|
2019-02-08 13:52:10 +01:00
|
|
|
class RefreshScaffold<T> extends StatefulWidget {
|
2019-02-03 16:10:10 +01:00
|
|
|
final Widget title;
|
2019-02-08 13:52:10 +01:00
|
|
|
final Widget Function(T payload) bodyBuilder;
|
|
|
|
final Future<T> Function() onRefresh;
|
2019-02-10 11:50:40 +01:00
|
|
|
final Widget Function(T payload) trailingBuilder;
|
2019-02-20 09:31:22 +01:00
|
|
|
// final List<Widget> Function(T payload) actionsBuilder;
|
2019-02-07 13:28:48 +01:00
|
|
|
final PreferredSizeWidget bottom;
|
2019-02-03 08:50:17 +01:00
|
|
|
|
|
|
|
RefreshScaffold({
|
|
|
|
@required this.title,
|
2019-02-03 16:10:10 +01:00
|
|
|
@required this.bodyBuilder,
|
2019-02-03 08:50:17 +01:00
|
|
|
@required this.onRefresh,
|
2019-02-10 11:50:40 +01:00
|
|
|
this.trailingBuilder,
|
2019-02-20 09:31:22 +01:00
|
|
|
// this.actionsBuilder,
|
2019-02-07 13:28:48 +01:00
|
|
|
this.bottom,
|
2019-02-03 08:50:17 +01:00
|
|
|
});
|
|
|
|
|
2019-02-08 13:52:10 +01:00
|
|
|
@override
|
|
|
|
_RefreshScaffoldState createState() => _RefreshScaffoldState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RefreshScaffoldState<T> extends State<RefreshScaffold<T>> {
|
|
|
|
bool loading;
|
|
|
|
T payload;
|
|
|
|
String error = '';
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_refresh();
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:35:52 +01:00
|
|
|
Widget _buildBody() {
|
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-02-09 06:29:44 +01:00
|
|
|
} else if (payload == null) {
|
|
|
|
return Loading(more: false);
|
2019-02-03 08:50:17 +01:00
|
|
|
} else {
|
2019-02-08 13:52:10 +01:00
|
|
|
return widget.bodyBuilder(payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refresh() async {
|
|
|
|
try {
|
|
|
|
setState(() {
|
|
|
|
error = '';
|
|
|
|
loading = true;
|
|
|
|
});
|
|
|
|
payload = await widget.onRefresh();
|
|
|
|
} catch (err) {
|
|
|
|
error = err.toString();
|
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
2019-02-03 08:50:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:31:22 +01:00
|
|
|
Widget _buildTrailing() {
|
2019-03-02 10:21:50 +01:00
|
|
|
if (payload == null || widget.trailingBuilder == null) return null;
|
2019-02-20 09:31:22 +01:00
|
|
|
|
|
|
|
return widget.trailingBuilder(payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Widget> _buildActions() {
|
2019-03-02 10:21:50 +01:00
|
|
|
if (payload == null || widget.trailingBuilder == null) return null;
|
2019-02-20 09:31:22 +01:00
|
|
|
|
|
|
|
return [widget.trailingBuilder(payload)];
|
|
|
|
}
|
|
|
|
|
2019-02-03 08:50:17 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2019-02-07 07:35:19 +01:00
|
|
|
switch (SettingsProvider.of(context).theme) {
|
|
|
|
case ThemeMap.cupertino:
|
2019-02-03 08:50:17 +01:00
|
|
|
return CupertinoPageScaffold(
|
2019-02-08 13:52:10 +01:00
|
|
|
navigationBar: CupertinoNavigationBar(
|
2019-02-10 11:50:40 +01:00
|
|
|
middle: widget.title,
|
2019-02-20 09:31:22 +01:00
|
|
|
trailing: _buildTrailing(),
|
2019-02-10 11:50:40 +01:00
|
|
|
),
|
2019-02-03 08:50:17 +01:00
|
|
|
child: SafeArea(
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: <Widget>[
|
2019-02-08 13:52:10 +01:00
|
|
|
CupertinoSliverRefreshControl(onRefresh: _refresh),
|
2019-02-06 14:35:52 +01:00
|
|
|
SliverToBoxAdapter(child: _buildBody())
|
2019-02-03 08:50:17 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
default:
|
2019-02-08 13:52:10 +01:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: widget.title,
|
2019-02-20 09:31:22 +01:00
|
|
|
actions: _buildActions(),
|
2019-02-08 13:52:10 +01:00
|
|
|
bottom: widget.bottom,
|
|
|
|
),
|
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: _refresh,
|
|
|
|
child: SingleChildScrollView(child: _buildBody()),
|
2019-02-03 08:50:17 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|