import 'package:flutter/material.dart'; import '../../core/models.dart'; class ConnectBar extends StatelessWidget { const ConnectBar({ super.key, required this.phase, required this.device, required this.onConnect, required this.onDisconnect, }); final ConnectionPhase phase; final DeviceInfo? device; final VoidCallback onConnect; final VoidCallback onDisconnect; @override Widget build(BuildContext context) { final theme = Theme.of(context); final connected = phase.isConnected; final busy = phase.isBusy; final discovered = phase != ConnectionPhase.idle; final handshaken = phase == ConnectionPhase.probing || phase == ConnectionPhase.direct || phase == ConnectionPhase.relaying; final color = switch (phase) { ConnectionPhase.direct || ConnectionPhase.relaying => theme.colorScheme.primary, ConnectionPhase.failed => theme.colorScheme.error, _ => theme.colorScheme.outline, }; return Card( child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Container( width: 10, height: 10, decoration: BoxDecoration(color: color, shape: BoxShape.circle), ), const SizedBox(width: 10), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(phase.label, style: theme.textTheme.titleSmall), Text( device == null ? '请从左侧选择一台设备' : '${device!.name} · ${device!.ip}', style: theme.textTheme.bodySmall, overflow: TextOverflow.ellipsis, ), ], ), ), FilledButton.icon( onPressed: device == null || busy ? null : (connected ? onDisconnect : onConnect), icon: Icon(connected ? Icons.link_off : Icons.play_arrow), label: Text(connected ? '断开' : '连接'), ), ], ), if (busy) ...[ const SizedBox(height: 12), const LinearProgressIndicator(), ], const SizedBox(height: 12), Row( children: [ _StepDone(active: discovered, label: '发现'), _StepDivider(active: handshaken), _StepDone(active: handshaken, label: '握手'), _StepDivider(active: connected), _StepDone(active: connected, label: '媒体'), ], ), ], ), ), ); } } class _StepDone extends StatelessWidget { const _StepDone({required this.active, required this.label}); final bool active; final String label; @override Widget build(BuildContext context) { final theme = Theme.of(context); return Row( mainAxisSize: MainAxisSize.min, children: [ Icon(active ? Icons.task_alt : Icons.radio_button_unchecked, size: 16, color: active ? theme.colorScheme.primary : theme.colorScheme.outline), const SizedBox(width: 4), Text(label, style: theme.textTheme.labelSmall), ], ); } } class _StepDivider extends StatelessWidget { const _StepDivider({required this.active}); final bool active; @override Widget build(BuildContext context) { return Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 8), child: Divider(thickness: 2, color: active ? Theme.of(context).colorScheme.primary : null), ), ); } }