Project

General

Profile


home_page.dart feeding_page.dart connection_page.dart config_page.dart

history_page.dart


import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class HistoryPage extends StatelessWidget {
  final String aquariumId;

  const HistoryPage({super.key, required this.aquariumId});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF001B2E),
      appBar: AppBar(
        title: const Text("Historial de Alertas", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
        backgroundColor: Colors.transparent,
        iconTheme: const IconThemeData(color: Colors.white),
        elevation: 0,
      ),
      body: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection('acuarios')
            .doc(aquariumId)
            .collection('historial')
            .orderBy('timestamp', descending: true)
            .limit(50)
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasError) return const Center(child: Text("Error cargando historial", style: TextStyle(color: Colors.white)));
          if (snapshot.connectionState == ConnectionState.waiting) return const Center(child: CircularProgressIndicator(color: Color(0xFFFF5400)));

          final docs = snapshot.data!.docs;

          if (docs.isEmpty) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.history_toggle_off, size: 70, color: Colors.white.withOpacity(0.2)),
                  const SizedBox(height: 10),
                  Text("Sin alertas registradas", style: TextStyle(color: Colors.white.withOpacity(0.5))),
                ],
              ),
            );
          }

          return ListView.builder(
            padding: const EdgeInsets.all(15),
            itemCount: docs.length,
            itemBuilder: (context, index) {
              final data = docs[index].data() as Map<String, dynamic>;

              // Diseño de cada fila del historial
              return Container(
                margin: const EdgeInsets.only(bottom: 10),
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.05),
                  borderRadius: BorderRadius.circular(15),
                  border: Border(left: BorderSide(color: Colors.redAccent.shade400, width: 4)),
                ),
                child: ListTile(
                  contentPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
                  leading: const CircleAvatar(
                    backgroundColor: Color(0xFF3E1313),
                    child: Icon(Icons.warning_amber_rounded, color: Colors.redAccent),
                  ),
                  title: Text(
                    data['mensaje'] ?? "Alerta", 
                    style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                  ),
                  subtitle: Text(
                    data['fecha'] ?? "--/--/--", 
                    style: TextStyle(color: Colors.white.withOpacity(0.5)),
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}