import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:loading_animation_widget/loading_animation_widget.dart'; import 'package:wallpaperx/generated/assets.dart'; /// 四种视图状态 enum ViewState { normal, error, loading, empty } class ViewStateWidget extends StatelessWidget { const ViewStateWidget({ super.key, required this.viewState, required this.child, }); final ViewState viewState; final Widget child; @override Widget build(BuildContext context) { switch (viewState) { case ViewState.normal: return child; case ViewState.loading: return loadingViewV1(); case ViewState.empty: return emptyView; case ViewState.error: return errorView; } } } /// 加载中视图 Widget loadingViewV1({ Color? color, }) { return Center( child: LoadingAnimationWidget.stretchedDots( color: color?? const Color(0xFF666666), size: 30.w, ), ); } /// 加载中视图 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 Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset( Assets.iconDataEmpty, width: 120.w, color: const Color(0xFF666666), gaplessPlayback: true, ), 10.verticalSpace, Text( 'No data available', textAlign: TextAlign.center, style: TextStyle( color: const Color(0xFF666666), fontSize: 14.sp, ), ), ], ); } /// 错误视图 Widget get errorView { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset( Assets.iconImgErrorType, width: 120.w, color: const Color(0xFF666666), gaplessPlayback: true, ), // 10.verticalSpace, // Text( // 'An error occurred, please try again later', // textAlign: TextAlign.center, // style: TextStyle( // color: const Color(0xFF666666), // fontSize: 14.sp, // ), // ) ], ); }