You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
17 KiB

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../controllers/pairing_controller.dart';
import '../../../core/widgets/common_bottom_nav.dart';
import '../../../core/themes/app_theme.dart';
class PairingView extends GetView<PairingController> {
const PairingView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
return Scaffold(
backgroundColor: isDarkMode ? Colors.black : Colors.white,
body: SafeArea(
child: _buildMainContent(isDarkMode),
),
// bottomNavigationBar: Opacity(
// opacity: 0.5,
// child: const CommonBottomNav(currentIndex: 0),
// ),
);
}
// 主要内容
Widget _buildMainContent(bool isDarkMode) {
return Container(
width: double.infinity,
height: MediaQuery.of(Get.context!).size.height,
padding: EdgeInsets.symmetric(horizontal: 24.w),
// 添加SingleChildScrollView以防止溢出
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight:
MediaQuery.of(Get.context!).size.height - 100.h, // 减去底部导航和状态栏高度
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(height: 20.h), // 顶部间距
// 标题部分
_buildHeader(isDarkMode),
SizedBox(height: 20.h),
// 设备动画 - 根据状态显示不同动画
_buildDeviceAnimation(isDarkMode),
SizedBox(height: 20.h),
// 状态信息
_buildStatusInfo(isDarkMode),
SizedBox(height: 40.h),
// 底部按钮
_buildActionButtons(isDarkMode),
SizedBox(height: 20.h),
],
),
),
),
);
}
// 头部标题
Widget _buildHeader(bool isDarkMode) {
return Column(
children: [
Text(
'DeepSound',
style: TextStyle(
fontSize: 30.sp,
fontWeight: FontWeight.bold,
color: isDarkMode ? Colors.white : Colors.black,
),
),
SizedBox(height: 8.h),
Text(
'homeSlogan'.tr, // 连接世界,超越声音
style: TextStyle(
fontSize: 16.sp,
color: isDarkMode ? Colors.white60 : Colors.black54,
),
),
],
);
}
// 设备动画 - 根据状态显示不同的动画效果
Widget _buildDeviceAnimation(bool isDarkMode) {
return Obx(() {
final state = controller.state;
final isLoading = controller.isLoading.value;
final isReadyToStart = controller.isReadyToStart.value;
final isManualMode = controller.isManualMode.value;
// 根据状态决定动画颜色
Color primaryColor = Color(0xFF0066CC);
Color secondaryColor = Color(0xFF5E5CE6);
// 错误状态使用红色
if (state.name.startsWith('exit')) {
primaryColor = Colors.red;
secondaryColor = Colors.redAccent;
}
// 成功状态使用绿色
else if (state == PairingState.paired) {
primaryColor = Colors.green;
secondaryColor = Colors.greenAccent;
}
// 手动模式待机状态的特殊处理
if (isManualMode && isReadyToStart) {
return Container(
width: 240.w,
height: 240.w,
child: Stack(
alignment: Alignment.center,
children: [
// 静态背景圆圈
Container(
width: 200.w,
height: 200.w,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
primaryColor.withOpacity(0.1),
secondaryColor.withOpacity(0.1),
],
),
shape: BoxShape.circle,
),
),
// 设备图标
Container(
width: 120.w,
height: 120.w,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [primaryColor, secondaryColor],
),
shape: BoxShape.circle,
),
child: Icon(
Icons.headphones,
color: Colors.white,
size: 60.w,
),
),
],
),
);
}
return Container(
width: 240.w,
height: 240.w,
child: Stack(
alignment: Alignment.center,
children: [
// 脉搏背景 - 只在加载时显示动画
if (isLoading)
TweenAnimationBuilder<double>(
tween: Tween<double>(begin: 0.8, end: 1.1),
duration: const Duration(seconds: 2),
curve: Curves.easeInOut,
builder: (context, value, child) {
return Container(
width: 200.w * value,
height: 200.w * value,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
primaryColor.withOpacity(0.3),
secondaryColor.withOpacity(0.3),
],
),
shape: BoxShape.circle,
),
);
},
onEnd: () {
// 重新开始动画
},
),
// 旋转边框 - 只在加载时显示
if (isLoading)
SizedBox(
width: 100.w,
height: 100.w,
child: CircularProgressIndicator(
strokeWidth: 4.w,
valueColor: AlwaysStoppedAnimation<Color>(primaryColor),
backgroundColor: Colors.grey.withOpacity(0.2),
),
),
// 设备图标
Container(
width: 120.w,
height: 120.w,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [primaryColor, secondaryColor],
),
shape: BoxShape.circle,
),
child: Icon(
_getIconForState(state),
color: Colors.white,
size: 60.w,
),
),
],
),
);
});
}
// 根据状态获取图标
IconData _getIconForState(PairingState state) {
switch (state) {
case PairingState.paired:
return Icons.check_circle;
case PairingState.exitNoPermission:
case PairingState.exitBtOff:
return Icons.bluetooth_disabled;
case PairingState.exitNoClassic:
case PairingState.exitNotMatch:
return Icons.headphones_outlined;
case PairingState.exitPairFailed:
case PairingState.exitTimeout:
case PairingState.exitIllegal:
case PairingState.exitBound:
return Icons.error_outline;
default:
return Icons.headphones;
}
}
// 状态信息
Widget _buildStatusInfo(bool isDarkMode) {
return Obx(() {
final status = controller.statusDescription.value;
final state = controller.state;
final device = controller.currentDevice;
return Column(
children: [
// 状态文本 - 更大更醒目
Text(
status,
style: TextStyle(
fontSize: 20.sp,
fontWeight: FontWeight.w600,
color: isDarkMode ? Colors.white : Colors.black,
),
textAlign: TextAlign.center,
),
SizedBox(height: 10.h),
// 子状态提示文本 - 根据状态显示不同提示
Text(
_getSubtitleForState(state),
style: TextStyle(
fontSize: 14.sp,
color: isDarkMode ? Colors.white60 : Colors.grey[600],
),
textAlign: TextAlign.center,
),
// 当前连接设备(有设备时才显示)
if (device != null &&
(state == PairingState.classicOk ||
state == PairingState.serverVerify ||
state == PairingState.pairing ||
state == PairingState.paired)) ...[
SizedBox(height: 24.h),
_buildConnectedDevice(device, isDarkMode),
],
],
);
});
}
// 根据状态获取副标题
String _getSubtitleForState(PairingState state) {
if (controller.isManualMode.value && controller.isReadyToStart.value) {
return '点击下方按钮开始配对您的DeapSound设备';
}
switch (state) {
case PairingState.init:
case PairingState.btReady:
return controller.isManualMode.value ? '准备开始配对' : '正在准备配对环境...';
case PairingState.classicOk:
return '请先通过系统蓝牙连接DeapSound设备';
case PairingState.serverVerify:
case PairingState.pairing:
return '请稍候,正在完成配对...';
case PairingState.paired:
return '配对成功!即将跳转到主页...';
case PairingState.exitNoPermission:
return '需要蓝牙权限才能继续';
case PairingState.exitBtOff:
return '请开启蓝牙后重试';
case PairingState.exitNoClassic:
return '请先通过系统蓝牙连接DeapSound设备';
case PairingState.exitNotMatch:
return '请确保连接的是DeapSound设备';
case PairingState.exitTimeout:
return '扫描超时,请确保设备在附近';
case PairingState.exitPairFailed:
return '配对失败,请重试';
case PairingState.exitBound:
return '该设备已被其他账号绑定';
case PairingState.exitIllegal:
return '设备验证失败,请检查设备是否正常';
default:
return '';
}
}
// 已连接设备信息
Widget _buildConnectedDevice(device, bool isDarkMode) {
return Container(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'currentlyConnected'.tr, // 当前已连接:
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
color: isDarkMode ? Colors.white70 : Colors.black87,
),
),
SizedBox(height: 8.h),
_buildDeviceItem(device, isDarkMode),
],
),
);
}
// 设备项
Widget _buildDeviceItem(device, bool isDarkMode) {
return Container(
margin: EdgeInsets.only(bottom: 8.h),
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
decoration: BoxDecoration(
color: isDarkMode ? Colors.grey[900] : Colors.grey[100],
borderRadius: BorderRadius.circular(8.r),
),
child: Row(
children: [
Icon(
Icons.headphones,
color: isDarkMode ? Colors.white70 : Colors.black54,
size: 24.w,
),
SizedBox(width: 12.w),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
device.name ?? 'unknown'.tr,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w500,
color: isDarkMode ? Colors.white : Colors.black,
),
),
SizedBox(height: 4.h),
Text(
device.address ?? '',
style: TextStyle(
fontSize: 12.sp,
color: isDarkMode ? Colors.white54 : Colors.black45,
),
),
],
),
),
Icon(
Icons.bluetooth_connected,
color: Colors.green,
size: 20.w,
),
],
),
);
}
// 操作按钮
Widget _buildActionButtons(bool isDarkMode) {
return Obx(() {
final showActionButton = controller.showActionButton;
final canSkip = controller.canSkip;
final actionButtonText = controller.actionButtonText.value;
final isReadyToStart = controller.isReadyToStart.value;
final isManualMode = controller.isManualMode.value;
return Container(
width: double.infinity,
child: Column(
children: [
// 手动模式:显示开始配对按钮
if (isManualMode && isReadyToStart)
ElevatedButton(
onPressed: () => controller.startManualPairing(),
style: ElevatedButton.styleFrom(
backgroundColor: Color(0xFF0066CC),
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 16.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.r),
),
elevation: 0,
minimumSize: Size(double.infinity, 56.h),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.bluetooth_searching, size: 20.w),
SizedBox(width: 8.w),
Text(
'开始配对设备',
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
],
),
),
// 原有的状态按钮(错误处理等)
if (showActionButton && !isManualMode)
ElevatedButton(
onPressed: () => controller.handleActionButtonTap(),
style: ElevatedButton.styleFrom(
backgroundColor:
_getButtonColor(controller.state, isDarkMode),
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(vertical: 16.h),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.r),
),
elevation: 0,
minimumSize: Size(double.infinity, 56.h),
),
child: Text(
actionButtonText,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,
),
),
),
// 跳过按钮和重置按钮
if (canSkip) ...[
SizedBox(height: 12.h),
TextButton(
onPressed: () => controller.skipPairing(),
style: TextButton.styleFrom(
foregroundColor: Color(0xFF0066CC),
padding: EdgeInsets.symmetric(vertical: 12.h),
minimumSize: Size(double.infinity, 48.h),
),
child: Text(
'talkLater'.tr, // 稍后再说
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w500,
),
),
),
],
// 添加重置按钮(在错误状态下)
if (!isManualMode &&
!controller.isLoading.value &&
controller.state.name.startsWith('exit')) ...[
SizedBox(height: 8.h),
TextButton(
onPressed: () => controller.resetToManualMode(),
style: TextButton.styleFrom(
foregroundColor: Colors.grey,
padding: EdgeInsets.symmetric(vertical: 8.h),
),
child: Text(
'重新配对',
style: TextStyle(fontSize: 14.sp),
),
),
],
],
),
);
});
}
// 根据状态获取按钮颜色
Color _getButtonColor(PairingState state, bool isDarkMode) {
switch (state) {
case PairingState.exitNoPermission:
case PairingState.exitBtOff:
return isDarkMode ? Colors.grey[800]! : Colors.grey[600]!;
case PairingState.exitPairFailed:
case PairingState.exitBound:
case PairingState.exitIllegal:
return Colors.red;
default:
return Color(0xFF0066CC);
}
}
}