diff --git a/PLAN.md b/PLAN.md index 5a7acbb..69c9a28 100644 --- a/PLAN.md +++ b/PLAN.md @@ -5,6 +5,15 @@ --- +## 0.3.18 需要密码的服务器无法输入密码(分支 `flutter`) + +| 项 | 结果 | +| --- | --- | +| 问题 | 「添加服务器」表单的密码框只在编辑已保存服务器时显示(`!_editNew`)。新服务器首次连接被 401 拒绝后只提示「需要密码或已保存的凭据」,却没有输入密码的地方 | +| 修复 | 密码框始终显示,文案为「密码(如果需要)」,提示服务器未设置可留空;连接失败且错误提示需要密码时自动聚焦密码框;服务端返回 `Invalid credentials` 时显示为「密码错误,请重试」。新增 `test/servers_form_test.dart` 覆盖新服务器表单含密码框 | +| 版本 | **0.3.18(code 318)** | +--- + ## 0.3.17 设置页、侧栏底部服务器栏与追问处理行为(分支 `flutter`) | 项 | 结果 | diff --git a/README.md b/README.md index 2e10ca5..1f3bb90 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Android first. This branch (`flutter`) is a framework evaluation: the same feature set as the earlier Asuka/Go and MyGo attempts, reimplemented in Flutter to compare mobile UI maturity. -Status: 0.3.17 — servers (passwordless / password login, HTTP/HTTPS and +Status: 0.3.18 — servers (passwordless / password login, HTTP/HTTPS and SOCKS5/socks5h proxy, self-signed certificates and pinning), session list with search and live events, chat with streaming Markdown and full history paging, tool steps grouped per turn (icon, one-line summary, foldable run) with image previews, a diff --git a/lib/screens/servers_screen.dart b/lib/screens/servers_screen.dart index 25abbde..494faff 100644 --- a/lib/screens/servers_screen.dart +++ b/lib/screens/servers_screen.dart @@ -28,6 +28,9 @@ class _ServersScreenState extends State { String _password = ''; bool _connecting = false; String? _formError; + // A server may ask for a password only after the first attempt; the field + // is always there, but this one takes the focus when it is what is missing. + final FocusNode _passwordFocus = FocusNode(); @override void initState() { @@ -35,6 +38,12 @@ class _ServersScreenState extends State { if (widget.startEditing) _editing = true; } + @override + void dispose() { + _passwordFocus.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return ListenableBuilder( @@ -159,6 +168,7 @@ class _ServersScreenState extends State { _password = ''; _formError = widget.app.lastError; }); + _focusPasswordIfNeeded(_formError); widget.app.clearLastError(); } else if (mounted) { setState(() => _connecting = false); @@ -236,16 +246,18 @@ class _ServersScreenState extends State { 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()), - ), - ], + // Shown for new servers too: a password-protected instance rejects + // the first attempt, and this is where the password goes. + _labelText('密码(如果需要)'), + TextField( + controller: TextEditingController(text: _password), + focusNode: _passwordFocus, + onChanged: (v) => _password = v, + obscureText: true, + decoration: const InputDecoration( + hintText: '服务器密码;服务器未设置可留空', + border: OutlineInputBorder()), + ), if (_formError != null) Padding( padding: const EdgeInsets.only(top: 8), @@ -296,11 +308,20 @@ class _ServersScreenState extends State { _connecting = false; _formError = widget.app.lastError; }); + _focusPasswordIfNeeded(_formError); widget.app.clearLastError(); } } } + /// The server asked for a password: put the caret where it is typed. + void _focusPasswordIfNeeded(String? message) { + if (message == null || !message.contains('需要密码')) return; + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) _passwordFocus.requestFocus(); + }); + } + void _confirmDelete(Connection c) { showDialog( context: context, diff --git a/lib/state.dart b/lib/state.dart index 132d658..71efa1f 100644 --- a/lib/state.dart +++ b/lib/state.dart @@ -239,7 +239,13 @@ class AppState extends ChangeNotifier { /// A short, actionable message for a failed connect. String friendlyConnectError(Object e) { - if (e is ApiError) return e.message; + if (e is ApiError) { + if (e.status == 401 && + e.message.toLowerCase().contains('invalid credentials')) { + return '密码错误,请重试'; + } + return e.message; + } if (e is ArgumentError) return e.message?.toString() ?? e.toString(); if (e is TimeoutException) return '连接超时:检查服务器与代理地址'; final s = e.toString(); diff --git a/pubspec.yaml b/pubspec.yaml index 70a3a92..89e5e1e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -2,7 +2,7 @@ name: openchamber description: "A lightweight OpenChamber client for phones." publish_to: 'none' -version: 0.3.17+317 +version: 0.3.18+318 environment: sdk: ^3.14.0 diff --git a/test/servers_form_test.dart b/test/servers_form_test.dart new file mode 100644 index 0000000..5801a43 --- /dev/null +++ b/test/servers_form_test.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:openchamber/screens/servers_screen.dart'; +import 'package:openchamber/state.dart'; + +void main() { + testWidgets('a new server form offers the password field', (tester) async { + final app = AppState(); + addTearDown(app.dispose); + + await tester.pumpWidget(MaterialApp( + home: ServersScreen(app: app, startEditing: true), + )); + + expect(find.text('添加服务器'), findsOneWidget); + // New servers need it too: a password-protected instance rejects the + // first attempt, and there must be somewhere to type the password. + expect(find.text('密码(如果需要)'), findsOneWidget); + expect(find.text('服务器密码;服务器未设置可留空'), findsOneWidget); + }); +}