import 'package:flutter/material.dart'; import '../../core/models.dart'; class DeviceList extends StatelessWidget { const DeviceList({ super.key, required this.devices, required this.selectedId, required this.onSelected, required this.onRefresh, this.refreshing = false, this.shrinkWrap = false, }); final List devices; final String? selectedId; final ValueChanged onSelected; final VoidCallback onRefresh; final bool refreshing; final bool shrinkWrap; @override Widget build(BuildContext context) { final theme = Theme.of(context); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: const EdgeInsets.fromLTRB(16, 16, 16, 8), child: Row( children: [ Icon(Icons.devices_other, size: 20, color: theme.colorScheme.primary), const SizedBox(width: 8), Text('可连接设备', style: theme.textTheme.titleMedium), const Spacer(), Text('${devices.length} 台', style: theme.textTheme.bodySmall), IconButton( tooltip: '重新发现', onPressed: onRefresh, icon: refreshing ? const SizedBox( width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2), ) : const Icon(Icons.refresh), ), ], ), ), if (shrinkWrap) _buildList() else Expanded(child: _buildList()), ], ); } Widget _buildList() { if (devices.isEmpty) return _EmptyState(onRefresh: onRefresh); return ListView.separated( shrinkWrap: shrinkWrap, physics: shrinkWrap ? const NeverScrollableScrollPhysics() : null, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), itemCount: devices.length, separatorBuilder: (_, __) => const SizedBox(height: 8), itemBuilder: (context, index) => _DeviceCard( device: devices[index], selected: devices[index].id == selectedId, onTap: () => onSelected(devices[index]), ), ); } } class _DeviceCard extends StatelessWidget { const _DeviceCard({required this.device, required this.selected, required this.onTap}); final DeviceInfo device; final bool selected; final VoidCallback onTap; @override Widget build(BuildContext context) { final theme = Theme.of(context); return Card( clipBehavior: Clip.antiAlias, color: selected ? theme.colorScheme.primaryContainer : null, child: InkWell( onTap: onTap, child: Padding( padding: const EdgeInsets.all(12), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ CircleAvatar( backgroundColor: selected ? theme.colorScheme.primary : theme.colorScheme.surfaceContainerHighest, foregroundColor: selected ? theme.colorScheme.onPrimary : theme.colorScheme.onSurface, child: const Icon(Icons.android), ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(device.name, style: theme.textTheme.titleSmall), const SizedBox(height: 2), Text('${device.ip}:${device.controlPort} · ${device.model}', style: theme.textTheme.bodySmall, maxLines: 1, overflow: TextOverflow.ellipsis), ], ), ), if (selected) Icon(Icons.check_circle, color: theme.colorScheme.primary), ], ), const SizedBox(height: 10), Wrap( spacing: 6, runSpacing: 4, children: [ for (final capability in device.capabilities.take(4)) Chip( visualDensity: VisualDensity.compact, label: Text(capability, style: theme.textTheme.labelSmall), padding: EdgeInsets.zero, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, ), ], ), const SizedBox(height: 6), Text( [ if (device.fingerprint.isNotEmpty) '指纹 ${device.shortFingerprint}', if (device.resolution.isNotEmpty) device.resolution, 'UDP 自动发现', ].join(' · '), style: theme.textTheme.labelSmall?.copyWith(color: theme.colorScheme.outline), ), ], ), ), ), ); } } class _EmptyState extends StatelessWidget { const _EmptyState({required this.onRefresh}); final VoidCallback onRefresh; @override Widget build(BuildContext context) { return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.wifi_find, size: 48), const SizedBox(height: 12), const Text('未发现设备'), const SizedBox(height: 4), Text( '被控端开启投屏后会自动出现(UDP 41003 自动发现,无需填写 IP)\n' '若长时间未出现,请确认两端在同一网段且防火墙放行 UDP 41003', style: Theme.of(context).textTheme.bodySmall, textAlign: TextAlign.center, ), const SizedBox(height: 12), OutlinedButton.icon( onPressed: onRefresh, icon: const Icon(Icons.refresh), label: const Text('重新扫描'), ), ], ), ); } }