Формы создания/редактирования зон, устройств и правил автоматизации
ZoneFormScreen/DeviceFormScreen/AutomationRuleFormScreen — одна форма на создание и редактирование, показ ошибок валидации Laravel (422) по полям. Списки зон/устройств/правил дополнены FAB "добавить", свайпом/меню удаления и переходом в форму по тапу — всё только для owner (RBAC), viewer видит те же списки в режиме только чтения. Опции дропдаунов (типы устройств, датчики/актуаторы, capabilities) берутся из данных бэкенда, не хардкодятся.
This commit is contained in:
+88
-33
@@ -2,7 +2,9 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../core/api_service.dart';
|
||||
import '../core/auth_service.dart';
|
||||
import '../models/zone.dart';
|
||||
import 'zone_form_screen.dart';
|
||||
|
||||
class ZonesTab extends StatefulWidget {
|
||||
const ZonesTab({super.key});
|
||||
@@ -28,44 +30,97 @@ class _ZonesTabState extends State<ZonesTab> {
|
||||
await future;
|
||||
}
|
||||
|
||||
Future<void> _openForm({Zone? zone}) async {
|
||||
final saved = await Navigator.of(context).push<bool>(
|
||||
MaterialPageRoute(builder: (_) => ZoneFormScreen(zone: zone)),
|
||||
);
|
||||
if (saved == true) _refresh();
|
||||
}
|
||||
|
||||
Future<bool> _confirmDelete(Zone zone) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Удалить зону?'),
|
||||
content: Text('«${zone.name}» будет удалена без возможности восстановления.'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Отмена')),
|
||||
TextButton(onPressed: () => Navigator.pop(context, true), child: const Text('Удалить')),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true || !mounted) return false;
|
||||
|
||||
try {
|
||||
await context.read<ApiService>().deleteZone(zone.id);
|
||||
return true;
|
||||
} catch (e) {
|
||||
if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('$e')));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: _refresh,
|
||||
child: FutureBuilder<List<Zone>>(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text('${snapshot.error}', textAlign: TextAlign.center),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
final isOwner = context.watch<AuthService>().user?.isOwner ?? false;
|
||||
|
||||
final zones = snapshot.data!;
|
||||
return ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: zones.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final zone = zones[index];
|
||||
return ListTile(
|
||||
title: Text(zone.name),
|
||||
subtitle: zone.description != null ? Text(zone.description!) : null,
|
||||
trailing: Text('${zone.devicesCount ?? 0} устр.'),
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: _refresh,
|
||||
child: FutureBuilder<List<Zone>>(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text('${snapshot.error}', textAlign: TextAlign.center),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
}
|
||||
|
||||
final zones = snapshot.data!;
|
||||
return ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: zones.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final zone = zones[index];
|
||||
final tile = ListTile(
|
||||
title: Text(zone.name),
|
||||
subtitle: zone.description != null ? Text(zone.description!) : null,
|
||||
trailing: Text('${zone.devicesCount ?? 0} устр.'),
|
||||
onTap: isOwner ? () => _openForm(zone: zone) : null,
|
||||
);
|
||||
|
||||
if (!isOwner) return tile;
|
||||
|
||||
return Dismissible(
|
||||
key: ValueKey(zone.id),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
child: const Icon(Icons.delete, color: Colors.white),
|
||||
),
|
||||
confirmDismiss: (_) => _confirmDelete(zone),
|
||||
child: tile,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
floatingActionButton: isOwner
|
||||
? FloatingActionButton(onPressed: () => _openForm(), child: const Icon(Icons.add))
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user