517 lines
17 KiB
Dart
517 lines
17 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_state.dart';
|
||
|
||
class MachineDetailsPage extends StatefulWidget {
|
||
final DeviceEntity? device;
|
||
|
||
const MachineDetailsPage({super.key, this.device});
|
||
|
||
@override
|
||
State<MachineDetailsPage> createState() => _MachineDetailsPageState();
|
||
}
|
||
|
||
class _MachineDetailsPageState extends State<MachineDetailsPage> {
|
||
late TextEditingController _nameController;
|
||
late String _deviceName;
|
||
late String _deviceId;
|
||
BuildContext? _dialogContext;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
|
||
_deviceName = (widget.device?.deviceAlias?.trim().isEmpty ?? true)
|
||
? '未知设备'
|
||
: widget.device!.deviceAlias!;
|
||
_deviceId = widget.device?.deviceName ?? '';
|
||
|
||
_nameController = TextEditingController();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_nameController.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
/// 弹出修改设备名称底部抽屉
|
||
void _showEditNameSheet() {
|
||
_nameController.text = _deviceName;
|
||
|
||
showModalBottomSheet(
|
||
context: context,
|
||
isScrollControlled: true,
|
||
shape: const RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
||
),
|
||
builder: (ctx) {
|
||
return Padding(
|
||
padding: EdgeInsets.only(
|
||
left: 20,
|
||
right: 20,
|
||
top: 20,
|
||
bottom: MediaQuery.of(ctx).viewInsets.bottom + 20,
|
||
),
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
const Text(
|
||
"修改设备名称",
|
||
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
|
||
),
|
||
const SizedBox(height: 16),
|
||
TextField(
|
||
controller: _nameController,
|
||
autofocus: true,
|
||
maxLength: 20,
|
||
decoration: const InputDecoration(
|
||
hintText: "请输入设备名称",
|
||
border: OutlineInputBorder(),
|
||
),
|
||
),
|
||
const SizedBox(height: 10),
|
||
Row(
|
||
children: [
|
||
Expanded(
|
||
child: OutlinedButton(
|
||
onPressed: () => Navigator.pop(ctx),
|
||
child: const Text("取消"),
|
||
),
|
||
),
|
||
const SizedBox(width: 12),
|
||
Expanded(
|
||
child: OutlinedButton(
|
||
onPressed: () {
|
||
final text = _nameController.text.trim();
|
||
|
||
if (text.isEmpty) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(content: Text("名称不能为空")),
|
||
);
|
||
return;
|
||
}
|
||
|
||
setState(() {
|
||
_deviceName = text;
|
||
});
|
||
|
||
Navigator.pop(ctx);
|
||
|
||
ScaffoldMessenger.of(
|
||
context,
|
||
).showSnackBar(SnackBar(content: Text("已修改为:$text")));
|
||
},
|
||
child: const Text("确认"),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
if (widget.device == null) {
|
||
return const Scaffold(body: Center(child: Text("未获取到设备信息")));
|
||
}
|
||
|
||
final currentDevice = widget.device!;
|
||
return BlocListener<DevicesCubit, DevicesState>(
|
||
listener: (context, state) {
|
||
// 只有解绑操作触发的状态变化才处理
|
||
if (state.isLoading || state.errorMessage != null) {
|
||
_handleUnbindResult(state);
|
||
}
|
||
},
|
||
child: Scaffold(
|
||
backgroundColor: const Color(0xFFF5F5F5),
|
||
appBar: AppBar(
|
||
centerTitle: true,
|
||
title: const Text(
|
||
'设备详情',
|
||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||
),
|
||
leading: IconButton(
|
||
icon: const Icon(Icons.arrow_back_ios),
|
||
onPressed: () => Navigator.pop(context),
|
||
),
|
||
),
|
||
body: SingleChildScrollView(
|
||
padding: const EdgeInsets.all(16),
|
||
child: Column(
|
||
children: [
|
||
_buildDeviceStatusCard(currentDevice),
|
||
const SizedBox(height: 16),
|
||
_buildBasicInfoCard(context, currentDevice), // 传递context和设备数据
|
||
const SizedBox(height: 16),
|
||
_buildResourceCenterCard(),
|
||
const SizedBox(height: 24),
|
||
_buildUnbindButton(context),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 设备状态卡片
|
||
Widget _buildDeviceStatusCard(DeviceEntity device) {
|
||
return Container(
|
||
padding: const EdgeInsets.all(24),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
),
|
||
child: Column(
|
||
children: [
|
||
Image.asset('assets/images/car.png', width: 300, fit: BoxFit.contain),
|
||
const SizedBox(height: 12),
|
||
Text(
|
||
device.isOnline ? '在线' : '离线',
|
||
style: TextStyle(
|
||
fontSize: 14,
|
||
color: device.isOnline
|
||
? const Color(0xFF00C853)
|
||
: const Color(0xFF999999),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
// 基本信息卡片(接收context和设备数据参数)
|
||
Widget _buildBasicInfoCard(BuildContext context, DeviceEntity device) {
|
||
// 处理设备名称(空值兜底)
|
||
final deviceName = (device.deviceAlias?.trim() ?? '').isEmpty
|
||
? '未知设备'
|
||
: device.deviceAlias!;
|
||
// 处理设备ID(空值兜底)
|
||
final deviceId = device.deviceName ?? '未知ID';
|
||
|
||
return Container(
|
||
padding: const EdgeInsets.all(16),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
const Padding(
|
||
padding: EdgeInsets.only(bottom: 16),
|
||
child: Text(
|
||
'基本信息',
|
||
style: TextStyle(fontSize: 14, color: Color(0xFF999999)),
|
||
),
|
||
),
|
||
// 设备名称行 - 传递context参数
|
||
_buildInfoRow(
|
||
context: context, // 关键:补上必填的context参数
|
||
icon: Icons.info_outline,
|
||
label: '设备名称',
|
||
trailing: GestureDetector(
|
||
onTap: _showEditNameSheet,
|
||
child: Row(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
Text(_deviceName),
|
||
const SizedBox(width: 8),
|
||
const Icon(Icons.edit, size: 16, color: Color(0xFF007AFF)),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
const Divider(height: 32, color: Color(0xFFF0F0F0)),
|
||
// 设备ID行 - 传递context参数
|
||
// 设备ID行 - 传递context参数
|
||
_buildInfoRow(
|
||
context: context,
|
||
icon: Icons.shield_outlined,
|
||
label: '设备ID',
|
||
trailing: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.end, // 右对齐
|
||
mainAxisSize: MainAxisSize.min, // 只占必要高度
|
||
children: [
|
||
// 核心修改:去掉宽度限制,允许换行显示全部ID
|
||
SizedBox(
|
||
// 取消固定宽度,让文本自适应(也可设置最大宽度适配屏幕)
|
||
width:
|
||
MediaQuery.of(context).size.width - 180, // 适配屏幕宽度(避免超出)
|
||
child: GestureDetector(
|
||
onLongPress: () {
|
||
// 长按文本复制
|
||
Clipboard.setData(ClipboardData(text: deviceId));
|
||
if (context.mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(
|
||
content: Text('设备ID已复制'),
|
||
duration: Duration(seconds: 1),
|
||
),
|
||
);
|
||
}
|
||
},
|
||
child: Text(
|
||
deviceId,
|
||
textAlign: TextAlign.right, // 右对齐更美观
|
||
softWrap: true, // 允许换行
|
||
overflow: TextOverflow.visible, // 显示全部内容(无省略号)
|
||
|
||
style: const TextStyle(
|
||
fontSize: 14,
|
||
color: Colors.black87,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
const SizedBox(height: 4), // 文本和复制图标间距
|
||
// 复制图标单独一行
|
||
GestureDetector(
|
||
onTap: () {
|
||
Clipboard.setData(ClipboardData(text: deviceId));
|
||
if (context.mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(
|
||
content: Text('设备ID已复制到剪贴板'),
|
||
duration: Duration(seconds: 1),
|
||
),
|
||
);
|
||
}
|
||
},
|
||
child: const Icon(
|
||
Icons.copy,
|
||
size: 16,
|
||
color: Color(0xFF999999),
|
||
),
|
||
),
|
||
|
||
//_buildInfoRow(
|
||
// context: context, // 关键:补上必填的context参数
|
||
// icon: Icons.info_outline,
|
||
// label: '激活时间',
|
||
// trailing: Row(
|
||
// mainAxisSize: MainAxisSize.min, // 防止Row撑满宽度
|
||
// children: [
|
||
// // 显示真实设备名称
|
||
// Text(activeTime),
|
||
// const SizedBox(width: 8),
|
||
// const Icon(
|
||
// Icons.edit,
|
||
// size: 16,
|
||
// color: Color(0xFF007AFF),
|
||
// ),
|
||
// ],
|
||
// ),
|
||
//),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
// 资源中心卡片(无设备数据依赖)
|
||
Widget _buildResourceCenterCard() {
|
||
return Container(
|
||
padding: const EdgeInsets.all(16),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
const Padding(
|
||
padding: EdgeInsets.only(bottom: 16),
|
||
child: Text(
|
||
'资源中心',
|
||
style: TextStyle(fontSize: 14, color: Color(0xFF999999)),
|
||
),
|
||
),
|
||
_buildResourceItem('使用说明', Icons.description_outlined),
|
||
const Divider(height: 32, color: Color(0xFFF0F0F0)),
|
||
_buildResourceItem('产品手册', Icons.book_outlined),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
// 信息行组件(添加context必填参数)
|
||
Widget _buildInfoRow({
|
||
required BuildContext context, // 必填的context参数
|
||
required IconData icon,
|
||
required String label,
|
||
required Widget trailing,
|
||
}) {
|
||
return Row(
|
||
children: [
|
||
Icon(icon, size: 18, color: const Color(0xFF999999)),
|
||
const SizedBox(width: 12),
|
||
Text(label, style: const TextStyle(fontSize: 16)),
|
||
const Spacer(),
|
||
trailing,
|
||
],
|
||
);
|
||
}
|
||
|
||
// 资源项组件
|
||
Widget _buildResourceItem(String title, IconData icon) {
|
||
return Row(
|
||
children: [
|
||
Icon(icon, size: 18, color: const Color(0xFF007AFF)),
|
||
const SizedBox(width: 12),
|
||
Text(title, style: const TextStyle(fontSize: 16)),
|
||
const Spacer(),
|
||
const Icon(Icons.arrow_forward_ios, size: 16, color: Color(0xFF999999)),
|
||
],
|
||
);
|
||
}
|
||
|
||
// 解绑设备按钮
|
||
Widget _buildUnbindButton(BuildContext context) {
|
||
return SizedBox(
|
||
width: double.infinity,
|
||
height: 48,
|
||
child: ElevatedButton(
|
||
style: ElevatedButton.styleFrom(
|
||
backgroundColor: Colors.white,
|
||
foregroundColor: const Color(0xFFFF3B30),
|
||
shape: RoundedRectangleBorder(
|
||
borderRadius: BorderRadius.circular(12),
|
||
),
|
||
),
|
||
onPressed: () {
|
||
// 实现解绑逻辑
|
||
showDialog(
|
||
context: context,
|
||
builder: (dialogContext) => AlertDialog(
|
||
title: const Text('确认解绑'),
|
||
content: Text('确定要解绑【${_deviceName ?? '该设备'}】吗?'),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(dialogContext),
|
||
child: const Text('取消'),
|
||
),
|
||
TextButton(
|
||
onPressed: _executeUnbind, // 执行解绑
|
||
//onPressed: () {
|
||
|
||
// //Navigator.pop(dialogContext);
|
||
// //context.read<DevicesCubit>().unbindDevice(
|
||
// // _deviceId ?? '',
|
||
// // _deviceName ?? '',
|
||
// //);
|
||
// // 执行解绑操作(传入设备ID)
|
||
// // _unbindDevice(device?.deviceName ?? '');
|
||
//},
|
||
child: const Text('确认'),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
},
|
||
child: const Row(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
Icon(Icons.unarchive_outlined, size: 18),
|
||
SizedBox(width: 12),
|
||
Text('解绑设备', style: TextStyle(fontSize: 12)),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Future<void> _executeUnbind() async {
|
||
if (_deviceId.isEmpty || _deviceName.isEmpty) {
|
||
if (context.mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
const SnackBar(
|
||
content: Text("设备信息异常,无法解绑"),
|
||
backgroundColor: Colors.red,
|
||
),
|
||
);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// 调用 Cubit 解绑方法
|
||
await context.read<DevicesCubit>().unbindDevice(_deviceId, _deviceName);
|
||
}
|
||
|
||
/// 处理解绑结果(成功/失败)
|
||
void _handleUnbindResult(DevicesState state) {
|
||
// 1. 关闭确认弹窗
|
||
if (_dialogContext != null && Navigator.canPop(_dialogContext!)) {
|
||
Navigator.pop(_dialogContext!);
|
||
_dialogContext = null;
|
||
}
|
||
|
||
// 2. 处理加载状态(隐藏加载弹窗/按钮禁用等)
|
||
if (state.isLoading) {
|
||
// 可选:显示加载弹窗
|
||
showDialog(
|
||
context: context,
|
||
barrierDismissible: false,
|
||
builder: (ctx) => const AlertDialog(
|
||
content: Row(
|
||
children: [
|
||
CircularProgressIndicator(),
|
||
SizedBox(width: 16),
|
||
Text("正在解绑设备..."),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
return;
|
||
}
|
||
|
||
// 3. 处理结果
|
||
if (state.errorMessage?.isEmpty == true) {
|
||
// 解绑成功
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
SnackBar(
|
||
content: Text("设备「$_deviceName」解绑成功"),
|
||
backgroundColor: Colors.green,
|
||
duration: const Duration(seconds: 2),
|
||
),
|
||
);
|
||
|
||
// 延迟返回上一页(让用户看到提示)
|
||
Future.delayed(const Duration(seconds: 1), () {
|
||
if (context.mounted) {
|
||
Navigator.pop(context); // 返回设备列表页
|
||
}
|
||
});
|
||
} else if (state.errorMessage?.isNotEmpty == true) {
|
||
// 解绑失败
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
SnackBar(
|
||
content: Text("解绑失败:${state.errorMessage ?? '未知错误'}"),
|
||
backgroundColor: Colors.red,
|
||
),
|
||
);
|
||
|
||
// 关闭加载弹窗(如果存在)
|
||
if (Navigator.canPop(context)) {
|
||
Navigator.pop(context);
|
||
}
|
||
}
|
||
final username = context.read<AppUserCubit>().state.user?.username ?? "";
|
||
context.read<DevicesCubit>().fetchAllDevices(username);
|
||
context.go(RoutePaths.home);
|
||
}
|
||
}
|