import 'package:flutter/material.dart'; import '../state.dart'; import '../storage.dart'; /// The first screen: the saved servers, or the connect form. class ServersScreen extends StatefulWidget { final AppState app; const ServersScreen({super.key, required this.app}); @override State createState() => _ServersScreenState(); } class _ServersScreenState extends State { bool _editing = false; bool _editNew = true; Connection? _edit; String _label = ''; String _url = ''; String _proxy = ''; bool _insecure = false; String _fingerprint = ''; String _password = ''; bool _connecting = false; String? _formError; @override Widget build(BuildContext context) { return ListenableBuilder( listenable: widget.app, builder: (context, _) { if (_editing) return _form(); return _list(); }, ); } Widget _list() { final theme = Theme.of(context); final conns = widget.app.connections; return Scaffold( body: SafeArea( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.fromLTRB(20, 20, 20, 8), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('OpenChamber', style: theme.textTheme.headlineMedium ?.copyWith(fontWeight: FontWeight.bold)), const SizedBox(height: 4), Text('连接你的 OpenChamber 服务器', style: theme.textTheme.bodyMedium ?.copyWith(color: theme.hintColor)), ], ), ), if (widget.app.error != null) Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), child: Text(widget.app.error!, style: TextStyle(color: theme.colorScheme.error, fontSize: 13)), ), Expanded( child: ListView( padding: const EdgeInsets.all(16), children: [ for (final c in conns) _serverCard(c), const SizedBox(height: 12), OutlinedButton.icon( onPressed: () => _startEdit(null), icon: const Icon(Icons.add), label: const Text('添加服务器'), ), ], ), ), ], ), ), ); } Widget _serverCard(Connection c) { final theme = Theme.of(context); final connected = widget.app.conn?.id == c.id; return Card( margin: const EdgeInsets.only(bottom: 10), child: ListTile( title: Text(c.label, style: const TextStyle(fontWeight: FontWeight.w600)), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(c.url, maxLines: 1, overflow: TextOverflow.ellipsis), if (c.proxy.isNotEmpty) Text('经由 ${c.proxy}', maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 12, color: theme.hintColor)), ], ), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ if (connected) Text('已连接', style: TextStyle(color: theme.colorScheme.primary)) else TextButton(onPressed: () => _connect(c), child: const Text('连接')), PopupMenuButton( onSelected: (v) { if (v == 'edit') { _startEdit(c); } else if (v == 'delete') { _confirmDelete(c); } }, itemBuilder: (_) => const [ PopupMenuItem(value: 'edit', child: Text('编辑')), PopupMenuItem(value: 'delete', child: Text('删除')), ], ), ], ), ), ); } void _connect(Connection c) async { setState(() { _connecting = true; _editNew = false; _edit = c; }); await widget.app.connect(c); if (mounted && widget.app.client == null && widget.app.lastError != null) { setState(() { _connecting = false; _editing = true; _edit = c; _label = c.label; _url = c.url; _proxy = c.proxy; _insecure = c.insecure; _fingerprint = c.fingerprint; _password = ''; _formError = widget.app.lastError; }); widget.app.clearLastError(); } else if (mounted) { setState(() => _connecting = false); } } void _startEdit(Connection? c) { widget.app.error = null; widget.app.clearLastError(); setState(() { _editing = true; _editNew = c == null; _edit = c; _label = c?.label ?? ''; _url = c?.url ?? ''; _proxy = c?.proxy ?? ''; _insecure = c?.insecure ?? false; _fingerprint = c?.fingerprint ?? ''; _password = ''; _formError = null; }); } Widget _form() { final theme = Theme.of(context); return Scaffold( appBar: AppBar( leading: IconButton( icon: const Icon(Icons.arrow_back), onPressed: () { widget.app.error = null; widget.app.clearLastError(); setState(() => _editing = false); }, ), title: Text(_editNew ? '添加服务器' : '连接 ${_edit!.label}'), ), body: ListView( padding: const EdgeInsets.all(16), children: [ _labelText('名称'), TextField( controller: TextEditingController(text: _label), onChanged: (v) => _label = v, decoration: const InputDecoration(hintText: '如 我的电脑', border: OutlineInputBorder()), ), const SizedBox(height: 12), _labelText('服务器地址'), TextField( controller: TextEditingController(text: _url), onChanged: (v) => _url = v, keyboardType: TextInputType.url, decoration: const InputDecoration( hintText: 'http://192.168.1.10:3000', border: OutlineInputBorder()), ), const SizedBox(height: 12), _labelText('代理(可选)'), TextField( controller: TextEditingController(text: _proxy), onChanged: (v) => _proxy = v, keyboardType: TextInputType.url, decoration: const InputDecoration( hintText: 'http://… 或 socks5://…', border: OutlineInputBorder()), ), const SizedBox(height: 12), _labelText('自签证书指纹(可选)'), TextField( controller: TextEditingController(text: _fingerprint), onChanged: (v) => _fingerprint = v, decoration: const InputDecoration(hintText: 'SHA-256 十六进制', border: OutlineInputBorder()), ), SwitchListTile( contentPadding: EdgeInsets.zero, title: const Text('信任自签名证书'), value: _insecure, onChanged: (v) => setState(() => _insecure = v), ), if (!_editNew) ...[ _labelText('密码'), TextField( controller: TextEditingController(text: _password), onChanged: (v) => _password = v, obscureText: true, decoration: const InputDecoration( hintText: '服务器密码', border: OutlineInputBorder()), ), ], if (_formError != null) Padding( padding: const EdgeInsets.only(top: 8), child: Text(_formError!, style: TextStyle(color: theme.colorScheme.error)), ), const SizedBox(height: 16), FilledButton( onPressed: _connecting ? null : _submit, child: _connecting ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2)) : const Text('连接'), ), ], ), ); } Widget _labelText(String s) => Padding( padding: const EdgeInsets.only(bottom: 4), child: Text(s, style: Theme.of(context).textTheme.bodySmall), ); Future _submit() async { setState(() { _connecting = true; _formError = null; }); final c = Connection( id: _edit?.id ?? _newId(), label: _label.isEmpty ? _hostOf(_url) : _label, url: _url, proxy: _proxy, insecure: _insecure, fingerprint: _fingerprint, ); // Only a connection that works is saved (AppState saves it on success), // so failed attempts do not pile up in the server list. await widget.app.connect(c, password: _password); if (mounted) { if (widget.app.client != null) { setState(() => _editing = false); } else { setState(() { _connecting = false; _formError = widget.app.lastError; }); widget.app.clearLastError(); } } } void _confirmDelete(Connection c) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('删除服务器?'), content: Text('“${c.label}”将从列表移除,设备令牌也会删除。'), actions: [ TextButton(onPressed: () => Navigator.pop(context), child: const Text('取消')), FilledButton( onPressed: () { Navigator.pop(context); widget.app.removeConnection(c); }, child: const Text('删除'), ), ], ), ); } static String _newId() => 'srv_${DateTime.now().microsecondsSinceEpoch.toRadixString(36)}'; static String _hostOf(String raw) { try { return Uri.parse(raw).host; } catch (_) { return raw; } } }