Geo location on 'homes' screen

This commit is contained in:
Artem Kokos
2026-04-13 22:13:56 +07:00
parent 5e09f41747
commit 1d31767ee0
5 changed files with 198 additions and 13 deletions

View File

@@ -7,13 +7,31 @@ import 'remote_screen.dart';
/// Экран "Дома" -- список серверов Ignis.
/// Пользователь может добавить, удалить, переключить активный дом.
class HomesScreen extends ConsumerWidget {
class HomesScreen extends ConsumerStatefulWidget {
const HomesScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
ConsumerState<HomesScreen> createState() => _HomesScreenState();
}
class _HomesScreenState extends ConsumerState<HomesScreen> {
@override
void initState() {
super.initState();
Future.microtask(() => ref.read(userLocationProvider.notifier).startWatching());
}
@override
void dispose() {
ref.read(userLocationProvider.notifier).stopWatching();
super.dispose();
}
@override
Widget build(BuildContext context) {
final homes = ref.watch(homesProvider);
final currentHome = ref.watch(currentHomeProvider);
final location = ref.watch(userLocationProvider);
return Scaffold(
appBar: AppBar(
@@ -46,6 +64,11 @@ class HomesScreen extends ConsumerWidget {
final home = homes[index];
final isActive = currentHome?.id == home.id;
// Расстояние до дома (null если нет координат или геолокации)
final distKm = location.distanceToKm(
home.latitude, home.longitude,
);
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: ListTile(
@@ -68,13 +91,31 @@ class HomesScreen extends ConsumerWidget {
home.url,
style: const TextStyle(color: Colors.white38, fontSize: 12),
),
if (home.hasCoordinates)
if (distKm != null)
Padding(
padding: const EdgeInsets.only(top: 2),
child: Row(
children: [
const Icon(Icons.near_me, size: 11, color: Colors.white30),
const SizedBox(width: 4),
Text(
'~${formatDistance(distKm)}',
style: const TextStyle(
color: Colors.white30,
fontSize: 11,
),
),
],
),
)
else if (home.hasCoordinates && !location.hasPosition)
// Координаты заданы, но геолокация недоступна
Row(
children: [
const Icon(Icons.location_on, size: 12, color: Colors.white24),
const SizedBox(width: 4),
Text(
'Координаты заданы',
location.error ?? 'Координаты заданы',
style: const TextStyle(color: Colors.white24, fontSize: 11),
),
],
@@ -87,16 +128,16 @@ class HomesScreen extends ConsumerWidget {
// Кнопка редактирования
IconButton(
icon: const Icon(Icons.edit, size: 20, color: Colors.white38),
onPressed: () => _editHome(context, ref, home),
onPressed: () => _editHome(context, home),
),
// Кнопка удаления
IconButton(
icon: const Icon(Icons.delete_outline, size: 20, color: Colors.redAccent),
onPressed: () => _confirmDelete(context, ref, home),
onPressed: () => _confirmDelete(context, home),
),
],
),
onTap: () => _selectHome(context, ref, home),
onTap: () => _selectHome(context, home),
),
);
},
@@ -110,7 +151,7 @@ class HomesScreen extends ConsumerWidget {
}
/// Выбрать дом и перейти на пульт
void _selectHome(BuildContext context, WidgetRef ref, HomeConfig home) async {
void _selectHome(BuildContext context, HomeConfig home) async {
await ref.read(currentHomeProvider.notifier).switchTo(home);
await ref.read(authInfoProvider.notifier).load();
if (context.mounted) {
@@ -128,14 +169,14 @@ class HomesScreen extends ConsumerWidget {
}
/// Редактировать дом
void _editHome(BuildContext context, WidgetRef ref, HomeConfig home) {
void _editHome(BuildContext context, HomeConfig home) {
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => HomeEditScreen(home: home)),
);
}
/// Подтвердить удаление
void _confirmDelete(BuildContext context, WidgetRef ref, HomeConfig home) {
void _confirmDelete(BuildContext context, HomeConfig home) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
@@ -157,4 +198,4 @@ class HomesScreen extends ConsumerWidget {
),
);
}
}
}