修复切换设备每次 都选中在第一个
This commit is contained in:
@@ -52,14 +52,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
// 机器大图
|
||||
Transform.translate(
|
||||
offset: const Offset(30, 80), // 正数向右移动(例如 20 像素),负数向左
|
||||
child: Center(
|
||||
child: Image.asset(
|
||||
'assets/images/car.png',
|
||||
width: 330,
|
||||
height: 190,
|
||||
fit: BoxFit.contain,
|
||||
),
|
||||
),
|
||||
child: Center(child: Image.asset('assets/images/car.png', width: 330, height: 190, fit: BoxFit.contain)),
|
||||
),
|
||||
|
||||
// 顶部状态信息
|
||||
@@ -78,28 +71,12 @@ class ImmersionHeader extends StatelessWidget {
|
||||
mainAxisSize: MainAxisSize.min, // 尽可能收缩高度
|
||||
children: [
|
||||
// 设备名称和电量
|
||||
Text(
|
||||
_formatName(device.displayName),
|
||||
style: GoogleFonts.roboto(
|
||||
fontSize: 22,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
Text(_formatName(device.displayName), style: GoogleFonts.roboto(fontSize: 22, fontWeight: FontWeight.w900)),
|
||||
const SizedBox(height: 4),
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
'${device.onlineStatus == 1 ? "100" : "--"} %',
|
||||
style: GoogleFonts.roboto(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.chevron_right,
|
||||
size: 20,
|
||||
color: Colors.grey,
|
||||
),
|
||||
Text('${device.onlineStatus == 1 ? "100" : "--"} %', style: GoogleFonts.roboto(fontSize: 24, fontWeight: FontWeight.w900)),
|
||||
const Icon(Icons.chevron_right, size: 20, color: Colors.grey),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -123,9 +100,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
_buildCircleIcon(Icons.sync, () {
|
||||
// 1. 触发 Cubit 请求最新设备列表
|
||||
// 假设你的 username 存储在 AuthCubit 或类似的全局状态中
|
||||
final username =
|
||||
context.read<AppUserCubit>().state.user?.username ??
|
||||
"";
|
||||
final username = context.read<AppUserCubit>().state.user?.username ?? "";
|
||||
context.read<DevicesCubit>().fetchAllDevices(username);
|
||||
|
||||
// 2. 弹出窗口(窗口内部会根据状态显示转圈或列表)
|
||||
@@ -151,22 +126,14 @@ class ImmersionHeader extends StatelessWidget {
|
||||
// 在 offWhite 背景下,标签用纯白色会显得更精致
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
boxShadow: [
|
||||
BoxShadow(color: Colors.black.withOpacity(0.02), blurRadius: 4),
|
||||
],
|
||||
boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.02), blurRadius: 4)],
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
CircleAvatar(
|
||||
radius: 3,
|
||||
backgroundColor: isOnline ? Colors.green : Colors.grey,
|
||||
),
|
||||
CircleAvatar(radius: 3, backgroundColor: isOnline ? Colors.green : Colors.grey),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
isOnline ? "在线" : "离线",
|
||||
style: const TextStyle(fontSize: 12, color: Colors.black54),
|
||||
),
|
||||
Text(isOnline ? "在线" : "离线", style: const TextStyle(fontSize: 12, color: Colors.black54)),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -177,10 +144,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
return Container(
|
||||
width: 120,
|
||||
height: 6,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blueAccent,
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
),
|
||||
decoration: BoxDecoration(color: Colors.blueAccent, borderRadius: BorderRadius.circular(5)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -205,11 +169,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 30,
|
||||
color: isAccent ? Colors.blue : Colors.black54,
|
||||
),
|
||||
child: Icon(icon, size: 30, color: isAccent ? Colors.blue : Colors.black54),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -235,21 +195,11 @@ class ImmersionHeader extends StatelessWidget {
|
||||
margin: const EdgeInsets.symmetric(vertical: 12),
|
||||
width: 36,
|
||||
height: 5,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[300],
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
"切换设备",
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
decoration: BoxDecoration(color: Colors.grey[300], borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
const Text("切换设备", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 10),
|
||||
Container(
|
||||
height: 0.5,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 20),
|
||||
color: Colors.grey.withOpacity(0.1),
|
||||
),
|
||||
Container(height: 0.5, margin: const EdgeInsets.symmetric(horizontal: 20), color: Colors.grey.withOpacity(0.1)),
|
||||
|
||||
// 💡 动态内容区
|
||||
Expanded(
|
||||
@@ -264,10 +214,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
children: [
|
||||
const CircularProgressIndicator(color: Colors.blue),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
"加载中...",
|
||||
style: TextStyle(color: Colors.grey[600]),
|
||||
),
|
||||
Text("加载中...", style: TextStyle(color: Colors.grey[600])),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -275,26 +222,26 @@ class ImmersionHeader extends StatelessWidget {
|
||||
|
||||
// 2. 列表展示
|
||||
if (state.devices.isEmpty) {
|
||||
return const Center(child: Text("暂无可用设备"));
|
||||
return const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.description_outlined, size: 60, color: Colors.grey),
|
||||
SizedBox(height: 16),
|
||||
Text('暂无设备', style: TextStyle(fontSize: 16, color: Colors.grey)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.builder(
|
||||
// 💡 底部留出安全距离,防止最后一条滚不上来
|
||||
padding: EdgeInsets.only(
|
||||
bottom: MediaQuery.of(context).padding.bottom + 20,
|
||||
top: 10,
|
||||
),
|
||||
padding: EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom + 20, top: 10),
|
||||
itemCount: state.devices.length,
|
||||
itemBuilder: (context, index) {
|
||||
// 传入当前选中的 ID 方便做 UI 区分
|
||||
final isSelected =
|
||||
state.selectedDevice?.deviceName ==
|
||||
state.devices[index].deviceName;
|
||||
return _buildDeviceItem(
|
||||
state.devices[index],
|
||||
context,
|
||||
isSelected,
|
||||
);
|
||||
final isSelected = state.selectedDevice?.deviceName == state.devices[index].deviceName;
|
||||
return _buildDeviceItem(state.devices[index], context, isSelected);
|
||||
},
|
||||
);
|
||||
},
|
||||
@@ -307,11 +254,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeviceItem(
|
||||
DeviceEntity device,
|
||||
BuildContext context,
|
||||
bool isSelected,
|
||||
) {
|
||||
Widget _buildDeviceItem(DeviceEntity device, BuildContext context, bool isSelected) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
print("跳转到详情页: ${device.deviceAlias}");
|
||||
@@ -323,29 +266,14 @@ class ImmersionHeader extends StatelessWidget {
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
// 💡 如果是当前选中设备,边框颜色略深
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? Colors.blue.withOpacity(0.5)
|
||||
: Colors.grey.withOpacity(0.3),
|
||||
width: isSelected ? 1.5 : 1,
|
||||
),
|
||||
border: Border.all(color: isSelected ? Colors.blue.withOpacity(0.5) : Colors.grey.withOpacity(0.3), width: isSelected ? 1.5 : 1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
boxShadow: [
|
||||
if (isSelected)
|
||||
BoxShadow(color: Colors.blue.withOpacity(0.05), blurRadius: 4),
|
||||
],
|
||||
boxShadow: [if (isSelected) BoxShadow(color: Colors.blue.withOpacity(0.05), blurRadius: 4)],
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 设备图片
|
||||
Transform.translate(
|
||||
offset: const Offset(10, 0),
|
||||
child: Image.asset(
|
||||
'assets/images/car.png',
|
||||
width: 100,
|
||||
height: 80,
|
||||
),
|
||||
),
|
||||
Transform.translate(offset: const Offset(10, 0), child: Image.asset('assets/images/car.png', width: 100, height: 80)),
|
||||
const SizedBox(width: 12),
|
||||
// 设备信息
|
||||
Expanded(
|
||||
@@ -354,17 +282,11 @@ class ImmersionHeader extends StatelessWidget {
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.circle,
|
||||
color: device.isOnline ? Colors.green : Colors.grey,
|
||||
size: 12,
|
||||
),
|
||||
Icon(Icons.circle, color: device.isOnline ? Colors.green : Colors.grey, size: 12),
|
||||
const SizedBox(width: 4),
|
||||
Flexible(
|
||||
child: Text(
|
||||
(device.deviceAlias?.trim() ?? '').isEmpty
|
||||
? '未知设备'
|
||||
: device.deviceAlias!,
|
||||
(device.deviceAlias?.trim() ?? '').isEmpty ? '未知设备' : device.deviceAlias!,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
// 关键属性:处理文本溢出
|
||||
maxLines: 2, // 最多显示2行
|
||||
@@ -375,10 +297,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
const Text(
|
||||
"点击查看详情",
|
||||
style: TextStyle(color: Colors.grey, fontSize: 12),
|
||||
),
|
||||
const Text("点击查看详情", style: TextStyle(color: Colors.grey, fontSize: 12)),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -390,9 +309,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
? null
|
||||
: () async {
|
||||
// 💡 执行切换逻辑
|
||||
await context.read<DevicesCubit>().switchDevice(
|
||||
device,
|
||||
);
|
||||
await context.read<DevicesCubit>().switchDevice(device);
|
||||
// 切换成功后,UI 会自动更新(因为 BlocBuilder 在监听),我们可以关闭弹窗
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
@@ -408,10 +325,7 @@ class ImmersionHeader extends StatelessWidget {
|
||||
const SizedBox(height: 8),
|
||||
CCPrimaryButton(
|
||||
onPressed: () {
|
||||
context.push(
|
||||
RoutePaths.machineDetails,
|
||||
extra: device,
|
||||
); // 把当前点击的这个设备对象传过去
|
||||
context.push(RoutePaths.machineDetails, extra: device); // 把当前点击的这个设备对象传过去
|
||||
print("通过按钮进入详情,设备名称:$device");
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user