HTTP-клиент к /api/v1 с Sanctum-токеном (хранится в secure storage), модели зеркалят Laravel API Resources. Экраны: логин, список устройств с живым статусом, детальная страница устройства (история телеметрии, turn-on/turn-off/set-level с проверкой capability), списки зон и правил автоматизации (пока только чтение — формы создания/редактирования впереди).
99 lines
2.9 KiB
Dart
99 lines
2.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../core/api_service.dart';
|
|
import '../models/device.dart';
|
|
import '../widgets/status_badge.dart';
|
|
import 'device_detail_screen.dart';
|
|
|
|
class DevicesTab extends StatefulWidget {
|
|
const DevicesTab({super.key});
|
|
|
|
@override
|
|
State<DevicesTab> createState() => _DevicesTabState();
|
|
}
|
|
|
|
class _DevicesTabState extends State<DevicesTab> {
|
|
late Future<List<Device>> _future;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_future = _load();
|
|
}
|
|
|
|
Future<List<Device>> _load() => context.read<ApiService>().devices();
|
|
|
|
Future<void> _refresh() async {
|
|
final future = _load();
|
|
setState(() => _future = future);
|
|
await future;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return RefreshIndicator(
|
|
onRefresh: _refresh,
|
|
child: FutureBuilder<List<Device>>(
|
|
future: _future,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState != ConnectionState.done) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (snapshot.hasError) {
|
|
return _ErrorList(message: '${snapshot.error}', onRetry: _refresh);
|
|
}
|
|
|
|
final devices = snapshot.data!;
|
|
if (devices.isEmpty) {
|
|
return _ErrorList(message: 'Устройств пока нет.', onRetry: _refresh);
|
|
}
|
|
|
|
return ListView.separated(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
itemCount: devices.length,
|
|
separatorBuilder: (context, index) => const Divider(height: 1),
|
|
itemBuilder: (context, index) {
|
|
final device = devices[index];
|
|
return ListTile(
|
|
title: Text(device.name),
|
|
subtitle: Text('${device.zone?.name ?? '—'} · ${device.deviceType?.code ?? device.protocol}'),
|
|
trailing: StatusBadge(status: device.status),
|
|
onTap: () => Navigator.of(context).push(
|
|
MaterialPageRoute(builder: (_) => DeviceDetailScreen(deviceId: device.id)),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ErrorList extends StatelessWidget {
|
|
const _ErrorList({required this.message, required this.onRetry});
|
|
|
|
final String message;
|
|
final Future<void> Function() onRetry;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListView(
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 64, horizontal: 24),
|
|
child: Column(
|
|
children: [
|
|
Text(message, textAlign: TextAlign.center),
|
|
const SizedBox(height: 12),
|
|
TextButton(onPressed: onRetry, child: const Text('Повторить')),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|