feat: add tag picker in sensor calibration flow

This commit is contained in:
2026-05-21 21:46:08 +02:00
parent cf3019f484
commit 12772bcd8f
6 changed files with 260 additions and 11 deletions
+190 -6
View File
@@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../domain/models/calibration.dart';
import '../../domain/models/tag.dart';
import '../../providers.dart';
void showCalibrationSheet(
@@ -71,11 +72,22 @@ class _CalibrationSheetState extends ConsumerState<CalibrationSheet> {
}
}
Future<void> _commitTag() async {
await ref.read(calibrationProvider(_key).notifier).commitTag();
if (mounted) {
_pageController.animateToPage(
2,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
Future<void> _finish() async {
await ref.read(calibrationProvider(_key).notifier).finishCalibration();
if (mounted) {
_pageController.animateToPage(
2,
3,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
@@ -115,6 +127,11 @@ class _CalibrationSheetState extends ConsumerState<CalibrationSheet> {
onBegin: _begin,
onCancel: () => _cancelAndClose(context),
),
_TagSelectionPage(
state: state,
sensorKey: _key,
onNext: _commitTag,
),
_CollectingPage(
state: state,
sensorKey: _key,
@@ -214,7 +231,170 @@ class _IntroPage extends StatelessWidget {
}
// ---------------------------------------------------------------------------
// Page 2 — Collecting
// Page 2 — Tag selection
// ---------------------------------------------------------------------------
class _TagSelectionPage extends ConsumerWidget {
const _TagSelectionPage({
required this.state,
required this.sensorKey,
required this.onNext,
});
final CalibrationState state;
final ({int id, String deviceId}) sensorKey;
final Future<void> Function() onNext;
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
final notifier = ref.read(calibrationProvider(sensorKey).notifier);
final tagsAsync = ref.watch(tagsProvider);
final tags = tagsAsync.whenOrNull(data: (t) => t) ?? const [];
final readings = state.tagRssiReadings;
// Sort: tags with readings by RSSI descending (nearest first),
// then tags without readings by id.
final sorted = [...tags]..sort((a, b) {
final ra = readings[a.tagId];
final rb = readings[b.tagId];
if (ra != null && rb != null) return rb.compareTo(ra);
if (ra != null) return -1;
if (rb != null) return 1;
return a.id.compareTo(b.id);
});
return SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(24, 16, 24, 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Center(
child: Container(
width: 32,
height: 4,
decoration: BoxDecoration(
color: theme.colorScheme.outlineVariant,
borderRadius: BorderRadius.circular(2),
),
),
),
const SizedBox(height: 20),
const Text(
'Select your tag',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
textAlign: TextAlign.center,
),
const SizedBox(height: 4),
Text(
'Hold each tag near the sensor — the one you\'re using will show a stronger signal.',
style: TextStyle(
fontSize: 13,
color: theme.colorScheme.onSurfaceVariant,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
Expanded(
child: tags.isEmpty
? Center(
child: Text(
'No tags enrolled',
style: TextStyle(
color: theme.colorScheme.onSurfaceVariant),
),
)
: ListView.builder(
itemCount: sorted.length,
itemBuilder: (context, i) {
final tag = sorted[i];
final rssi = readings[tag.tagId];
final selected = tag.tagId == state.selectedTagId;
return _TagListTile(
key: ValueKey(tag.tagId),
tag: tag,
rssi: rssi,
selected: selected,
onTap: () => notifier.selectTag(tag.tagId),
);
},
),
),
const SizedBox(height: 16),
_AsyncButton(
onPressed: onNext,
enabled: state.selectedTagId != null,
label: 'Next',
icon: Icons.arrow_forward,
),
],
),
),
);
}
}
class _TagListTile extends StatelessWidget {
const _TagListTile({
super.key,
required this.tag,
required this.rssi,
required this.selected,
required this.onTap,
});
final Tag tag;
final double? rssi;
final bool selected;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return ListTile(
selected: selected,
selectedTileColor: cs.primaryContainer,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
leading: Icon(
selected ? Icons.radio_button_checked : Icons.radio_button_unchecked,
color: selected ? cs.primary : cs.outline,
),
title: Text(tag.name),
subtitle: Text(
tag.tagId,
style: TextStyle(fontSize: 12, color: cs.onSurfaceVariant),
),
trailing: rssi != null
? TweenAnimationBuilder<double>(
tween: Tween(begin: rssi, end: rssi),
duration: const Duration(milliseconds: 300),
builder: (context, value, child) => Chip(
label: Text(
'${value.round()} dBm',
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
),
backgroundColor: cs.primaryContainer,
side: BorderSide.none,
padding: const EdgeInsets.symmetric(horizontal: 4),
),
)
: Chip(
label: Text(
'',
style: TextStyle(fontSize: 12, color: cs.onSurfaceVariant),
),
side: BorderSide.none,
padding: const EdgeInsets.symmetric(horizontal: 4),
),
onTap: onTap,
);
}
}
// ---------------------------------------------------------------------------
// Page 3 — Collecting
// ---------------------------------------------------------------------------
class _CollectingPage extends ConsumerWidget {
@@ -772,12 +952,14 @@ class _AsyncButton extends StatefulWidget {
required this.label,
this.icon,
this.compact = false,
this.enabled = true,
});
final Future<void> Function() onPressed;
final String label;
final IconData? icon;
final bool compact;
final bool enabled;
@override
State<_AsyncButton> createState() => _AsyncButtonState();
@@ -806,12 +988,14 @@ class _AsyncButtonState extends State<_AsyncButton> {
Icon(widget.icon, size: 18),
],
)
: Text(widget.label);
: Text(widget.label, style: TextStyle(fontSize: 36, color: Theme.of(context).colorScheme.primary));
final canPress = !_loading && widget.enabled;
if (widget.compact) {
return TextButton(
style: TextButton.styleFrom(shape: shape, textStyle: const .new(fontSize: 32)),
onPressed: _loading ? null : _run,
style: TextButton.styleFrom(shape: shape, textStyle: const TextStyle(fontSize: 36, color: Colors.black)),
onPressed: canPress ? _run : null,
child: child,
);
}
@@ -821,7 +1005,7 @@ class _AsyncButtonState extends State<_AsyncButton> {
shape: shape,
minimumSize: const Size.fromHeight(48),
),
onPressed: _loading ? null : _run,
onPressed: canPress ? _run : null,
child: child,
);
}