From 7c0f47a946939875b13250b204898911fc525827 Mon Sep 17 00:00:00 2001 From: uvok Date: Tue, 29 Jul 2025 14:12:32 +0200 Subject: Rename classes --- lib/device.dart | 103 ------------------------------------------------ lib/device_details.dart | 93 +++++++++++++++++++++++++++++++++++++++++++ lib/main.dart | 6 ++- 3 files changed, 97 insertions(+), 105 deletions(-) delete mode 100644 lib/device.dart create mode 100644 lib/device_details.dart diff --git a/lib/device.dart b/lib/device.dart deleted file mode 100644 index 6442ee8..0000000 --- a/lib/device.dart +++ /dev/null @@ -1,103 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/material.dart'; -import 'package:flutter_blue_plus/flutter_blue_plus.dart'; -import 'package:logger/logger.dart'; - -var logger = Logger(printer: PrettyPrinter()); - -// Is this even needed? I have no idea... -class Device extends StatelessWidget { - const Device({super.key}); - - @override - Widget build(BuildContext context) { - return MaterialApp(); - } -} - -class DeviceScreen extends StatefulWidget { - final BluetoothDevice btDevice; - - const DeviceScreen({super.key, required this.btDevice}); - - @override - State createState() { - return DeviceState(); - } -} - -class DeviceState extends State { - String connectStatus = ""; - // ??? - StreamSubscription subs = - Stream.empty().listen((e) => ()); - bool backActive = false; - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: Text("Device details")), - body: Center( - child: Column( - spacing: 20, - children: [ - Text(connectStatus), - ElevatedButton( - onPressed: backActive ? backClick : null, - child: Text("Back"), - ), - ], - ), - ), - ); - } - - @override - void initState() { - super.initState(); - _doConnect(); - } - - void backClick() { - Navigator.pop(context); - } - - void onConnStateChange(BluetoothConnectionState event) { - setState(() { - connectStatus = event.toString(); - }); - logger.i("New conn state: ${event.toString()}"); - } - - @override - void deactivate() { - super.deactivate(); - logger.i("Closing state"); - subs.cancel().ignore(); - widget.btDevice.disconnect().ignore(); - } - - void _doConnect() async { - final dev = widget.btDevice; - subs.cancel().ignore(); - subs = dev.connectionState.listen(onConnStateChange); - try { - logger.i("Try to connect..."); - // connect timeout doesn't work under Linux - await dev.connect().timeout(Duration(seconds: 2)); - logger.i("Connected!"); - - connectStatus = "Connected"; - } catch (e) { - logger.e(e); - dev.disconnect().ignore(); - connectStatus = e.toString(); - } finally { - backActive = true; - if (mounted) { - setState(() {}); - } - } - } -} diff --git a/lib/device_details.dart b/lib/device_details.dart new file mode 100644 index 0000000..4f96ed8 --- /dev/null +++ b/lib/device_details.dart @@ -0,0 +1,93 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_blue_plus/flutter_blue_plus.dart'; +import 'package:logger/logger.dart'; + +var logger = Logger(printer: PrettyPrinter()); + +class DeviceDetailsScreen extends StatefulWidget { + final BluetoothDevice btDevice; + + const DeviceDetailsScreen({super.key, required this.btDevice}); + + @override + State createState() { + return DeviceDetailsState(); + } +} + +class DeviceDetailsState extends State { + String connectStatus = ""; + // ??? + StreamSubscription subs = + Stream.empty().listen((e) => ()); + bool backActive = false; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: Text("Device details")), + body: Center( + child: Column( + spacing: 20, + children: [ + Text(connectStatus), + ElevatedButton( + onPressed: backActive ? backClick : null, + child: Text("Back"), + ), + ], + ), + ), + ); + } + + @override + void initState() { + super.initState(); + _doConnect(); + } + + void backClick() { + Navigator.pop(context); + } + + void onConnStateChange(BluetoothConnectionState event) { + setState(() { + connectStatus = event.toString(); + }); + logger.i("New conn state: ${event.toString()}"); + } + + @override + void deactivate() { + super.deactivate(); + logger.i("Closing state"); + subs.cancel().ignore(); + widget.btDevice.disconnect().ignore(); + } + + void _doConnect() async { + final dev = widget.btDevice; + subs.cancel().ignore(); + subs = dev.connectionState.listen(onConnStateChange); + try { + logger.i("Try to connect..."); + // connect timeout doesn't work under Linux + await dev.connect().timeout(Duration(seconds: 2)); + logger.i("Connected!"); + + connectStatus = "Connected"; + } catch (e) { + logger.e(e); + dev.disconnect().ignore(); + connectStatus = e.toString(); + } finally { + backActive = true; + if (mounted) { + setState(() {}); + } + } + } +} diff --git a/lib/main.dart b/lib/main.dart index b503674..a04247b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,6 @@ import 'dart:io' show Platform; -import 'package:badge/device.dart'; +import 'package:badge/device_details.dart'; import 'package:badge/device_scan_select.dart'; import 'package:flutter/material.dart'; @@ -75,7 +75,9 @@ class _MyHomePageState extends State { //??? Navigator.push( context, - MaterialPageRoute(builder: (context) => DeviceScreen(btDevice: dev)), + MaterialPageRoute( + builder: (context) => DeviceDetailsScreen(btDevice: dev), + ), ); } -- cgit v1.2.3