Files
ignis_app/lib/screens/homes_screen.dart

144 lines
5.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../models/home_config.dart';
import '../providers/providers.dart';
import 'home_edit_screen.dart';
import 'remote_screen.dart';
/// Экран "Дома" -- список серверов Ignis.
/// Пользователь может добавить, удалить, переключить активный дом.
class HomesScreen extends ConsumerWidget {
const HomesScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final homes = ref.watch(homesProvider);
final currentHome = ref.watch(currentHomeProvider);
return Scaffold(
appBar: AppBar(
title: const Text('ДОМА'),
automaticallyImplyLeading: false,
),
body: homes.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.home_outlined, size: 64, color: Colors.white24),
const SizedBox(height: 16),
const Text(
'Нет добавленных домов',
style: TextStyle(color: Colors.white54, fontSize: 16),
),
const SizedBox(height: 8),
const Text(
'Добавьте сервер Ignis',
style: TextStyle(color: Colors.white38, fontSize: 14),
),
],
),
)
: ListView.builder(
padding: const EdgeInsets.all(12),
itemCount: homes.length,
itemBuilder: (context, index) {
final home = homes[index];
final isActive = currentHome?.id == home.id;
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: ListTile(
leading: Icon(
Icons.home,
color: isActive ? Colors.deepOrange : Colors.white38,
size: 28,
),
title: Text(
home.name,
style: TextStyle(
fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
color: isActive ? Colors.deepOrange : Colors.white,
),
),
subtitle: Text(
home.url,
style: const TextStyle(color: Colors.white38, fontSize: 12),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
// Кнопка редактирования
IconButton(
icon: const Icon(Icons.edit, size: 20, color: Colors.white38),
onPressed: () => _editHome(context, ref, home),
),
// Кнопка удаления
IconButton(
icon: const Icon(Icons.delete_outline, size: 20, color: Colors.redAccent),
onPressed: () => _confirmDelete(context, ref, home),
),
],
),
onTap: () => _selectHome(context, ref, home),
),
);
},
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.deepOrange,
onPressed: () => _addHome(context),
child: const Icon(Icons.add),
),
);
}
/// Выбрать дом и перейти на пульт
void _selectHome(BuildContext context, WidgetRef ref, HomeConfig home) async {
await ref.read(currentHomeProvider.notifier).switchTo(home);
if (context.mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const RemoteScreen()),
);
}
}
/// Добавить новый дом
void _addHome(BuildContext context) {
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const HomeEditScreen()),
);
}
/// Редактировать дом
void _editHome(BuildContext context, WidgetRef ref, HomeConfig home) {
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => HomeEditScreen(home: home)),
);
}
/// Подтвердить удаление
void _confirmDelete(BuildContext context, WidgetRef ref, HomeConfig home) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Удалить дом?'),
content: Text('Удалить "${home.name}" (${home.url})?'),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).pop(),
child: const Text('Отмена'),
),
TextButton(
onPressed: () async {
Navigator.of(ctx).pop();
await ref.read(homesProvider.notifier).remove(home.id);
},
child: const Text('Удалить', style: TextStyle(color: Colors.redAccent)),
),
],
),
);
}
}