1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
import 'package:badge/device_details.dart';
import 'package:badge/device_scan_select.dart';
import 'package:badge/model/device.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:permission_handler/permission_handler.dart';
var logger = Logger();
class ScanPage extends StatefulWidget {
const ScanPage({super.key, required this.title});
// Original doc: Fields in a Widget subclass are always marked "final".
final String title;
@override
State<ScanPage> createState() => _ScanPageState();
}
class _ScanPageState extends State<ScanPage> {
List<Device> scanResults = [];
bool isScanning = false;
Device? selectedDevice;
void _doConnect() async {
Device? dev = selectedDevice;
if (dev == null) return;
//???
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DeviceDetailsScreen(device: dev)),
);
}
void _doScan() async {
setState(() {
selectedDevice = null;
scanResults = [];
isScanning = true;
});
// ...
await Future.delayed(Duration(seconds: 5));
setState(() {
isScanning = false;
});
}
Future getPermissions() async {
try {
await Permission.bluetooth.request();
} catch (e) {
logger.e(e.toString());
}
}
@override
void initState() {
super.initState();
getPermissions();
}
@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(
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"),
),
],
),
Expanded(
child: DeviceScanSelection(
items: scanResults,
onItemSelected: (item) {
setState(() => selectedDevice = item);
},
),
),
],
),
),
);
}
}
|