Skip to content

Commit 5a3d5ac

Browse files
committed
fix:管理连接页面的部分问题
1 parent ab4aff0 commit 5a3d5ac

4 files changed

Lines changed: 113 additions & 9 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# connecter
22
A lightweight SSH connection tool based on flutter
33
# todo
4-
* 编辑连接的启动按钮失效
54
* 连接名称的编辑
65
* SFTP
76
* 极端情况下的overflow

lib/manage_connections_page.dart

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import 'package:flutter/material.dart';
2+
import 'package:t_samuioto_ssh/terminal_page.dart';
23
// ignore: unused_import
34
import 'package:uuid/uuid.dart';
45
import 'models/connection_model.dart';
56
import 'services/storage_service.dart';
67
import 'quick_connect_dialog.dart';
8+
import 'services/ssh_service.dart';
9+
//import 'services/storage_service.dart';
710

811
class ManageConnectionsPage extends StatefulWidget {
912
const ManageConnectionsPage({super.key});
@@ -32,7 +35,7 @@ class _ManageConnectionsPageState extends State<ManageConnectionsPage> {
3235
void _editConnection(ConnectionInfo connection) {
3336
showDialog(
3437
context: context,
35-
builder: (context) => QuickConnectDialog(connection: connection), // 传入连接进行编辑
38+
builder: (context) => QuickConnectDialog(connection: connection),
3639
).then((_) => _loadConnections());
3740
}
3841

@@ -41,7 +44,7 @@ void _deleteConnection(ConnectionInfo connection) {
4144
context: context,
4245
builder: (context) => AlertDialog(
4346
title: const Text('删除连接'),
44-
content: Text('确定要删除连接 "${connection.name}" 吗?'),
47+
content: Text('要删除连接 "${connection.name}" 吗?'),
4548
actions: [
4649
TextButton(
4750
onPressed: () => Navigator.of(context).pop(),
@@ -65,9 +68,56 @@ void _deleteConnection(ConnectionInfo connection) {
6568
);
6669
}
6770

68-
void _connectTo(ConnectionInfo connection) {
69-
// 应该暂时不需要了
71+
void _connectTo(ConnectionInfo connection) async {
72+
try {
73+
final storageService = StorageService();
74+
final sshService = SshService();
75+
76+
// 获取对应的凭证
77+
final credentials = await storageService.getCredentials();
78+
final credential = credentials.firstWhere(
79+
(c) => c.id == connection.credentialId,
80+
orElse: () => throw Exception('找不到认证凭证'),
81+
);
82+
83+
// 显示连接中状态
84+
if (mounted) {
85+
ScaffoldMessenger.of(context).showSnackBar(
86+
const SnackBar(content: Text('正在连接...')),
87+
);
88+
}
89+
90+
await sshService.connect(connection, credential);
91+
92+
if (mounted) {
93+
Navigator.of(context).push(
94+
MaterialPageRoute(
95+
builder: (context) => TerminalPage(
96+
connection: connection,
97+
credential: credential,
98+
),
99+
),
100+
);
101+
}
102+
103+
} catch (e) {
104+
if (mounted) {
105+
showDialog(
106+
context: context,
107+
builder: (context) => AlertDialog(
108+
title: const Text('连接失败'),
109+
content: Text(e.toString()),
110+
actions: [
111+
TextButton(
112+
onPressed: () => Navigator.of(context).pop(),
113+
child: const Text('确定'),
114+
),
115+
],
116+
),
117+
);
118+
}
70119
}
120+
}
71121

72122
@override
73123
Widget build(BuildContext context) {

lib/manage_credentials_page.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ class _CredentialDialogState extends State<CredentialDialog> {
283283
ScaffoldMessenger.of(context).showSnackBar(
284284
SnackBar(
285285
content: Text(_isEditing ? '凭证已更新' : '凭证已创建'),
286-
backgroundColor: Colors.green,
287286
),
288287
);
289288
}

lib/quick_connect_dialog.dart

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class _QuickConnectDialogState extends State<QuickConnectDialog> {
6464
}
6565
}
6666

67-
Future<void> _connect() async {
67+
Future<void> _connectToServer() async {
6868
if (!_formKey.currentState!.validate()) return;
6969
if (_selectedCredential == null) {
7070
ScaffoldMessenger.of(context).showSnackBar(
@@ -118,6 +118,56 @@ class _QuickConnectDialogState extends State<QuickConnectDialog> {
118118
}
119119
}
120120

121+
// 新增:仅更新连接信息而不连接
122+
Future<void> _updateConnection() async {
123+
if (!_formKey.currentState!.validate()) return;
124+
if (_selectedCredential == null) {
125+
ScaffoldMessenger.of(context).showSnackBar(
126+
const SnackBar(content: Text('请选择认证凭证')),
127+
);
128+
return;
129+
}
130+
131+
setState(() {
132+
_isConnecting = true;
133+
});
134+
135+
try {
136+
final connection = ConnectionInfo(
137+
id: widget.connection!.id, // 使用原有的ID
138+
name: '${_hostController.text}:${_portController.text}',
139+
host: _hostController.text,
140+
port: int.parse(_portController.text),
141+
credentialId: _selectedCredential!.id,
142+
type: _selectedType,
143+
remember: true, // 编辑模式下总是记住连接
144+
);
145+
146+
// 仅保存连接信息,不进行SSH连接
147+
await _storageService.saveConnection(connection);
148+
149+
if (mounted) {
150+
Navigator.of(context).pop();
151+
// 不跳转到终端页面
152+
ScaffoldMessenger.of(context).showSnackBar(
153+
const SnackBar(content: Text('连接信息已更新')),
154+
);
155+
}
156+
} catch (e) {
157+
if (mounted) {
158+
ScaffoldMessenger.of(context).showSnackBar(
159+
SnackBar(content: Text('更新失败: $e')),
160+
);
161+
}
162+
} finally {
163+
if (mounted) {
164+
setState(() {
165+
_isConnecting = false;
166+
});
167+
}
168+
}
169+
}
170+
121171
void _showConnectionError(String error) {
122172
showDialog(
123173
context: context,
@@ -132,7 +182,7 @@ class _QuickConnectDialogState extends State<QuickConnectDialog> {
132182
TextButton(
133183
onPressed: () {
134184
Navigator.of(context).pop();
135-
_connect();
185+
_connectToServer();
136186
},
137187
child: const Text('重试'),
138188
),
@@ -278,7 +328,13 @@ class _QuickConnectDialogState extends State<QuickConnectDialog> {
278328
child: const Text('取消'),
279329
),
280330
ElevatedButton(
281-
onPressed: _isConnecting ? null : _connect,
331+
onPressed: _isConnecting ? null : () {
332+
if (_isEditing) {
333+
_updateConnection();
334+
} else {
335+
_connectToServer();
336+
}
337+
},
282338
child: _isConnecting
283339
? const SizedBox(
284340
width: 16,

0 commit comments

Comments
 (0)