feat: use floor plan widget in onboarding Floor Plan setup step
This commit is contained in:
@@ -1,30 +1,158 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
class StepFloorPlan extends StatelessWidget {
|
import '../../../domain/models/floor_plan_mode.dart';
|
||||||
|
import '../../../providers.dart';
|
||||||
|
import '../../floorplan/widgets/konva_web_view.dart';
|
||||||
|
|
||||||
|
class StepFloorPlan extends ConsumerStatefulWidget {
|
||||||
const StepFloorPlan({super.key, required this.onComplete});
|
const StepFloorPlan({super.key, required this.onComplete});
|
||||||
|
|
||||||
final VoidCallback onComplete;
|
final VoidCallback onComplete;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ConsumerState<StepFloorPlan> createState() => _StepFloorPlanState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _StepFloorPlanState extends ConsumerState<StepFloorPlan> {
|
||||||
|
final _konvaKey = GlobalKey<KonvaWebViewState>();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
ref.read(roomsProvider.future).then(
|
||||||
|
(rooms) => _konvaKey.currentState?.loadFloorPlan(rooms),
|
||||||
|
);
|
||||||
|
_konvaKey.currentState?.setMode(FloorPlanMode.edit);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Padding(
|
ref.listen(roomsProvider, (_, next) {
|
||||||
padding: const EdgeInsets.all(24),
|
next.whenData((r) => _konvaKey.currentState?.loadFloorPlan(r));
|
||||||
child: Column(
|
});
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
||||||
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
Text('Draw floor plan',
|
Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(24, 16, 24, 8),
|
||||||
|
child: Text('Draw floor plan',
|
||||||
style: Theme.of(context).textTheme.titleLarge),
|
style: Theme.of(context).textTheme.titleLarge),
|
||||||
const SizedBox(height: 16),
|
),
|
||||||
// TODO: embed KonvaWebView in editor mode (no live overlays).
|
Expanded(
|
||||||
// User draws rooms, sets scale, then taps Continue.
|
child: KonvaWebView(
|
||||||
const Expanded(child: Placeholder()),
|
key: _konvaKey,
|
||||||
const SizedBox(height: 16),
|
mode: FloorPlanMode.edit,
|
||||||
FilledButton(
|
onSensorTapped: (_) {},
|
||||||
onPressed: onComplete,
|
onSensorMoved: (_, __, ___, ____) {},
|
||||||
|
onRoomsUpdated: (roomUpdates) async {
|
||||||
|
final floor = ref.read(floorProvider).valueOrNull;
|
||||||
|
if (floor == null) return;
|
||||||
|
final repo = ref.read(floorRepositoryProvider);
|
||||||
|
for (final r in roomUpdates) {
|
||||||
|
await repo.updateRoom(floor.id, r.id, x: r.x, y: r.y);
|
||||||
|
}
|
||||||
|
ref.invalidate(roomsProvider);
|
||||||
|
},
|
||||||
|
onRoomAdded: (x, y) => _showAddRoomDialog(x, y),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: FilledButton(
|
||||||
|
onPressed: widget.onComplete,
|
||||||
child: const Text('Continue'),
|
child: const Text('Continue'),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _showAddRoomDialog(double x, double y) async {
|
||||||
|
final nameCtrl = TextEditingController();
|
||||||
|
final widthCtrl = TextEditingController(text: '5.0');
|
||||||
|
final heightCtrl = TextEditingController(text: '4.0');
|
||||||
|
|
||||||
|
final confirmed = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (ctx) => AlertDialog(
|
||||||
|
title: const Text('Add room'),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
TextField(
|
||||||
|
controller: nameCtrl,
|
||||||
|
decoration: const InputDecoration(labelText: 'Name'),
|
||||||
|
autofocus: true,
|
||||||
|
onSubmitted: (_) => Navigator.of(ctx).pop(true),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: TextField(
|
||||||
|
controller: widthCtrl,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Width', suffixText: 'm'),
|
||||||
|
keyboardType: const TextInputType.numberWithOptions(
|
||||||
|
decimal: true),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: TextField(
|
||||||
|
controller: heightCtrl,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Height', suffixText: 'm'),
|
||||||
|
keyboardType: const TextInputType.numberWithOptions(
|
||||||
|
decimal: true),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(ctx).pop(false),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.of(ctx).pop(true),
|
||||||
|
child: const Text('Add'),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
final name = nameCtrl.text.trim();
|
||||||
|
final width = double.tryParse(widthCtrl.text) ?? 5.0;
|
||||||
|
final height = double.tryParse(heightCtrl.text) ?? 4.0;
|
||||||
|
nameCtrl.dispose();
|
||||||
|
widthCtrl.dispose();
|
||||||
|
heightCtrl.dispose();
|
||||||
|
|
||||||
|
if (confirmed != true || !mounted) return;
|
||||||
|
if (name.isEmpty) return;
|
||||||
|
|
||||||
|
var floor = ref.read(floorProvider).valueOrNull;
|
||||||
|
if (floor == null) {
|
||||||
|
floor = await ref
|
||||||
|
.read(floorRepositoryProvider)
|
||||||
|
.createFloor(name: 'Ground Floor');
|
||||||
|
ref.invalidate(floorProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ref.read(floorRepositoryProvider).createRoom(
|
||||||
|
floor.id,
|
||||||
|
name: name,
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
);
|
||||||
|
ref.invalidate(roomsProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user