// Settings. Three groups for now: appearance (device-local theme), chat (the // server's follow-up behavior) and a pet placeholder. import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import '../appearance_prefs.dart'; import '../pets/pet_library.dart'; import '../state.dart'; class SettingsScreen extends StatefulWidget { final AppState app; const SettingsScreen({super.key, required this.app}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { @override void initState() { super.initState(); // Pick up a change made on another client while this page opens. widget.app.refreshFollowUpBehavior(); PetLibrary.instance.ensureLoaded(); } @override Widget build(BuildContext context) { final look = AppearancePrefs.instance; return Scaffold( appBar: AppBar(title: const Text('设置')), body: ListenableBuilder( listenable: Listenable.merge( [widget.app, look, PetLibrary.instance]), builder: (context, _) { final theme = Theme.of(context); return ListView( padding: const EdgeInsets.only(top: 4, bottom: 32), children: [ const _SectionHeader('外观', note: '仅当前设备生效,不同步到服务器'), _appearance(theme, look), const _SectionHeader('聊天'), _chat(theme), const _SectionHeader('宠物', note: '默认不提供;宠物包只存放在本机'), _pet(theme), ], ); }, ), ); } // ------------------------------------------------------------- appearance Widget _appearance(ThemeData theme, AppearancePrefs look) { const modes = [ (ThemeMode.system, '跟随系统'), (ThemeMode.light, '浅色'), (ThemeMode.dark, '深色'), ]; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const _Label('主题模式'), Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 12), child: Wrap( spacing: 8, runSpacing: 8, children: [ for (final (mode, label) in modes) ChoiceChip( label: Text(label), selected: look.themeMode == mode, onSelected: (_) => look.setThemeMode(mode), ), ], ), ), const _Label('主题色'), Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 8), child: Wrap( spacing: 12, runSpacing: 12, children: [ for (final seed in appearanceSeeds) _swatch(theme, look, seed), ], ), ), ], ); } Widget _swatch(ThemeData theme, AppearancePrefs look, int seed) { final color = Color(seed); final selected = look.seedColor.toARGB32() == seed; return Semantics( button: true, selected: selected, label: '主题色 ${seed.toRadixString(16)}', child: InkWell( borderRadius: BorderRadius.circular(22), onTap: () => look.setSeedColor(seed), child: Container( width: 40, height: 40, decoration: BoxDecoration( color: color, shape: BoxShape.circle, border: Border.all( color: selected ? theme.colorScheme.onSurface : theme.colorScheme.outlineVariant, width: selected ? 2.5 : 1, ), ), child: selected ? const Icon(Icons.check, size: 20, color: Colors.white) : null, ), ), ); } // ------------------------------------------------------------------- chat Widget _chat(ThemeData theme) { final steer = widget.app.followUpBehavior == 'steer'; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const ListTile( title: Text('追问处理行为'), subtitle: Text('运行中按发送时,消息默认怎么处理。与网页端设置同步。'), ), Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 6), child: Wrap( spacing: 8, runSpacing: 8, children: [ ChoiceChip( label: const Text('转向'), selected: steer, onSelected: (_) => _setFollowUp('steer'), ), ChoiceChip( label: const Text('排队'), selected: !steer, onSelected: (_) => _setFollowUp('queue'), ), ], ), ), Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 8), child: Text( steer ? '转向:在下一步加入当前回合,工作继续。' : '排队:等当前回合结束后再发送。', style: TextStyle(fontSize: 12.5, color: theme.hintColor), ), ), ], ); } Future _setFollowUp(String value) async { final ok = await widget.app.setFollowUpBehavior(value); if (!ok && mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(widget.app.lastError ?? '保存失败')), ); widget.app.clearLastError(); } } // -------------------------------------------------------------------- pet Widget _pet(ThemeData theme) { final library = PetLibrary.instance; final pack = library.pack; if (pack == null) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const ListTile( title: Text('尚未导入宠物'), subtitle: Text('默认不提供宠物。导入一个宠物包(.zip)后,它会站在输入框上方。'), ), Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 8), child: FilledButton.tonalIcon( onPressed: _importPet, icon: const Icon(Icons.file_upload_outlined, size: 18), label: const Text('导入宠物包'), ), ), ], ); } final info = [pack.author, pack.license] .where((s) => s.trim().isNotEmpty) .join(' · '); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ ListTile( title: Text(pack.name.isEmpty ? pack.id : pack.name), subtitle: info.isEmpty ? null : Text(info, maxLines: 2, overflow: TextOverflow.ellipsis), ), SwitchListTile( title: const Text('启用宠物'), subtitle: const Text('站在输入框上方,默认关闭'), value: library.enabled, onChanged: (v) => library.setEnabled(v), ), const _Label('大小'), Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 8), child: Wrap( spacing: 8, children: [ for (final (size, label) in const [ (40.0, '小'), (52.0, '中'), (64.0, '大'), ]) ChoiceChip( label: Text(label), selected: (library.size - size).abs() < 0.5, onSelected: (_) => library.setSize(size), ), ], ), ), Padding( padding: const EdgeInsets.fromLTRB(20, 2, 20, 8), child: Row( children: [ FilledButton.tonalIcon( onPressed: _importPet, icon: const Icon(Icons.swap_horiz, size: 18), label: const Text('更换宠物包'), ), const SizedBox(width: 8), TextButton( onPressed: _confirmRemovePet, child: const Text('移除'), ), ], ), ), ], ); } Future _importPet() async { try { final file = await FilePicker.pickFile( dialogTitle: '选择宠物包(.zip)', type: FileType.custom, allowedExtensions: const ['zip'], ); if (file == null) return; final bytes = await file.readAsBytes(); final pack = await PetLibrary.instance.importZip(bytes); if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('已导入 ${pack.name.isEmpty ? pack.id : pack.name}')), ); } catch (e) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('导入失败:$e')), ); } } Future _confirmRemovePet() async { final ok = await showDialog( context: context, builder: (dialog) => AlertDialog( title: const Text('移除宠物?'), content: const Text('本机保存的宠物包会被删除。'), actions: [ TextButton( onPressed: () => Navigator.pop(dialog, false), child: const Text('取消')), FilledButton( onPressed: () => Navigator.pop(dialog, true), child: const Text('移除')), ], ), ); if (ok == true) await PetLibrary.instance.remove(); } } class _SectionHeader extends StatelessWidget { final String title; final String? note; const _SectionHeader(this.title, {this.note}); @override Widget build(BuildContext context) { final theme = Theme.of(context); return Padding( padding: const EdgeInsets.fromLTRB(20, 18, 20, 6), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title, style: TextStyle( fontSize: 13, fontWeight: FontWeight.w700, color: theme.colorScheme.primary, )), if (note != null) Padding( padding: const EdgeInsets.only(top: 2), child: Text(note!, style: TextStyle(fontSize: 11.5, color: theme.hintColor)), ), ], ), ); } } class _Label extends StatelessWidget { final String text; const _Label(this.text); @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.fromLTRB(20, 2, 20, 6), child: Text(text, style: const TextStyle(fontSize: 13.5, fontWeight: FontWeight.w500)), ); } }