// Author: fengshengxiong // Date: 2024/5/7 // Description: 状态视图 import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; /// 四种视图状态 enum ViewState { normal, error, loading, empty } class ViewStateWidget extends StatelessWidget { const ViewStateWidget({super.key, required this.state, required this.child}); final ViewState state; final Widget child; @override Widget build(BuildContext context) { switch (state) { case ViewState.normal: return child; case ViewState.loading: return loadingView(); case ViewState.error: return errorView; case ViewState.empty: return emptyView; } } } /// 加载中视图 Widget loadingView({ Color? color, Animation? valueColor, Color? backgroundColor, double? value, }) { return Center( child: CircularProgressIndicator( color: color, valueColor: valueColor, backgroundColor: backgroundColor ?? Colors.grey, value: value, ), ); } /// 空视图 Widget get emptyView { return Center( child: Text( 'No data available', textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 14.sp, ), ), ); } /// 错误视图 Widget get errorView { return Center( child: Text( 'An error occurred, please try again later', textAlign: TextAlign.center, style: TextStyle( color: Colors.white, fontSize: 14.sp, ), ), ); }