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 _output = '';
|
||||||
String key = '';
|
String key = '';
|
||||||
|
|
||||||
Future<void> _incrementCounter() async {
|
Future<void> doSSH() async {
|
||||||
if (navIndex == 0) {
|
SSHSocket? socket;
|
||||||
SSHSocket? socket;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket = await SSHSocket.connect(
|
socket = await SSHSocket.connect(
|
||||||
'192.168.2.123',
|
'192.168.2.123',
|
||||||
22,
|
22,
|
||||||
timeout: const Duration(seconds: 0, milliseconds: 500),
|
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},
|
|
||||||
);
|
);
|
||||||
|
} 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
|
@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 bodyComponent = bodyComponentMain;
|
||||||
|
var actionButton = actionButtonMain;
|
||||||
|
|
||||||
if (navIndex == 0) {
|
if (navIndex == 0) {
|
||||||
// Main page
|
// Main page
|
||||||
bodyComponent = bodyComponentMain;
|
bodyComponent = bodyComponentMain;
|
||||||
|
actionButton = actionButtonMain;
|
||||||
} else if (navIndex == 1) {
|
} else if (navIndex == 1) {
|
||||||
// Info page
|
// Info page
|
||||||
bodyComponent = bodyComponentInfo;
|
bodyComponent = bodyComponentInfo;
|
||||||
|
actionButton = actionButtonInfo;
|
||||||
} else {
|
} else {
|
||||||
throw Exception('Unknown navIndex: $navIndex');
|
throw Exception('Unknown navIndex: $navIndex');
|
||||||
}
|
}
|
||||||
@ -159,11 +174,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(backgroundColor: Colors.amber, title: Text(widget.title)),
|
appBar: AppBar(backgroundColor: Colors.amber, title: Text(widget.title)),
|
||||||
body: bodyComponent,
|
body: bodyComponent,
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: actionButton,
|
||||||
onPressed: _incrementCounter,
|
|
||||||
tooltip: 'Increment',
|
|
||||||
child: const Icon(Icons.add),
|
|
||||||
),
|
|
||||||
drawer: Drawer(
|
drawer: Drawer(
|
||||||
child: ListView(
|
child: ListView(
|
||||||
padding: EdgeInsets.zero,
|
padding: EdgeInsets.zero,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user