1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-19 19:54:54 +01:00
git-touch-android-ios-app/lib/scaffolds/refresh.dart

112 lines
2.8 KiB
Dart
Raw Normal View History

2019-02-03 08:50:17 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:git_touch/models/theme.dart';
import 'package:provider/provider.dart';
import '../widgets/loading.dart';
import '../widgets/error_reload.dart';
2019-02-03 08:50:17 +01:00
// This is a scaffold for pull to refresh
class RefreshScaffold<T> extends StatefulWidget {
final Widget title;
final Widget Function(T payload) bodyBuilder;
final Future<T> Function() onRefresh;
final Widget Function(T payload) trailingBuilder;
2019-02-20 09:31:22 +01:00
// final List<Widget> Function(T payload) actionsBuilder;
2019-02-03 08:50:17 +01:00
RefreshScaffold({
@required this.title,
@required this.bodyBuilder,
2019-02-03 08:50:17 +01:00
@required this.onRefresh,
this.trailingBuilder,
2019-02-20 09:31:22 +01:00
// this.actionsBuilder,
2019-02-03 08:50:17 +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() {
if (error.isNotEmpty) {
return ErrorReload(text: error, onTap: _refresh);
} else if (payload == null) {
return Loading(more: false);
2019-02-03 08:50:17 +01:00
} else {
return widget.bodyBuilder(payload);
}
}
Future<void> _refresh() async {
try {
setState(() {
error = '';
loading = true;
});
payload = await widget.onRefresh();
} catch (err) {
error = err.toString();
2019-03-12 12:00:31 +01:00
throw err;
} finally {
if (mounted) {
setState(() {
loading = false;
});
}
2019-02-03 08:50:17 +01:00
}
}
2019-02-20 09:31:22 +01:00
Widget _buildTrailing() {
if (payload == null || widget.trailingBuilder == null) return null;
2019-02-20 09:31:22 +01:00
return widget.trailingBuilder(payload);
}
List<Widget> _buildActions() {
if (payload == null || widget.trailingBuilder == null) return null;
2019-09-19 13:58:07 +02:00
var w = widget.trailingBuilder(payload);
return [if (w != null) w];
2019-02-20 09:31:22 +01:00
}
2019-02-03 08:50:17 +01:00
@override
Widget build(BuildContext context) {
switch (Provider.of<ThemeModel>(context).theme) {
2019-09-15 11:36:09 +02:00
case AppThemeMap.cupertino:
2019-02-03 08:50:17 +01:00
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: widget.title,
2019-02-20 09:31:22 +01:00
trailing: _buildTrailing(),
),
2019-02-03 08:50:17 +01:00
child: SafeArea(
child: CustomScrollView(
slivers: <Widget>[
CupertinoSliverRefreshControl(onRefresh: _refresh),
2019-02-06 14:35:52 +01:00
SliverToBoxAdapter(child: _buildBody())
2019-02-03 08:50:17 +01:00
],
),
),
);
default:
return Scaffold(
appBar: AppBar(
title: widget.title,
2019-02-20 09:31:22 +01:00
actions: _buildActions(),
),
body: RefreshIndicator(
onRefresh: _refresh,
child: SingleChildScrollView(child: _buildBody()),
2019-02-03 08:50:17 +01:00
),
);
}
}
}