143 lines
3.5 KiB
Dart
143 lines
3.5 KiB
Dart
// Author: fengshengxiong
|
|
// Date: 2024/5/7
|
|
// Description: 状态视图
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:tone_snap/data/enum/app_side_enum.dart';
|
|
import 'package:tone_snap/generated/assets.dart';
|
|
import 'package:tone_snap/global/app_config.dart';
|
|
import 'package:tone_snap/res/themes/app_colors.dart';
|
|
|
|
/// 四种视图状态
|
|
enum ViewState { normal, error, loading, empty }
|
|
|
|
class ViewStateWidget extends StatelessWidget {
|
|
const ViewStateWidget({
|
|
super.key,
|
|
required this.viewState,
|
|
required this.child,
|
|
this.cpiBgColor,
|
|
this.showTryAgain = false,
|
|
this.onTapTryAgain,
|
|
});
|
|
|
|
final ViewState viewState;
|
|
final Widget child;
|
|
final Color? cpiBgColor;
|
|
final bool showTryAgain;
|
|
final Function()? onTapTryAgain;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
switch (viewState) {
|
|
case ViewState.normal:
|
|
return child;
|
|
case ViewState.loading:
|
|
return loadingView(backgroundColor: cpiBgColor);
|
|
case ViewState.empty:
|
|
return AppConfig.appSideEnum == AppSideEnum.sideA ? emptyViewA() : emptyViewB(
|
|
showTryAgain: showTryAgain,
|
|
onTapTryAgain: onTapTryAgain,
|
|
);
|
|
case ViewState.error:
|
|
return errorView();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 加载中视图
|
|
Widget loadingView({
|
|
Color? color,
|
|
Color? backgroundColor,
|
|
}) {
|
|
return Center(
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2.w,
|
|
color: AppConfig.appSideEnum == AppSideEnum.sideA ? color : seedColor,
|
|
backgroundColor: backgroundColor,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 空视图A
|
|
Widget emptyViewA({String? msg, Color? textColor}) {
|
|
return Center(
|
|
child: Text(
|
|
msg ?? 'No data',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: textColor ?? Colors.white,
|
|
fontSize: 15.sp,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 空视图B
|
|
Widget emptyViewB({
|
|
required bool showTryAgain,
|
|
Function()? onTapTryAgain,
|
|
}) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Image.asset(
|
|
Assets.sideBEmpty,
|
|
width: 180.w,
|
|
height: 120.h,
|
|
),
|
|
if (showTryAgain) ...[
|
|
Text(
|
|
'An error occurred\nSorry, please try again later',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: const Color(0x73FFFFFF),
|
|
fontSize: 14.sp,
|
|
),
|
|
),
|
|
SizedBox(height: 10.h),
|
|
GestureDetector(
|
|
onTap: onTapTryAgain,
|
|
child: Container(
|
|
width: 122.w,
|
|
height: 35.h,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(
|
|
width: 1.w,
|
|
color: seedColor,
|
|
),
|
|
borderRadius: BorderRadius.circular(40).r,
|
|
),
|
|
child: Text(
|
|
'Try again',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: seedColor,
|
|
fontSize: 16.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,
|
|
),
|
|
),
|
|
);
|
|
}
|