2023-09-17 21:18:54 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:ju_rc_app/lib/serial.dart';
|
|
|
|
|
2023-09-18 19:45:59 +02:00
|
|
|
// ignore: must_be_immutable
|
2023-09-17 21:18:54 +02:00
|
|
|
class ConnectionBar extends StatefulWidget {
|
2023-09-18 19:45:59 +02:00
|
|
|
USerial serial = getSerial();
|
|
|
|
ConnectionBar({super.key});
|
2023-09-17 21:18:54 +02:00
|
|
|
|
|
|
|
@override
|
2023-09-18 19:45:59 +02:00
|
|
|
// ignore: library_private_types_in_public_api
|
|
|
|
_ConnectionBarState createState() => _ConnectionBarState();
|
2023-09-17 21:18:54 +02:00
|
|
|
}
|
|
|
|
|
2023-09-18 19:45:59 +02:00
|
|
|
class _ConnectionBarState extends State<ConnectionBar> {
|
2023-09-17 21:18:54 +02:00
|
|
|
late Timer timer;
|
|
|
|
String _status = "";
|
2023-09-18 19:45:59 +02:00
|
|
|
List<UPort> _ports = [];
|
|
|
|
StateSetter? sheetsetstate;
|
|
|
|
UPort? connectedTo;
|
2023-09-17 21:18:54 +02:00
|
|
|
|
2023-09-18 19:45:59 +02:00
|
|
|
_ConnectionBarState() {
|
2023-09-17 21:18:54 +02:00
|
|
|
timer = Timer.periodic(const Duration(seconds: 1), (_) async {
|
|
|
|
if (sheetsetstate == null) return;
|
2023-09-18 19:45:59 +02:00
|
|
|
var ports = await widget.serial.getPorts();
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
_ports = ports;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-18 19:45:59 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
timer.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-09-17 21:18:54 +02:00
|
|
|
@override
|
|
|
|
void setState(VoidCallback fn) {
|
|
|
|
super.setState(fn);
|
|
|
|
try {
|
|
|
|
if (sheetsetstate != null) (sheetsetstate!)(() {});
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void showModal() async {
|
|
|
|
if (connectedTo != null && connectedTo!.connected) {
|
2023-09-18 19:45:59 +02:00
|
|
|
await widget.serial.disconnect();
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
connectedTo = null;
|
|
|
|
});
|
|
|
|
} else {
|
2023-09-18 19:45:59 +02:00
|
|
|
var ports = await widget.serial.getPorts();
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
_ports = ports;
|
2023-09-18 19:45:59 +02:00
|
|
|
_status = "";
|
2023-09-17 21:18:54 +02:00
|
|
|
});
|
|
|
|
// ignore: use_build_context_synchronously
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => StatefulBuilder(
|
|
|
|
builder: (BuildContext context, StateSetter setStateM) {
|
|
|
|
sheetsetstate = setStateM;
|
|
|
|
return BottomSheet(
|
2023-09-18 19:45:59 +02:00
|
|
|
enableDrag: false,
|
2023-09-17 21:18:54 +02:00
|
|
|
builder: (context) => Column(children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: Column(children: [
|
|
|
|
const Text("Devices",
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 20.0,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
)),
|
|
|
|
Text(_status,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 10.0,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
))
|
|
|
|
])),
|
|
|
|
..._ports.map((port) => ListTile(
|
|
|
|
leading: const Icon(Icons.usb),
|
|
|
|
title: Text(port.productName),
|
|
|
|
subtitle: Text(port.manufacturerName),
|
|
|
|
trailing: ElevatedButton(
|
|
|
|
child:
|
|
|
|
Text(port.connected ? "Disconnect" : "Connect"),
|
|
|
|
onPressed: () async {
|
|
|
|
if (port.connected) {
|
2023-09-18 19:45:59 +02:00
|
|
|
await widget.serial.disconnect();
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
connectedTo = null;
|
|
|
|
_status = "Successfully disconnected!";
|
|
|
|
});
|
|
|
|
} else {
|
2023-09-18 19:45:59 +02:00
|
|
|
if (await widget.serial.connect(port)) {
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
connectedTo = port;
|
|
|
|
_status = "Successfully connected!";
|
|
|
|
});
|
2023-09-18 19:45:59 +02:00
|
|
|
} else {
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
_status = "Error while connecting!";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 19:45:59 +02:00
|
|
|
var ports = await widget.serial.getPorts();
|
2023-09-17 21:18:54 +02:00
|
|
|
setState(() {
|
|
|
|
_ports = ports;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
)))
|
|
|
|
]),
|
|
|
|
onClosing: () {
|
|
|
|
sheetsetstate = null;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ListTile(
|
|
|
|
leading: const Icon(Icons.usb),
|
|
|
|
title: Text(connectedTo != null && connectedTo!.connected
|
|
|
|
? connectedTo!.productName
|
|
|
|
: "Not connected"),
|
|
|
|
subtitle: Text(connectedTo != null && connectedTo!.connected
|
|
|
|
? connectedTo!.manufacturerName
|
|
|
|
: "Click 'connect'"),
|
|
|
|
trailing: ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
showModal();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Text(connectedTo != null && connectedTo!.connected
|
|
|
|
? "Disconnect"
|
|
|
|
: "Connect"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|