import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../core/localization/app_localizations.dart'; import '../../../core/theme/app_theme.dart'; import '../../../shared/widgets/status_indicator.dart'; import '../../device/providers/device_info_provider.dart'; /// 状态栏组件 - 明亮工业风格 /// 显示设备名称、实时时钟、系统状态、照明控制 class StatusBar extends ConsumerStatefulWidget { final bool isRunning; final VoidCallback? onLightToggle; const StatusBar({ super.key, this.isRunning = false, this.onLightToggle, }); @override ConsumerState createState() => _StatusBarState(); } class _StatusBarState extends ConsumerState { String _currentTime = ''; Timer? _timer; @override void initState() { super.initState(); _updateTime(); _timer = Timer.periodic(const Duration(seconds: 1), (_) => _updateTime()); } @override void dispose() { _timer?.cancel(); super.dispose(); } void _updateTime() { final now = DateTime.now(); _currentTime = '${now.year}-${_twoDigits(now.month)}-${_twoDigits(now.day)} ' '${_twoDigits(now.hour)}:${_twoDigits(now.minute)}:${_twoDigits(now.second)}'; if (mounted) setState(() {}); } String _twoDigits(int n) => n.toString().padLeft(2, '0'); Future _onLightTap() async { widget.onLightToggle?.call(); await ref.read(deviceInfoProvider.notifier).toggleLight(); } @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context); final deviceInfo = ref.watch(deviceInfoProvider); return Container( height: 56, padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8), decoration: BoxDecoration( color: AppTheme.primaryColor, boxShadow: [ BoxShadow( color: Colors.black.withValues(alpha: 0.08), blurRadius: 4, offset: const Offset(0, 2), ), ], ), child: Row( children: [ Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.precision_manufacturing, color: Colors.white, size: 22), const SizedBox(width: 10), Text( l10n?.deviceName ?? '污水毒品前处理一体机', style: const TextStyle( color: Colors.white, fontSize: 17, fontWeight: FontWeight.w700, ), ), ], ), const Spacer(), _LightToggleButton( isOn: deviceInfo.lightingOn, onTap: _onLightTap, ), const SizedBox(width: 20), StatusIndicator( text: widget.isRunning ? (l10n?.running ?? '运行中') : (l10n?.idle ?? '未运行'), status: widget.isRunning ? DeviceStatusType.running : DeviceStatusType.idle, ), const SizedBox(width: 20), Text( _currentTime, style: const TextStyle( color: Colors.white, fontSize: 13, fontFamily: 'monospace', fontWeight: FontWeight.normal, ), ), ], ), ); } } class _LightToggleButton extends StatelessWidget { final bool isOn; final VoidCallback? onTap; const _LightToggleButton({this.isOn = false, this.onTap}); @override Widget build(BuildContext context) { return Material( color: Colors.transparent, child: InkWell( onTap: onTap, borderRadius: BorderRadius.circular(20), child: Container( width: 40, height: 40, decoration: BoxDecoration( color: isOn ? Colors.white.withValues(alpha: 0.25) : Colors.white.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(20), border: Border.all( color: Colors.white.withValues(alpha: 0.3), width: 1, ), ), child: Icon( isOn ? Icons.lightbulb : Icons.lightbulb_outline_rounded, color: isOn ? Colors.yellowAccent : Colors.white.withValues(alpha: 0.8), size: 20, ), ), ), ); } }