ItemBuilder.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter/material.dart';
  2. // 非全屏FutureBuilder封装
  3. class ItemBuilder<T> extends StatelessWidget {
  4. final Future<T> future;
  5. final AsyncWidgetBuilder<T> successBuilder;
  6. final Future<dynamic> Function() onRefresh;
  7. final double? loadingHeight;
  8. final double? height;
  9. const ItemBuilder({
  10. Key? key,
  11. required this.future,
  12. required this.successBuilder,
  13. required this.onRefresh,
  14. this.height,
  15. this.loadingHeight,
  16. }) : super(key: key);
  17. @override
  18. Widget build(BuildContext context) {
  19. return LayoutBuilder(
  20. builder: (BuildContext context, BoxConstraints constraints) {
  21. var _maxWidth = constraints.maxWidth;
  22. var _loadingHeight = height ?? loadingHeight ?? _maxWidth / 2;
  23. return FutureBuilder(
  24. future: future,
  25. builder: (BuildContext context, AsyncSnapshot<T> snapshot) {
  26. if (snapshot.hasError) {
  27. print("${snapshot.error}");
  28. print("${snapshot.stackTrace}");
  29. return InkWell(
  30. onTap: onRefresh,
  31. child: SizedBox(
  32. width: _maxWidth,
  33. height: _loadingHeight,
  34. child: Center(
  35. child:
  36. Icon(Icons.sync_problem, size: _loadingHeight / 1.5),
  37. ),
  38. ),
  39. );
  40. }
  41. if (snapshot.connectionState != ConnectionState.done) {
  42. return SizedBox(
  43. width: _maxWidth,
  44. height: _loadingHeight,
  45. child: Center(
  46. child: Icon(Icons.sync, size: _loadingHeight / 1.5),
  47. ),
  48. );
  49. }
  50. return SizedBox(
  51. width: _maxWidth,
  52. height: height,
  53. child: successBuilder(context, snapshot),
  54. );
  55. });
  56. },
  57. );
  58. }
  59. }