fix: a new server could not be given its password

The form only rendered the password field when editing a saved server
(!_editNew). Adding a password-protected instance therefore failed with
"需要密码或已保存的凭据" and offered nowhere to type it. The field is now
always shown ("密码(如果需要)", empty for a passwordless server); a failed
connect that asks for a password focuses it, and a rejected password reads
as "密码错误,请重试".

Adds test/servers_form_test.dart. 0.3.18 (318).
This commit is contained in:
blumia committed 2026-11-22 11:38:24 +08:00
1 parent e646130d1f
commit d9d5424318
6 files changed
+70 -13

No files matched your search

+31 -10
View File
@@ -28,6 +28,9 @@ class _ServersScreenState extends State<ServersScreen> {
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<ServersScreen> {
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<ServersScreen> {
_password = '';
_formError = widget.app.lastError;
});
_focusPasswordIfNeeded(_formError);
widget.app.clearLastError();
} else if (mounted) {
setState(() => _connecting = false);
@@ -236,16 +246,18 @@ class _ServersScreenState extends State<ServersScreen> {
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<ServersScreen> {
_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,