Restructure Action Button Logic
This commit is contained in:
parent
df687a52a8
commit
bae5cc903c
117
lib/main.dart
117
lib/main.dart
@ -68,57 +68,57 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
String _output = '';
|
||||
String key = '';
|
||||
|
||||
Future<void> _incrementCounter() async {
|
||||
if (navIndex == 0) {
|
||||
SSHSocket? socket;
|
||||
Future<void> doSSH() async {
|
||||
SSHSocket? socket;
|
||||
|
||||
try {
|
||||
socket = await SSHSocket.connect(
|
||||
'192.168.2.123',
|
||||
22,
|
||||
timeout: const Duration(seconds: 0, milliseconds: 500),
|
||||
);
|
||||
} on SocketException {
|
||||
setState(() {
|
||||
_output = "SocketException: Could not connect to the server.";
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final client = SSHClient(
|
||||
socket,
|
||||
username: 'test',
|
||||
//onPasswordRequest: () => '123456',
|
||||
identities: List.of(keyPair != null ? [keyPair!] : []),
|
||||
);
|
||||
|
||||
final uptime = await client.run('uptime');
|
||||
setState(() {
|
||||
_output = utf8.decode(uptime);
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_output = "Error: $e";
|
||||
});
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Generate a key pair
|
||||
|
||||
final generatedKeyPair =
|
||||
await generateKeyPair(); // local variable avoids downcast to ? ptr
|
||||
keyPair = generatedKeyPair;
|
||||
|
||||
setState(() {
|
||||
key = encodePublicKey(generatedKeyPair, comment: "leinelab-app-key");
|
||||
});
|
||||
|
||||
await SystemChannels.platform.invokeMethod<void>(
|
||||
'Clipboard.setData',
|
||||
<String, dynamic>{'text': key},
|
||||
try {
|
||||
socket = await SSHSocket.connect(
|
||||
'192.168.2.123',
|
||||
22,
|
||||
timeout: const Duration(seconds: 0, milliseconds: 500),
|
||||
);
|
||||
} on SocketException {
|
||||
setState(() {
|
||||
_output = "SocketException: Could not connect to the server.";
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final client = SSHClient(
|
||||
socket,
|
||||
username: 'test',
|
||||
//onPasswordRequest: () => '123456',
|
||||
identities: List.of(keyPair != null ? [keyPair!] : []),
|
||||
);
|
||||
|
||||
final uptime = await client.run('uptime');
|
||||
setState(() {
|
||||
_output = utf8.decode(uptime);
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_output = "Error: $e";
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> generateKey() async {
|
||||
// Generate a key pair
|
||||
|
||||
final generatedKeyPair =
|
||||
await generateKeyPair(); // local variable avoids downcast to ? ptr
|
||||
keyPair = generatedKeyPair;
|
||||
|
||||
setState(() {
|
||||
key = encodePublicKey(generatedKeyPair, comment: "leinelab-app-key");
|
||||
});
|
||||
|
||||
await SystemChannels.platform.invokeMethod<void>(
|
||||
'Clipboard.setData',
|
||||
<String, dynamic>{'text': key},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@ -144,14 +144,29 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
),
|
||||
);
|
||||
|
||||
final actionButtonMain = FloatingActionButton(
|
||||
onPressed: doSSH,
|
||||
tooltip: 'Do SSH',
|
||||
child: const Icon(Icons.computer),
|
||||
);
|
||||
|
||||
final actionButtonInfo = FloatingActionButton(
|
||||
onPressed: generateKey,
|
||||
tooltip: 'Regenerate Key',
|
||||
child: const Icon(Icons.refresh),
|
||||
);
|
||||
|
||||
var bodyComponent = bodyComponentMain;
|
||||
var actionButton = actionButtonMain;
|
||||
|
||||
if (navIndex == 0) {
|
||||
// Main page
|
||||
bodyComponent = bodyComponentMain;
|
||||
actionButton = actionButtonMain;
|
||||
} else if (navIndex == 1) {
|
||||
// Info page
|
||||
bodyComponent = bodyComponentInfo;
|
||||
actionButton = actionButtonInfo;
|
||||
} else {
|
||||
throw Exception('Unknown navIndex: $navIndex');
|
||||
}
|
||||
@ -159,11 +174,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
return Scaffold(
|
||||
appBar: AppBar(backgroundColor: Colors.amber, title: Text(widget.title)),
|
||||
body: bodyComponent,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _incrementCounter,
|
||||
tooltip: 'Increment',
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
floatingActionButton: actionButton,
|
||||
drawer: Drawer(
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
|
Loading…
x
Reference in New Issue
Block a user