1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-20 04:05:26 +01:00
git-touch-android-ios-app/lib/scaffolds/refresh_stateful.dart

76 lines
1.7 KiB
Dart
Raw Normal View History

2019-09-25 11:06:36 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:git_touch/scaffolds/utils.dart';
class RefreshStatefulScaffold<T> extends StatefulWidget {
final Widget title;
final Widget Function(T payload) bodyBuilder;
final Future<T> Function() onRefresh;
final Widget Function(T payload) trailingBuilder;
RefreshStatefulScaffold({
@required this.title,
@required this.bodyBuilder,
@required this.onRefresh,
this.trailingBuilder,
});
@override
_RefreshStatefulScaffoldState createState() =>
_RefreshStatefulScaffoldState();
}
class _RefreshStatefulScaffoldState<T>
extends State<RefreshStatefulScaffold<T>> {
bool _loading;
T _payload;
String _error = '';
@override
void initState() {
super.initState();
_refresh();
}
Future<void> _refresh() async {
try {
setState(() {
_error = '';
_loading = true;
});
_payload = await widget.onRefresh();
} catch (err) {
_error = err.toString();
throw err;
} finally {
if (mounted) {
setState(() {
_loading = false;
});
}
}
}
Widget get _trailing {
2019-09-25 15:41:44 +02:00
if (widget.trailingBuilder == null) return null;
2019-09-25 11:06:36 +02:00
return widget.trailingBuilder(_payload);
}
@override
Widget build(BuildContext context) {
return CommonScaffold(
title: widget.title,
2019-09-25 12:47:34 +02:00
body: RefreshWrapper(
onRefresh: _refresh,
body: ErrorLoadingWrapper(
bodyBuilder: () => widget.bodyBuilder(_payload),
error: _error,
loading: _payload == null,
reload: _refresh,
),
),
2019-09-25 11:06:36 +02:00
trailing: _trailing,
);
}
}