feat: implement basic BLE provision sheet widget
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
import '../../data/sources/ble/ble_provisioner.dart';
|
import '../../data/sources/ble/ble_provisioner.dart';
|
||||||
|
|
||||||
|
enum _Step { provision, done }
|
||||||
|
|
||||||
// Shared bottom sheet used by onboarding and the main sensor screens.
|
// Shared bottom sheet used by onboarding and the main sensor screens.
|
||||||
// Flow: scan → select device → enter WiFi credentials → provision → place on map.
|
// Flow: scan → select device → enter WiFi credentials → provision → place on map.
|
||||||
class BleProvisionSheet extends ConsumerStatefulWidget {
|
class BleProvisionSheet extends ConsumerStatefulWidget {
|
||||||
@@ -17,6 +20,7 @@ class _BleProvisionSheetState extends ConsumerState<BleProvisionSheet> {
|
|||||||
final _ssidController = TextEditingController();
|
final _ssidController = TextEditingController();
|
||||||
final _wifiPasswordController = TextEditingController();
|
final _wifiPasswordController = TextEditingController();
|
||||||
|
|
||||||
|
_Step _step = _Step.provision;
|
||||||
BleScanResult? _selected;
|
BleScanResult? _selected;
|
||||||
bool _provisioning = false;
|
bool _provisioning = false;
|
||||||
String? _error;
|
String? _error;
|
||||||
@@ -41,15 +45,24 @@ class _BleProvisionSheetState extends ConsumerState<BleProvisionSheet> {
|
|||||||
ssid: _ssidController.text.trim(),
|
ssid: _ssidController.text.trim(),
|
||||||
wifiPassword: _wifiPasswordController.text,
|
wifiPassword: _wifiPasswordController.text,
|
||||||
);
|
);
|
||||||
// TODO: poll localiserd until sensor appears, then prompt placement on map.
|
if (mounted) setState(() => _step = _Step.done);
|
||||||
if (mounted) Navigator.of(context).pop();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setState(() => _error = e.toString());
|
if (mounted) setState(() => _error = e.toString());
|
||||||
} finally {
|
} finally {
|
||||||
if (mounted) setState(() => _provisioning = false);
|
if (mounted) setState(() => _provisioning = false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _reset() {
|
||||||
|
setState(() {
|
||||||
|
_step = _Step.provision;
|
||||||
|
_selected = null;
|
||||||
|
_error = null;
|
||||||
|
_ssidController.clear();
|
||||||
|
_wifiPasswordController.clear();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return DraggableScrollableSheet(
|
return DraggableScrollableSheet(
|
||||||
@@ -63,62 +76,163 @@ class _BleProvisionSheetState extends ConsumerState<BleProvisionSheet> {
|
|||||||
24,
|
24,
|
||||||
MediaQuery.of(context).viewInsets.bottom + 24,
|
MediaQuery.of(context).viewInsets.bottom + 24,
|
||||||
),
|
),
|
||||||
child: ListView(
|
child: _step == _Step.done
|
||||||
controller: scrollController,
|
? _DoneStep(
|
||||||
children: [
|
onGoToFloorPlan: () {
|
||||||
Text('Add sensor',
|
final router = GoRouter.of(context);
|
||||||
style: Theme.of(context).textTheme.titleLarge),
|
Navigator.of(context).pop();
|
||||||
const SizedBox(height: 16),
|
router.go('/floorplan');
|
||||||
|
},
|
||||||
// Scan results
|
onAddAnother: _reset,
|
||||||
// TODO: StreamBuilder on _provisioner.scan() — show a list of
|
onDone: () => Navigator.of(context).pop(),
|
||||||
// BleScanResult tiles; tapping one sets _selected.
|
)
|
||||||
const Text('Nearby ESP32 devices'),
|
: _ProvisionStep(
|
||||||
const SizedBox(height: 8),
|
scrollController: scrollController,
|
||||||
const Placeholder(fallbackHeight: 120),
|
provisioner: _provisioner,
|
||||||
const SizedBox(height: 24),
|
selected: _selected,
|
||||||
|
onSelected: (r) => setState(() => _selected = r),
|
||||||
if (_selected != null) ...[
|
ssidController: _ssidController,
|
||||||
Text('Selected: ${_selected!.name}'),
|
wifiPasswordController: _wifiPasswordController,
|
||||||
const SizedBox(height: 16),
|
provisioning: _provisioning,
|
||||||
TextField(
|
error: _error,
|
||||||
controller: _ssidController,
|
onProvision: _provision,
|
||||||
decoration: const InputDecoration(
|
|
||||||
labelText: 'WiFi SSID',
|
|
||||||
border: OutlineInputBorder(),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
const SizedBox(height: 12),
|
|
||||||
TextField(
|
|
||||||
controller: _wifiPasswordController,
|
|
||||||
decoration: const InputDecoration(
|
|
||||||
labelText: 'WiFi password',
|
|
||||||
border: OutlineInputBorder(),
|
|
||||||
),
|
|
||||||
obscureText: true,
|
|
||||||
),
|
|
||||||
const SizedBox(height: 16),
|
|
||||||
],
|
|
||||||
|
|
||||||
if (_error != null)
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.only(bottom: 12),
|
|
||||||
child: Text(_error!,
|
|
||||||
style: TextStyle(
|
|
||||||
color: Theme.of(context).colorScheme.error)),
|
|
||||||
),
|
|
||||||
|
|
||||||
FilledButton(
|
|
||||||
onPressed: (_selected == null || _provisioning) ? null : _provision,
|
|
||||||
child: _provisioning
|
|
||||||
? const SizedBox.square(
|
|
||||||
dimension: 20,
|
|
||||||
child: CircularProgressIndicator(strokeWidth: 2))
|
|
||||||
: const Text('Provision & add'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _ProvisionStep extends StatelessWidget {
|
||||||
|
const _ProvisionStep({
|
||||||
|
required this.scrollController,
|
||||||
|
required this.provisioner,
|
||||||
|
required this.selected,
|
||||||
|
required this.onSelected,
|
||||||
|
required this.ssidController,
|
||||||
|
required this.wifiPasswordController,
|
||||||
|
required this.provisioning,
|
||||||
|
required this.error,
|
||||||
|
required this.onProvision,
|
||||||
|
});
|
||||||
|
|
||||||
|
final ScrollController scrollController;
|
||||||
|
final BleProvisioner provisioner;
|
||||||
|
final BleScanResult? selected;
|
||||||
|
final ValueChanged<BleScanResult> onSelected;
|
||||||
|
final TextEditingController ssidController;
|
||||||
|
final TextEditingController wifiPasswordController;
|
||||||
|
final bool provisioning;
|
||||||
|
final String? error;
|
||||||
|
final VoidCallback onProvision;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return ListView(
|
||||||
|
controller: scrollController,
|
||||||
|
children: [
|
||||||
|
Text('Add sensor', style: Theme.of(context).textTheme.titleLarge),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
|
// Scan results
|
||||||
|
// TODO: StreamBuilder on provisioner.scan() — show a list of
|
||||||
|
// BleScanResult tiles; tapping one calls onSelected.
|
||||||
|
const Text('Nearby ESP32 devices'),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
const Placeholder(fallbackHeight: 120),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
|
||||||
|
if (selected != null) ...[
|
||||||
|
Text('Selected: ${selected!.name}'),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextField(
|
||||||
|
controller: ssidController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'WiFi SSID',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TextField(
|
||||||
|
controller: wifiPasswordController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'WiFi password',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
obscureText: true,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
],
|
||||||
|
|
||||||
|
if (error != null)
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(bottom: 12),
|
||||||
|
child: Text(error!,
|
||||||
|
style:
|
||||||
|
TextStyle(color: Theme.of(context).colorScheme.error)),
|
||||||
|
),
|
||||||
|
|
||||||
|
FilledButton(
|
||||||
|
onPressed: (selected == null || provisioning) ? null : onProvision,
|
||||||
|
child: provisioning
|
||||||
|
? const SizedBox.square(
|
||||||
|
dimension: 20,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2))
|
||||||
|
: const Text('Provision & add'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _DoneStep extends StatelessWidget {
|
||||||
|
const _DoneStep({
|
||||||
|
required this.onGoToFloorPlan,
|
||||||
|
required this.onAddAnother,
|
||||||
|
required this.onDone,
|
||||||
|
});
|
||||||
|
|
||||||
|
final VoidCallback onGoToFloorPlan;
|
||||||
|
final VoidCallback onAddAnother;
|
||||||
|
final VoidCallback onDone;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.check_circle_outline, size: 56),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
Text(
|
||||||
|
'Sensor provisioned!',
|
||||||
|
style: Theme.of(context).textTheme.titleLarge,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
'The sensor will appear in the list once it connects to the network. '
|
||||||
|
'Open the floor plan to place it.',
|
||||||
|
style: Theme.of(context).textTheme.bodyMedium,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 32),
|
||||||
|
FilledButton.icon(
|
||||||
|
icon: const Icon(Icons.map_outlined),
|
||||||
|
label: const Text('Place on floor plan'),
|
||||||
|
onPressed: onGoToFloorPlan,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
OutlinedButton.icon(
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
label: const Text('Add another sensor'),
|
||||||
|
onPressed: onAddAnother,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
TextButton(
|
||||||
|
onPressed: onDone,
|
||||||
|
child: const Text('Done'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user