Files
kuaishai2/lib/features/settings/widgets/usb_import_panel.dart
Developer 67e2c7c76c refactor(settings): 语言/密码/U盘导入改用右侧内嵌面板
- 新增 LanguagePanel / PasswordPanel / UsbImportPanel 三个 widget
- settings_page 移除 AlertDialog 弹窗逻辑
- 5 个菜单项统一走 _buildContent() switch 切换右侧内容
- 验证:flutter analyze 无新增 issue
2026-06-04 15:10:03 +08:00

190 lines
6.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/theme/app_theme.dart';
import '../../../shared/services/toast_service.dart';
import '../services/usb_detection_service.dart';
/// U盘导入面板
///
/// 在设置页右侧主区域渲染 U盘检测与导入操作。
class UsbImportPanel extends ConsumerStatefulWidget {
const UsbImportPanel({super.key});
@override
ConsumerState<UsbImportPanel> createState() => _UsbImportPanelState();
}
class _UsbImportPanelState extends ConsumerState<UsbImportPanel> {
bool _detecting = false;
Future<void> _detect() async {
if (_detecting) return;
setState(() => _detecting = true);
final connected = await UsbDetectionService.instance.detectUsb();
if (!mounted) return;
setState(() => _detecting = false);
if (connected) {
ToastService.showInfo(context, '正在检测U盘...');
} else {
ToastService.showWarning(context, '未检测到U盘');
}
}
@override
Widget build(BuildContext context) {
final state = ref.watch(_usbStateProvider);
final connected = state.maybeWhen(
data: (s) => s.isConnected,
orElse: () => UsbDetectionService.instance.isConnected,
);
final path = state.maybeWhen(
data: (s) => s.path,
orElse: () => UsbDetectionService.instance.usbPath,
);
return ListView(
padding: EdgeInsets.zero,
children: [
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: AppTheme.borderSubtle),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Icon(
connected ? Icons.usb : Icons.usb_off,
color:
connected ? AppTheme.primaryColor : AppTheme.warningColor,
size: 28,
),
const SizedBox(width: 12),
Expanded(
child: Text(
connected ? 'U盘已连接' : '未检测到U盘',
style: TextStyle(
color: AppTheme.textPrimary,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),
const SizedBox(height: 12),
Text(
connected
? '挂载路径: ${path ?? "未知"}'
: '请插入U盘后点击"重新检测"',
style: TextStyle(
color: AppTheme.textSecondary, fontSize: 13),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
OutlinedButton.icon(
onPressed: _detecting ? null : _detect,
icon: _detecting
? const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.refresh, size: 18),
label: const Text('重新检测'),
),
const SizedBox(width: 12),
ElevatedButton.icon(
onPressed: connected ? () => _import(path) : null,
icon: const Icon(Icons.download, size: 18),
label: const Text('导入程序'),
),
],
),
],
),
),
const SizedBox(height: 16),
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(color: AppTheme.borderSubtle),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'使用说明',
style: TextStyle(
color: AppTheme.textPrimary,
fontSize: 14,
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 8),
_bullet('将程序文件 (.json) 放入 U盘根目录的 programs 文件夹'),
_bullet('插入 U盘后点击"重新检测"'),
_bullet('检测成功后点击"导入程序"加载程序列表'),
],
),
),
],
);
}
void _import(String? path) {
if (path == null) {
ToastService.showWarning(context, 'U盘路径无效');
return;
}
ToastService.showInfo(context, '正在导入程序...');
// TODO: 接入 program_import_service 实现真正的导入流程
}
Widget _bullet(String text) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(top: 6, right: 8),
child: Container(
width: 4,
height: 4,
decoration: BoxDecoration(
color: AppTheme.primaryColor,
shape: BoxShape.circle,
),
),
),
Expanded(
child: Text(
text,
style: TextStyle(
color: AppTheme.textSecondary,
fontSize: 13,
height: 1.5,
),
),
),
],
),
);
}
}
/// 监听 USB 状态变化的 Riverpod StreamProvider
final _usbStateProvider = StreamProvider<UsbState>((ref) {
return UsbDetectionService.instance.stateStream;
});