WallPaper_FSX_Flutter/lib/common/components/view_state_widget.dart
2024-05-13 13:44:27 +08:00

76 lines
1.5 KiB
Dart

// 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<Color?>? 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,
),
),
);
}