ToneSnap_FSX_Flutter/lib/components/view_state_widget.dart
fengshengxiong c7cbdb04be 个人曲库
2024-07-14 16:13:46 +08:00

85 lines
1.7 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.viewState,
required this.child,
this.cpiBgColor,
});
final ViewState viewState;
final Widget child;
final Color? cpiBgColor;
@override
Widget build(BuildContext context) {
switch (viewState) {
case ViewState.normal:
return child;
case ViewState.loading:
return loadingView(
backgroundColor: cpiBgColor,
);
case ViewState.empty:
return emptyView();
case ViewState.error:
return errorView();
}
}
}
/// 加载中视图
Widget loadingView({
Color? color,
Animation<Color?>? valueColor,
Color? backgroundColor,
double? value,
}) {
return Center(
child: CircularProgressIndicator(
strokeWidth: 3,
color: color,
valueColor: valueColor,
backgroundColor: backgroundColor,
value: value,
),
);
}
/// 空视图
Widget emptyView({String? msg, Color? textColor}) {
return Center(
child: Text(
msg ?? 'No data',
textAlign: TextAlign.center,
style: TextStyle(
color: textColor ?? Colors.white,
fontSize: 15.sp,
),
),
);
}
/// 错误视图
Widget errorView({String? msg, Color? textColor}) {
return Center(
child: Text(
msg ?? 'An error occurred, please try again later',
textAlign: TextAlign.center,
style: TextStyle(
color: textColor ?? Colors.white,
fontSize: 15.sp,
),
),
);
}