summaryrefslogtreecommitdiff
path: root/lib/scan_page.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/scan_page.dart')
-rw-r--r--lib/scan_page.dart93
1 files changed, 49 insertions, 44 deletions
diff --git a/lib/scan_page.dart b/lib/scan_page.dart
index 0ebc2e3..fc08303 100644
--- a/lib/scan_page.dart
+++ b/lib/scan_page.dart
@@ -1,3 +1,4 @@
+import 'package:badge/control/scanner_controller.dart';
import 'package:badge/device_details.dart';
import 'package:badge/device_scan_select.dart';
import 'package:badge/model/device.dart';
@@ -8,11 +9,12 @@ import 'package:permission_handler/permission_handler.dart';
var logger = Logger();
class ScanPage extends StatefulWidget {
- const ScanPage({super.key, required this.title});
+ const ScanPage({super.key, required this.title, required this.deviceScanner});
// Original doc: Fields in a Widget subclass are always marked "final".
final String title;
+ final ScannerController deviceScanner;
@override
State<ScanPage> createState() => _ScanPageState();
@@ -20,7 +22,6 @@ class ScanPage extends StatefulWidget {
class _ScanPageState extends State<ScanPage> {
List<Device> scanResults = [];
- bool isScanning = false;
Device? selectedDevice;
@@ -38,15 +39,10 @@ class _ScanPageState extends State<ScanPage> {
setState(() {
selectedDevice = null;
scanResults = [];
- isScanning = true;
});
// ...
- await Future.delayed(Duration(seconds: 5));
-
- setState(() {
- isScanning = false;
- });
+ await widget.deviceScanner.startScan();
}
Future getPermissions() async {
@@ -65,47 +61,56 @@ class _ScanPageState extends State<ScanPage> {
@override
Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Theme.of(context).colorScheme.inversePrimary,
- title: Text(widget.title),
- ),
- body: Center(
- child: Column(
- // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
- // action in the IDE, or press "p" in the console), to see the
- // wireframe for each widget.
- mainAxisAlignment: MainAxisAlignment.center,
- spacing: 24,
- children: <Widget>[
- SizedBox(height: 15),
- Row(
+ return StreamBuilder(
+ stream: widget.deviceScanner.statusStream,
+ initialData: ScanStatus.idle,
+ builder: (context, asyncSnapshot) {
+ bool isScanning = asyncSnapshot.data == ScanStatus.scanning;
+ return Scaffold(
+ appBar: AppBar(
+ backgroundColor: Theme.of(context).colorScheme.inversePrimary,
+ title: Text(widget.title),
+ ),
+ body: Center(
+ child: Column(
+ // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
+ // action in the IDE, or press "p" in the console), to see the
+ // wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
- spacing: 15.0,
- children: [
- ElevatedButton(
- onPressed: isScanning ? null : _doScan,
- child: isScanning ? Text("Scanning...") : Text("Start scan"),
+ spacing: 24,
+ children: <Widget>[
+ SizedBox(height: 15),
+ Row(
+ mainAxisAlignment: MainAxisAlignment.center,
+ spacing: 15.0,
+ children: [
+ ElevatedButton(
+ onPressed: isScanning ? null : _doScan,
+ child: isScanning
+ ? Text("Scanning...")
+ : Text("Start scan"),
+ ),
+ ElevatedButton(
+ onPressed: (selectedDevice == null || isScanning)
+ ? null
+ : _doConnect,
+ child: Text("Connect"),
+ ),
+ ],
),
- ElevatedButton(
- onPressed: (selectedDevice == null || isScanning)
- ? null
- : _doConnect,
- child: Text("Connect"),
+ Expanded(
+ child: DeviceScanSelection(
+ items: scanResults,
+ onItemSelected: (item) {
+ setState(() => selectedDevice = item);
+ },
+ ),
),
],
),
- Expanded(
- child: DeviceScanSelection(
- items: scanResults,
- onItemSelected: (item) {
- setState(() => selectedDevice = item);
- },
- ),
- ),
- ],
- ),
- ),
+ ),
+ );
+ },
);
}
}