+141
@@ -0,0 +1,141 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:crisp_sdk/crisp_sdk.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import '../controllers/kr_crisp_controller.dart';
|
||||
import 'package:kaer_with_panels/app/localization/app_translations.dart';
|
||||
import 'package:kaer_with_panels/app/widgets/kr_app_text_style.dart';
|
||||
import '../../../widgets/kr_simple_loading.dart';
|
||||
|
||||
/// Crisp 客服聊天视图
|
||||
class KRCrispView extends GetView<KRCrispController> {
|
||||
const KRCrispView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopScope(
|
||||
onPopInvokedWithResult: (didPop, result) async {
|
||||
if (didPop) {
|
||||
await controller.kr_cleanupResources();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
|
||||
appBar: _kr_buildAppBar(context),
|
||||
body: _kr_buildBody(context),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建导航栏
|
||||
PreferredSizeWidget _kr_buildAppBar(BuildContext context) {
|
||||
return AppBar(
|
||||
backgroundColor: Theme.of(context).cardColor,
|
||||
elevation: 0,
|
||||
title: Text(
|
||||
AppTranslations.kr_userInfo.customerService,
|
||||
style: KrAppTextStyle(
|
||||
color: Theme.of(context).textTheme.bodyMedium?.color,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
leading: IconButton(
|
||||
icon: Icon(
|
||||
Icons.arrow_back_ios,
|
||||
size: 20.sp,
|
||||
color: Theme.of(context).textTheme.bodyMedium?.color,
|
||||
),
|
||||
onPressed: () => _kr_handleBack(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建主体内容
|
||||
Widget _kr_buildBody(BuildContext context) {
|
||||
return Container(
|
||||
color: Theme.of(context).scaffoldBackgroundColor,
|
||||
child: Obx(() {
|
||||
if (controller.kr_isLoading.value) {
|
||||
return _kr_buildLoadingView(context, '正在初始化客服系统...');
|
||||
}
|
||||
|
||||
if (controller.kr_isInitialized.value && controller.crispController != null) {
|
||||
return CrispView(
|
||||
crispController: controller.crispController!,
|
||||
clearCache: true,
|
||||
onSessionIdReceived: _kr_onSessionIdReceived,
|
||||
);
|
||||
}
|
||||
|
||||
return _kr_buildErrorView(context);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建加载视图
|
||||
Widget _kr_buildLoadingView(BuildContext context, String message) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
KRSimpleLoading(
|
||||
color: Colors.blue,
|
||||
size: 50.0,
|
||||
),
|
||||
if (message.isNotEmpty) SizedBox(height: 16.sp),
|
||||
if (message.isNotEmpty)
|
||||
Text(
|
||||
message,
|
||||
style: KrAppTextStyle(
|
||||
fontSize: 14,
|
||||
color: Theme.of(context).textTheme.bodyMedium?.color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建错误视图
|
||||
Widget _kr_buildErrorView(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 64.sp,
|
||||
color: Colors.red,
|
||||
),
|
||||
SizedBox(height: 16.sp),
|
||||
Text(
|
||||
'客服系统初始化失败',
|
||||
style: KrAppTextStyle(
|
||||
fontSize: 16,
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
SizedBox(height: 24.sp),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
controller.kr_initializeCrisp();
|
||||
},
|
||||
child: Text('重试'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 处理返回事件
|
||||
Future<void> _kr_handleBack() async {
|
||||
await controller.kr_cleanupResources();
|
||||
Get.back();
|
||||
}
|
||||
|
||||
/// 处理会话 ID 接收事件
|
||||
void _kr_onSessionIdReceived(String sessionId) {
|
||||
debugPrint('Crisp 会话 ID: $sessionId');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user