import 'package:flutter/material.dart'; import 'package:ftrecruiter/comm/constants/color_constants.dart'; import 'package:ftrecruiter/comm/widget/my_load_image.dart'; import 'package:ftrecruiter/comm/widget/my_text_view.dart'; import 'package:get/get.dart'; ///四种视图状态 enum LoadState { State_Success, State_Error, State_Loading, State_Empty } ///根据不同状态来展示不同的视图 class LoadStateLayout extends StatefulWidget { final LoadState state; //页面状态 final Widget? successWidget; //成功视图 final VoidCallback? errorRetry; //错误事件处理 const LoadStateLayout( {Key? key, this.state = LoadState.State_Loading, //默认为加载状态 this.successWidget, this.errorRetry}) : super(key: key); @override _LoadStateLayoutState createState() => _LoadStateLayoutState(); } class _LoadStateLayoutState extends State { @override Widget build(BuildContext context) { return Container( //宽高都充满屏幕剩余空间 width: double.infinity, height: double.infinity, child: _buildWidget, ); } ///根据不同状态来显示不同的视图 Widget get _buildWidget { switch (widget.state) { case LoadState.State_Success: return widget.successWidget ?? const SizedBox(); case LoadState.State_Error: return _errorView; case LoadState.State_Loading: return _loadingView; case LoadState.State_Empty: return _emptyView; default: return _loadingView; } } ///加载中视图 Widget get _loadingView { return Container( width: double.infinity, height: double.infinity, alignment: Alignment.center, child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, children: [ const CircularProgressIndicator( strokeWidth: 3, valueColor: AlwaysStoppedAnimation(ColorConstants.appBlue), ), MyTextView('loading'.tr, marginTop: 15, fontSize: 15.5) ], ), ); } ///错误视图 Widget get _errorView { return SizedBox( width: double.infinity, height: double.infinity, child: GestureDetector( onTap: widget.errorRetry, child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ const MyAssetImage('other/page_no_data.webp', width: 180, height: 180, fit: BoxFit.contain), MyTextView('load_error_try_again'.tr, marginTop: 10, fontSize: 15.5) ], ))); } ///数据为空的视图 Widget get _emptyView { return Container( width: double.infinity, height: double.infinity, child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ const MyAssetImage('other/page_no_data.webp', width: 180, height: 180, fit: BoxFit.contain), MyTextView('load_no_data'.tr, marginTop: 10, fontSize: 15.5) ], ), ); } }