git-touch-android-ios-app/lib/scaffolds/refresh_stateful.dart

96 lines
2.2 KiB
Dart
Raw Normal View History

2019-09-25 11:06:36 +02:00
import 'package:flutter/cupertino.dart';
2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
2019-10-02 08:58:11 +02:00
import 'package:git_touch/scaffolds/common.dart';
2019-09-25 11:06:36 +02:00
import 'package:git_touch/scaffolds/utils.dart';
class RefreshStatefulScaffold<T> extends StatefulWidget {
2022-09-06 18:28:12 +02:00
const RefreshStatefulScaffold({
2021-05-16 09:16:35 +02:00
required this.title,
required this.bodyBuilder,
required this.fetch,
2019-09-30 11:13:12 +02:00
this.actionBuilder,
2020-01-27 07:43:10 +01:00
this.action,
2019-12-07 09:52:54 +01:00
this.canRefresh = true,
2020-01-27 07:43:10 +01:00
}) : assert(actionBuilder == null || action == null);
2022-09-21 18:28:21 +02:00
final Widget title;
final Widget? Function(T data, void Function(T newData) setData) bodyBuilder;
final Future<T> Function() fetch;
final Widget? Function(T data, void Function(T newData) setData)?
actionBuilder;
final Widget? action;
2022-10-03 19:05:29 +02:00
final bool canRefresh;
2019-09-25 11:06:36 +02:00
@override
2022-10-03 19:05:29 +02:00
State<RefreshStatefulScaffold<T>> createState() =>
2019-09-25 11:06:36 +02:00
_RefreshStatefulScaffoldState();
}
class _RefreshStatefulScaffoldState<T>
2021-05-30 17:55:57 +02:00
extends State<RefreshStatefulScaffold<T>> {
2021-01-30 08:42:02 +01:00
// bool _loading;
2021-05-16 09:16:35 +02:00
T? _data;
2019-09-25 11:06:36 +02:00
String _error = '';
@override
void initState() {
super.initState();
_refresh();
2019-11-02 13:54:23 +01:00
setState(() {});
2019-09-25 11:06:36 +02:00
}
Future<void> _refresh() async {
try {
setState(() {
_error = '';
2021-01-30 08:42:02 +01:00
// _loading = true;
2019-09-25 11:06:36 +02:00
});
_data = await widget.fetch();
2020-01-28 13:59:32 +01:00
} catch (err) {
_error = err.toString();
2022-09-06 18:28:12 +02:00
rethrow;
2019-09-25 11:06:36 +02:00
} finally {
if (mounted) {
setState(() {
2021-01-30 08:42:02 +01:00
// _loading = false;
2019-09-25 11:06:36 +02:00
});
}
}
}
2021-05-16 09:16:35 +02:00
Widget? get _action {
2020-01-27 07:43:10 +01:00
if (widget.action != null) return widget.action;
if (widget.actionBuilder == null || _data == null) return null;
2022-09-06 18:28:12 +02:00
return widget.actionBuilder!(_data as T, (v) {
2021-01-31 08:49:28 +01:00
setState(() {
_data = v;
});
});
2019-09-25 11:06:36 +02:00
}
@override
Widget build(BuildContext context) {
2019-12-07 09:52:54 +01:00
Widget child = ErrorLoadingWrapper(
2022-09-06 18:28:12 +02:00
bodyBuilder: () => widget.bodyBuilder(_data as T, (v) {
2021-01-31 08:49:28 +01:00
setState(() {
_data = v;
});
}),
2019-12-07 09:52:54 +01:00
error: _error,
loading: _data == null,
reload: _refresh,
);
if (widget.canRefresh) {
child = RefreshWrapper(
onRefresh: _refresh,
body: child,
);
}
2019-09-25 11:06:36 +02:00
return CommonScaffold(
title: widget.title,
2019-12-07 09:52:54 +01:00
body: child,
2019-10-03 04:01:54 +02:00
action: _action,
2019-09-25 11:06:36 +02:00
);
}
}