pikapika/lib/screens/components/ContentBuilder.dart

37 lines
1002 B
Dart
Raw Permalink Normal View History

2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
import 'ContentError.dart';
import 'ContentLoading.dart';
class ContentBuilder<T> extends StatelessWidget {
final Future<T> future;
final Future<dynamic> Function() onRefresh;
final AsyncWidgetBuilder<T> successBuilder;
2022-07-12 07:28:47 +00:00
const ContentBuilder({
required Key? key,
required this.future,
required this.onRefresh,
required this.successBuilder,
}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (BuildContext context, AsyncSnapshot<T> snapshot) {
if (snapshot.hasError) {
return ContentError(
error: snapshot.error,
stackTrace: snapshot.stackTrace,
onRefresh: onRefresh,
);
}
if (snapshot.connectionState != ConnectionState.done) {
2022-03-25 14:57:30 +00:00
return const ContentLoading(label: '加载中');
2021-09-29 23:57:09 +00:00
}
return successBuilder(context, snapshot);
},
);
}
}