pikapika/lib/screens/components/ItemBuilder.dart

63 lines
1.9 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2021-10-27 08:14:35 +00:00
// 非全屏FutureBuilder封装
2021-09-29 23:57:09 +00:00
class ItemBuilder<T> extends StatelessWidget {
final Future<T> future;
final AsyncWidgetBuilder<T> successBuilder;
final Future<dynamic> Function() onRefresh;
final double? loadingHeight;
final double? height;
const ItemBuilder({
Key? key,
required this.future,
required this.successBuilder,
required this.onRefresh,
this.height,
this.loadingHeight,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
var _maxWidth = constraints.maxWidth;
var _loadingHeight = height ?? loadingHeight ?? _maxWidth / 2;
return FutureBuilder(
future: future,
builder: (BuildContext context, AsyncSnapshot<T> snapshot) {
if (snapshot.hasError) {
print("${snapshot.error}");
print("${snapshot.stackTrace}");
return InkWell(
onTap: onRefresh,
child: Container(
width: _maxWidth,
height: _loadingHeight,
child: Center(
child:
Icon(Icons.sync_problem, size: _loadingHeight / 1.5),
),
),
);
}
if (snapshot.connectionState != ConnectionState.done) {
return Container(
width: _maxWidth,
height: _loadingHeight,
child: Center(
child: Icon(Icons.sync, size: _loadingHeight / 1.5),
),
);
}
return Container(
width: _maxWidth,
height: height,
child: successBuilder(context, snapshot),
);
});
},
);
}
}