import 'package:flutter/material.dart'; import 'package:playmaker/classe/home.config.dart'; import 'package:playmaker/controllers/team_controllers.dart'; import 'package:playmaker/grafico%20de%20pizza/grafico.dart'; // Certifica-te que o caminho do controller está correto: class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State { int _selectedIndex = 0; // 1. Instanciar o Controller para aceder ao Supabase final TeamController _teamController = TeamController(); late final List _pages; @override void initState() { super.initState(); _pages = [ _buildHomeContent(), // Index 0: Home const Center(child: Text('Tela de Jogo')), // Index 1: Jogo _buildTeamsContent(), // Index 2: Equipas (O teu StreamBuilder entra aqui) const Center(child: Text('Tela de Status')), // Index 3: Status ]; } void _onItemSelected(int index) { setState(() { _selectedIndex = index; }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: const Text('PlayMaker'), backgroundColor: HomeConfig.primaryColor, foregroundColor: Colors.white, leading: IconButton( icon: const Icon(Icons.person), onPressed: () {}, ), ), body: IndexedStack( index: _selectedIndex, children: _pages, ), bottomNavigationBar: NavigationBar( selectedIndex: _selectedIndex, onDestinationSelected: _onItemSelected, backgroundColor: Theme.of(context).colorScheme.surface, surfaceTintColor: Theme.of(context).colorScheme.surfaceTint, elevation: 1, height: 70, destinations: const [ NavigationDestination( icon: Icon(Icons.home_outlined), selectedIcon: Icon(Icons.home_filled), label: 'Home', ), NavigationDestination( icon: Icon(Icons.sports_soccer_outlined), selectedIcon: Icon(Icons.sports_soccer), label: 'Jogo', ), NavigationDestination( icon: Icon(Icons.people_outline), selectedIcon: Icon(Icons.people), label: 'Equipas', ), NavigationDestination( icon: Icon(Icons.insights_outlined), selectedIcon: Icon(Icons.insights), label: 'Status', ), ], ), ); } // --- WIDGETS DE CONTEÚDO --- // 2. O teu StreamBuilder foi movido para aqui Widget _buildTeamsContent() { return StreamBuilder>>( stream: _teamController.teamsStream, builder: (context, snapshot) { // Verificar estado de carregamento if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); } // Verificar erros ou lista vazia if (snapshot.hasError) { return Center(child: Text("Erro: ${snapshot.error}")); } if (!snapshot.hasData || snapshot.data!.isEmpty) { return const Center(child: Text("Ainda não tens equipas.")); } // Obter dados (Lista simples do Supabase) final teams = snapshot.data!; return ListView.builder( padding: const EdgeInsets.all(16), itemCount: teams.length, itemBuilder: (context, index) { final team = teams[index]; // Construção do Card da Equipa return Card( elevation: 2, margin: const EdgeInsets.only(bottom: 12), child: ListTile( leading: CircleAvatar( backgroundColor: HomeConfig.primaryColor, child: Text( team['name'][0].toUpperCase(), style: const TextStyle(color: Colors.white), ), ), title: Text( team['name'], style: const TextStyle(fontWeight: FontWeight.bold), ), subtitle: Text("Época: ${team['season']}"), trailing: IconButton( icon: const Icon(Icons.delete, color: Colors.red), onPressed: () { // Confirmação antes de apagar (Opcional, mas recomendado) _teamController.deleteTeam(team['id']); }, ), ), ); }, ); }, ); } Widget _buildHomeContent() { return SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(20.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildStatCard( title: 'Mais Pontos', playerName: 'Michael Jordan', statValue: '34.5', statLabel: 'PPG', color: Colors.blue[800]!, icon: Icons.sports_basketball, isHighlighted: true, ), const SizedBox(width: 20), _buildStatCard( title: 'Mais Assistências', playerName: 'Magic Johnson', statValue: '12.8', statLabel: 'APG', color: Colors.green[800]!, icon: Icons.sports_basketball, isHighlighted: false, ), ], ), const SizedBox(height: 20), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildStatCard( title: 'Mais Rebotes', playerName: 'Dennis Rodman', statValue: '15.3', statLabel: 'RPG', color: Colors.purple[800]!, icon: Icons.sports_basketball, isHighlighted: false, ), const SizedBox(width: 20), PieChartCard( title: 'DESEMPENHO', subtitle: 'Vitórias vs Derrotas', backgroundColor: Colors.red[800]!, onTap: () {}, ), ], ), const SizedBox(height: 40), Text( 'Histórico de Jogos', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.grey[800], ), ), ], ), ), ); } Widget _buildStatCard({ required String title, required String playerName, required String statValue, required String statLabel, required Color color, required IconData icon, bool isHighlighted = false, }) { return SizedBox( width: HomeConfig.cardwidthPadding, height: HomeConfig.cardheightPadding, child: Card( elevation: 0, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20), side: isHighlighted ? const BorderSide(color: Colors.amber, width: 2) : BorderSide.none, ), child: Container( decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [color.withOpacity(0.9), color.withOpacity(0.7)], ), ), child: Padding( padding: const EdgeInsets.all(20.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title.toUpperCase(), style: const TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Colors.white70)), Text(playerName, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white)), ], ), ), if (isHighlighted) const Icon(Icons.star, color: Colors.amber, size: 20), ], ), const Spacer(), Center( child: Text(statValue, style: const TextStyle(fontSize: 38, fontWeight: FontWeight.bold, color: Colors.white)), ), Center( child: Text(statLabel, style: const TextStyle(fontSize: 12, color: Colors.white70)), ), const Spacer(), Container( width: double.infinity, padding: const EdgeInsets.symmetric(vertical: 8), decoration: BoxDecoration(color: Colors.white24, borderRadius: BorderRadius.circular(10)), child: const Center(child: Text('VER DETALHES', style: TextStyle(color: Colors.white, fontSize: 12))), ), ], ), ), ), ), ); } }