fazer vitoria e derrota
This commit is contained in:
@@ -3,6 +3,9 @@ import 'package:playmaker/classe/home.config.dart';
|
||||
import 'package:playmaker/grafico%20de%20pizza/grafico.dart';
|
||||
import 'package:playmaker/pages/gamePage.dart';
|
||||
import 'package:playmaker/pages/teamPage.dart';
|
||||
import 'package:playmaker/controllers/team_controller.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import 'dart:math' as math;
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
const HomeScreen({super.key});
|
||||
@@ -13,53 +16,50 @@ class HomeScreen extends StatefulWidget {
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen> {
|
||||
int _selectedIndex = 0;
|
||||
final TeamController _teamController = TeamController();
|
||||
String? _selectedTeamId;
|
||||
String _selectedTeamName = "Selecionar Equipa";
|
||||
|
||||
|
||||
late final List<Widget> _pages;
|
||||
// Instância do Supabase para buscar as estatísticas
|
||||
final _supabase = Supabase.instance.client;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_pages = [
|
||||
_buildHomeContent(),
|
||||
Widget build(BuildContext context) {
|
||||
final double wScreen = MediaQuery.of(context).size.width;
|
||||
final double hScreen = MediaQuery.of(context).size.height;
|
||||
final double sf = math.min(wScreen, hScreen) / 400;
|
||||
|
||||
final List<Widget> pages = [
|
||||
_buildHomeContent(sf, wScreen),
|
||||
const GamePage(),
|
||||
const TeamsPage(),
|
||||
const Center(child: Text('Tela de Status')),
|
||||
];
|
||||
}
|
||||
|
||||
void _onItemSelected(int index) {
|
||||
setState(() {
|
||||
_selectedIndex = index;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: const Text('PlayMaker'),
|
||||
title: Text('PlayMaker', style: TextStyle(fontSize: 20 * sf)),
|
||||
backgroundColor: HomeConfig.primaryColor,
|
||||
foregroundColor: Colors.white,
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.person),
|
||||
icon: Icon(Icons.person, size: 24 * sf),
|
||||
onPressed: () {},
|
||||
),
|
||||
),
|
||||
|
||||
body: IndexedStack(
|
||||
index: _selectedIndex,
|
||||
children: _pages,
|
||||
children: pages,
|
||||
),
|
||||
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _selectedIndex,
|
||||
onDestinationSelected: _onItemSelected,
|
||||
onDestinationSelected: (index) => setState(() => _selectedIndex = index),
|
||||
backgroundColor: Theme.of(context).colorScheme.surface,
|
||||
surfaceTintColor: Theme.of(context).colorScheme.surfaceTint,
|
||||
elevation: 1,
|
||||
height: 70,
|
||||
height: 70 * math.min(sf, 1.2),
|
||||
destinations: const [
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.home_outlined),
|
||||
@@ -85,136 +85,230 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
),
|
||||
);
|
||||
}
|
||||
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],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// --- POPUP DE SELEÇÃO DE EQUIPA ---
|
||||
void _showTeamSelector(BuildContext context, double sf) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(20 * sf)),
|
||||
),
|
||||
builder: (context) {
|
||||
return StreamBuilder<List<Map<String, dynamic>>>(
|
||||
stream: _teamController.teamsStream,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const SizedBox(height: 200, child: Center(child: CircularProgressIndicator()));
|
||||
}
|
||||
if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
||||
return SizedBox(height: 200 * sf, child: Center(child: Text("Nenhuma equipa criada.")));
|
||||
}
|
||||
|
||||
final teams = snapshot.data!;
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: teams.length,
|
||||
itemBuilder: (context, index) {
|
||||
final team = teams[index];
|
||||
return ListTile(
|
||||
title: Text(team['name']),
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_selectedTeamId = team['id'];
|
||||
_selectedTeamName = team['name'];
|
||||
});
|
||||
Navigator.pop(context);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
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)],
|
||||
),
|
||||
),
|
||||
Widget _buildHomeContent(double sf, double wScreen) {
|
||||
final double cardWidth = (wScreen - (40 * sf) - (20 * sf)) / 2;
|
||||
final double cardHeight = cardWidth * 1.4; // Ajustado para não cortar conteúdo
|
||||
|
||||
return StreamBuilder<List<Map<String, dynamic>>>(
|
||||
// Buscar estatísticas de todos os jogadores da equipa selecionada
|
||||
stream: _selectedTeamId != null
|
||||
? _supabase.from('player_stats_with_names').stream(primaryKey: ['id']).eq('team_id', _selectedTeamId!)
|
||||
: const Stream.empty(),
|
||||
builder: (context, snapshot) {
|
||||
// Lógica de cálculo de líderes
|
||||
Map<String, dynamic> leaders = _calculateLeaders(snapshot.data ?? []);
|
||||
|
||||
return SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
padding: EdgeInsets.all(20.0 * sf),
|
||||
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)),
|
||||
],
|
||||
),
|
||||
// Seletor de Equipa
|
||||
InkWell(
|
||||
onTap: () => _showTeamSelector(context, sf),
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(12 * sf),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.shade100,
|
||||
borderRadius: BorderRadius.circular(15 * sf),
|
||||
border: Border.all(color: Colors.grey.shade300),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.shield, color: HomeConfig.primaryColor, size: 24 * sf),
|
||||
SizedBox(width: 10 * sf),
|
||||
Text(_selectedTeamName, style: TextStyle(fontSize: 16 * sf, fontWeight: FontWeight.bold)),
|
||||
],
|
||||
),
|
||||
const Icon(Icons.arrow_drop_down),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 25 * sf),
|
||||
|
||||
// Primeira Linha: Pontos e Assistências
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildStatCard(
|
||||
title: 'Mais Pontos',
|
||||
playerName: leaders['pts_name'],
|
||||
statValue: leaders['pts_val'].toString(),
|
||||
statLabel: 'TOTAL',
|
||||
color: const Color(0xFF1565C0),
|
||||
icon: Icons.bolt,
|
||||
isHighlighted: true,
|
||||
sf: sf, cardWidth: cardWidth, cardHeight: cardHeight,
|
||||
),
|
||||
SizedBox(width: 20 * sf),
|
||||
_buildStatCard(
|
||||
title: 'Assistências',
|
||||
playerName: leaders['ast_name'],
|
||||
statValue: leaders['ast_val'].toString(),
|
||||
statLabel: 'TOTAL',
|
||||
color: const Color(0xFF2E7D32),
|
||||
icon: Icons.star,
|
||||
sf: sf, cardWidth: cardWidth, cardHeight: cardHeight,
|
||||
),
|
||||
if (isHighlighted) const Icon(Icons.star, color: Colors.amber, size: 20),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 20 * sf),
|
||||
|
||||
// Segunda Linha: Rebotes e Gráfico
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildStatCard(
|
||||
title: 'Rebotes',
|
||||
playerName: leaders['rbs_name'],
|
||||
statValue: leaders['rbs_val'].toString(),
|
||||
statLabel: 'TOTAL',
|
||||
color: const Color(0xFF6A1B9A),
|
||||
icon: Icons.trending_up,
|
||||
sf: sf, cardWidth: cardWidth, cardHeight: cardHeight,
|
||||
),
|
||||
SizedBox(width: 20 * sf),
|
||||
PieChartCard(
|
||||
title: 'DESEMPENHO',
|
||||
subtitle: 'Temporada',
|
||||
backgroundColor: const Color(0xFFC62828),
|
||||
onTap: () {},
|
||||
sf: sf, cardWidth: cardWidth, cardHeight: cardHeight,
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 40 * sf),
|
||||
Text('Histórico de Jogos', style: TextStyle(fontSize: 24 * sf, fontWeight: FontWeight.bold, color: Colors.grey[800])),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _calculateLeaders(List<Map<String, dynamic>> data) {
|
||||
Map<String, int> ptsMap = {};
|
||||
Map<String, int> astMap = {};
|
||||
Map<String, int> rbsMap = {};
|
||||
Map<String, String> namesMap = {}; // Aqui vamos guardar o nome real
|
||||
|
||||
for (var row in data) {
|
||||
String pid = row['member_id'].toString();
|
||||
|
||||
// 👇 BUSCA O NOME QUE VEM DA VIEW 👇
|
||||
namesMap[pid] = row['player_name']?.toString() ?? "Desconhecido";
|
||||
|
||||
ptsMap[pid] = (ptsMap[pid] ?? 0) + (row['pts'] as int? ?? 0);
|
||||
astMap[pid] = (astMap[pid] ?? 0) + (row['ast'] as int? ?? 0);
|
||||
rbsMap[pid] = (rbsMap[pid] ?? 0) + (row['rbs'] as int? ?? 0);
|
||||
}
|
||||
|
||||
// Se não houver dados, namesMap estará vazio e o reduce daria erro.
|
||||
// Por isso, se estiver vazio, retornamos logo "---".
|
||||
if (ptsMap.isEmpty) {
|
||||
return {
|
||||
'pts_name': '---', 'pts_val': 0,
|
||||
'ast_name': '---', 'ast_val': 0,
|
||||
'rbs_name': '---', 'rbs_val': 0,
|
||||
};
|
||||
}
|
||||
|
||||
String getBest(Map<String, int> map) {
|
||||
var bestId = map.entries.reduce((a, b) => a.value > b.value ? a : b).key;
|
||||
return namesMap[bestId]!;
|
||||
}
|
||||
|
||||
int getBestVal(Map<String, int> map) => map.values.reduce((a, b) => a > b ? a : b);
|
||||
|
||||
return {
|
||||
'pts_name': getBest(ptsMap), 'pts_val': getBestVal(ptsMap),
|
||||
'ast_name': getBest(astMap), 'ast_val': getBestVal(astMap),
|
||||
'rbs_name': getBest(rbsMap), 'rbs_val': getBestVal(rbsMap),
|
||||
};
|
||||
}
|
||||
|
||||
Widget _buildStatCard({
|
||||
required String title, required String playerName, required String statValue,
|
||||
required String statLabel, required Color color, required IconData icon,
|
||||
bool isHighlighted = false, required double sf, required double cardWidth, required double cardHeight,
|
||||
}) {
|
||||
return SizedBox(
|
||||
width: cardWidth, height: cardHeight,
|
||||
child: Card(
|
||||
elevation: 4,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(20 * sf),
|
||||
side: isHighlighted ? const BorderSide(color: Colors.amber, width: 2) : BorderSide.none,
|
||||
),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(20 * sf),
|
||||
gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [color.withOpacity(0.9), color]),
|
||||
),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16.0 * sf),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(title.toUpperCase(), style: TextStyle(fontSize: 10 * sf, fontWeight: FontWeight.bold, color: Colors.white70)),
|
||||
Text(playerName, style: TextStyle(fontSize: 14 * sf, fontWeight: FontWeight.bold, color: Colors.white), maxLines: 1, overflow: TextOverflow.ellipsis),
|
||||
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)),
|
||||
),
|
||||
Center(child: Text(statValue, style: TextStyle(fontSize: 32 * sf, fontWeight: FontWeight.bold, color: Colors.white))),
|
||||
Center(child: Text(statLabel, style: TextStyle(fontSize: 10 * sf, color: Colors.white70))),
|
||||
const Spacer(),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
width: double.infinity, padding: const EdgeInsets.symmetric(vertical: 6),
|
||||
decoration: BoxDecoration(color: Colors.white24, borderRadius: BorderRadius.circular(10)),
|
||||
child: const Center(child: Text('VER DETALHES', style: TextStyle(color: Colors.white, fontSize: 12))),
|
||||
child: Center(child: Text('DETALHES', style: TextStyle(color: Colors.white, fontSize: 10 * sf))),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user