load_state_layout.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:flutter/material.dart';
  2. import 'package:ftrecruiter/comm/constants/color_constants.dart';
  3. import 'package:ftrecruiter/comm/widget/my_load_image.dart';
  4. import 'package:ftrecruiter/comm/widget/my_text_view.dart';
  5. import 'package:get/get.dart';
  6. ///四种视图状态
  7. enum LoadState { State_Success, State_Error, State_Loading, State_Empty }
  8. ///根据不同状态来展示不同的视图
  9. class LoadStateLayout extends StatefulWidget {
  10. final LoadState state; //页面状态
  11. final Widget? successWidget; //成功视图
  12. final VoidCallback? errorRetry; //错误事件处理
  13. const LoadStateLayout(
  14. {Key? key,
  15. this.state = LoadState.State_Loading, //默认为加载状态
  16. this.successWidget,
  17. this.errorRetry})
  18. : super(key: key);
  19. @override
  20. _LoadStateLayoutState createState() => _LoadStateLayoutState();
  21. }
  22. class _LoadStateLayoutState extends State<LoadStateLayout> {
  23. @override
  24. Widget build(BuildContext context) {
  25. return Container(
  26. //宽高都充满屏幕剩余空间
  27. width: double.infinity,
  28. height: double.infinity,
  29. child: _buildWidget,
  30. );
  31. }
  32. ///根据不同状态来显示不同的视图
  33. Widget get _buildWidget {
  34. switch (widget.state) {
  35. case LoadState.State_Success:
  36. return widget.successWidget ?? const SizedBox();
  37. case LoadState.State_Error:
  38. return _errorView;
  39. case LoadState.State_Loading:
  40. return _loadingView;
  41. case LoadState.State_Empty:
  42. return _emptyView;
  43. default:
  44. return _loadingView;
  45. }
  46. }
  47. ///加载中视图
  48. Widget get _loadingView {
  49. return Container(
  50. width: double.infinity,
  51. height: double.infinity,
  52. alignment: Alignment.center,
  53. child: Column(
  54. mainAxisSize: MainAxisSize.max,
  55. mainAxisAlignment: MainAxisAlignment.center,
  56. children: [
  57. const CircularProgressIndicator(
  58. strokeWidth: 3,
  59. valueColor: AlwaysStoppedAnimation(ColorConstants.appBlue),
  60. ),
  61. MyTextView('loading'.tr, marginTop: 15, fontSize: 15.5)
  62. ],
  63. ),
  64. );
  65. }
  66. ///错误视图
  67. Widget get _errorView {
  68. return SizedBox(
  69. width: double.infinity,
  70. height: double.infinity,
  71. child: GestureDetector(
  72. onTap: widget.errorRetry,
  73. child: Column(
  74. crossAxisAlignment: CrossAxisAlignment.center,
  75. mainAxisAlignment: MainAxisAlignment.center,
  76. children: <Widget>[
  77. const MyAssetImage('other/page_no_data.webp', width: 180, height: 180, fit: BoxFit.contain),
  78. MyTextView('load_error_try_again'.tr, marginTop: 10, fontSize: 15.5)
  79. ],
  80. )));
  81. }
  82. ///数据为空的视图
  83. Widget get _emptyView {
  84. return Container(
  85. width: double.infinity,
  86. height: double.infinity,
  87. child: Column(
  88. crossAxisAlignment: CrossAxisAlignment.center,
  89. mainAxisAlignment: MainAxisAlignment.center,
  90. children: <Widget>[
  91. const MyAssetImage('other/page_no_data.webp', width: 180, height: 180, fit: BoxFit.contain),
  92. MyTextView('load_no_data'.tr, marginTop: 10, fontSize: 15.5)
  93. ],
  94. ),
  95. );
  96. }
  97. }