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';
|
|
|
|
import 'loading.dart';
|
|
|
|
|
|
|
|
typedef RefreshCallback = Future<void> Function();
|
2019-02-03 16:10:10 +01:00
|
|
|
typedef BodyBuilder = Widget Function();
|
2019-02-03 08:50:17 +01:00
|
|
|
|
|
|
|
class RefreshScaffold extends StatefulWidget {
|
2019-02-03 16:10:10 +01:00
|
|
|
final Widget title;
|
|
|
|
final BodyBuilder bodyBuilder;
|
2019-02-03 08:50:17 +01:00
|
|
|
final RefreshCallback onRefresh;
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_RefreshScaffoldState createState() => _RefreshScaffoldState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RefreshScaffoldState extends State<RefreshScaffold> {
|
|
|
|
bool loading = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refresh() async {
|
|
|
|
print('refresh');
|
|
|
|
setState(() {
|
|
|
|
loading = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await widget.onRefresh();
|
|
|
|
} catch (err) {
|
|
|
|
print(err);
|
|
|
|
} finally {
|
|
|
|
setState(() {
|
|
|
|
loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBody(BuildContext context) {
|
|
|
|
if (loading) {
|
|
|
|
return Loading(more: false);
|
|
|
|
} else {
|
2019-02-03 16:10:10 +01:00
|
|
|
return widget.bodyBuilder();
|
2019-02-03 08:50:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
switch (SettingsProvider.of(context).layout) {
|
|
|
|
case LayoutMap.cupertino:
|
|
|
|
return CupertinoPageScaffold(
|
2019-02-03 16:10:10 +01:00
|
|
|
navigationBar: CupertinoNavigationBar(middle: widget.title),
|
2019-02-03 08:50:17 +01:00
|
|
|
child: SafeArea(
|
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: <Widget>[
|
|
|
|
CupertinoSliverRefreshControl(onRefresh: widget.onRefresh),
|
|
|
|
SliverToBoxAdapter(child: _buildBody(context))
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return Scaffold(
|
2019-02-03 16:10:10 +01:00
|
|
|
appBar: AppBar(title: widget.title),
|
2019-02-03 08:50:17 +01:00
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: widget.onRefresh,
|
|
|
|
child: _buildBody(context),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|