// =========================================================================== // FILE: lib/ui/home/home_screen.dart // =========================================================================== import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:flutter/services.dart'; import 'dart:math' as math; import 'package:google_fonts/google_fonts.dart'; import '../../core/theme_manager.dart'; import '../../core/app_colors.dart'; import '../../models/game_board.dart'; import '../game/game_screen.dart'; import '../settings/settings_screen.dart'; import '../../logic/game_controller.dart'; import '../../services/storage_service.dart'; import '../multiplayer/lobby_screen.dart'; import 'history_screen.dart'; // --- HELPER PER IL FONT --- TextStyle _getTextStyle(AppThemeType themeType, TextStyle baseStyle) { if (themeType == AppThemeType.doodle) { return GoogleFonts.permanentMarker(textStyle: baseStyle); } return baseStyle; } // =========================================================================== // IL NOSTRO "PITTORE" DI SCARABOCCHI // Genera bordi irregolari, tratti doppi a penna e riempimenti sbavati. // =========================================================================== class _DoodleBackgroundPainter extends CustomPainter { final Color fillColor; final Color strokeColor; final int seed; final bool isCircle; _DoodleBackgroundPainter({required this.fillColor, required this.strokeColor, required this.seed, this.isCircle = false}); @override void paint(Canvas canvas, Size size) { final math.Random random = math.Random(seed); double wobble() => random.nextDouble() * 6 - 3; final Paint fillPaint = Paint() ..color = fillColor ..style = PaintingStyle.fill; final Paint strokePaint = Paint() ..color = strokeColor ..strokeWidth = 2.5 ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round; if (isCircle) { final Rect rect = Rect.fromLTWH(wobble(), wobble(), size.width + wobble(), size.height + wobble()); canvas.save(); canvas.translate(wobble(), wobble()); canvas.drawOval(rect, fillPaint); canvas.restore(); canvas.drawOval(rect, strokePaint); canvas.save(); canvas.translate(random.nextDouble() * 4 - 2, random.nextDouble() * 4 - 2); canvas.drawOval(rect, strokePaint..strokeWidth = 1.0..color = strokeColor.withOpacity(0.6)); canvas.restore(); } else { final Path path = Path(); path.moveTo(wobble(), wobble()); path.lineTo(size.width + wobble(), wobble()); path.lineTo(size.width + wobble(), size.height + wobble()); path.lineTo(wobble(), size.height + wobble()); path.close(); final Path fillPath = Path(); fillPath.moveTo(wobble() * 1.5, wobble() * 1.5); fillPath.lineTo(size.width + wobble() * 1.5, wobble() * 1.5); fillPath.lineTo(size.width + wobble() * 1.5, size.height + wobble() * 1.5); fillPath.lineTo(wobble() * 1.5, size.height + wobble() * 1.5); fillPath.close(); canvas.drawPath(fillPath, fillPaint); canvas.drawPath(path, strokePaint); canvas.save(); canvas.translate(random.nextDouble() * 3 - 1.5, random.nextDouble() * 3 - 1.5); canvas.drawPath(path, strokePaint..strokeWidth = 1.0..color = strokeColor.withOpacity(0.6)); canvas.restore(); } } @override bool shouldRepaint(covariant _DoodleBackgroundPainter oldDelegate) { return oldDelegate.fillColor != fillColor || oldDelegate.strokeColor != strokeColor; } } // --- WIDGET PERSONALIZZATI --- class _NeonShapeButton extends StatelessWidget { final IconData icon; final String label; final bool isSelected; final ThemeColors theme; final AppThemeType themeType; final VoidCallback onTap; final bool isLocked; final bool isSpecial; const _NeonShapeButton({ required this.icon, required this.label, required this.isSelected, required this.theme, required this.themeType, required this.onTap, this.isLocked = false, this.isSpecial = false }); Color _getDoodleColor() { switch (label) { case 'Rombo': return Colors.lightBlue.shade200; case 'Croce': return Colors.green.shade200; case 'Buco': return Colors.pink.shade200; case 'Clessidra': return Colors.purple.shade200; case 'Caos': return Colors.grey.shade300; default: return Colors.lightBlue.shade200; } } @override Widget build(BuildContext context) { if (themeType == AppThemeType.doodle) { Color doodleColor = isLocked ? Colors.grey : _getDoodleColor(); Color inkColor = const Color(0xFF111122); double tilt = (label.length % 2 == 0) ? -0.05 : 0.04; return Transform.rotate( angle: tilt, child: GestureDetector( onTap: isLocked ? null : onTap, child: CustomPaint( painter: _DoodleBackgroundPainter( fillColor: isSelected ? doodleColor : Colors.white.withOpacity(0.8), strokeColor: inkColor, seed: label.length * 3, ), child: Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(isLocked ? Icons.lock : icon, color: inkColor, size: 24), const SizedBox(height: 2), Text(isLocked ? "Liv. 10" : label, style: _getTextStyle(themeType, TextStyle(color: inkColor, fontSize: 11, fontWeight: FontWeight.w900, letterSpacing: 0.5))), ], ), ), ), ), ); } // --- STILE STANDARD --- Color mainColor = isSpecial && !isLocked ? Colors.purpleAccent : theme.playerBlue; return GestureDetector( onTap: isLocked ? null : onTap, child: AnimatedContainer( duration: const Duration(milliseconds: 250), curve: Curves.easeOutCubic, padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12), transform: Matrix4.translationValues(0, isSelected ? 2 : 0, 0), decoration: BoxDecoration( borderRadius: BorderRadius.circular(15), gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: isLocked ? [Colors.grey.withOpacity(0.1), Colors.black.withOpacity(0.2)] : isSelected ? [mainColor.withOpacity(0.3), mainColor.withOpacity(0.1)] : [theme.text.withOpacity(0.1), theme.text.withOpacity(0.02)], ), border: Border.all( color: isLocked ? Colors.transparent : (isSelected ? mainColor : Colors.white.withOpacity(0.1)), width: isSelected ? 2 : 1, ), boxShadow: isLocked ? [] : isSelected ? [BoxShadow(color: mainColor.withOpacity(0.5), blurRadius: 15, spreadRadius: 1, offset: const Offset(0, 0))] : [ BoxShadow(color: Colors.black.withOpacity(0.4), blurRadius: 6, offset: const Offset(2, 4)), BoxShadow(color: Colors.white.withOpacity(0.05), blurRadius: 2, offset: const Offset(-1, -1)), ], ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(isLocked ? Icons.lock : icon, color: isLocked ? Colors.grey.withOpacity(0.5) : (isSelected ? Colors.white : theme.text.withOpacity(0.6)), size: 24), const SizedBox(height: 6), Text(isLocked ? "Liv. 10" : label, style: _getTextStyle(themeType, TextStyle(color: isLocked ? Colors.grey.withOpacity(0.5) : (isSelected ? Colors.white : theme.text.withOpacity(0.6)), fontSize: 11, fontWeight: isSelected ? FontWeight.w900 : FontWeight.bold))), ], ), ), ); } } class _NeonSizeButton extends StatelessWidget { final String label; final bool isSelected; final ThemeColors theme; final AppThemeType themeType; final VoidCallback onTap; const _NeonSizeButton({required this.label, required this.isSelected, required this.theme, required this.themeType, required this.onTap}); @override Widget build(BuildContext context) { if (themeType == AppThemeType.doodle) { Color doodleColor = label == 'MAX' ? Colors.red.shade200 : Colors.cyan.shade100; Color inkColor = const Color(0xFF111122); double tilt = (label == 'M' || label == 'MAX') ? 0.05 : -0.04; return Transform.rotate( angle: tilt, child: GestureDetector( onTap: onTap, child: CustomPaint( painter: _DoodleBackgroundPainter( fillColor: isSelected ? doodleColor : Colors.white.withOpacity(0.8), strokeColor: inkColor, seed: label.codeUnitAt(0), isCircle: true, ), child: SizedBox( width: 50, height: 50, child: Center( child: Text(label, style: _getTextStyle(themeType, TextStyle(color: inkColor, fontSize: 18, fontWeight: FontWeight.w900))), ), ), ), ), ); } return GestureDetector( onTap: onTap, child: AnimatedContainer( duration: const Duration(milliseconds: 250), curve: Curves.easeOutCubic, width: 50, height: 50, transform: Matrix4.translationValues(0, isSelected ? 2 : 0, 0), decoration: BoxDecoration( shape: BoxShape.circle, gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: isSelected ? [theme.playerRed.withOpacity(0.3), theme.playerRed.withOpacity(0.1)] : [theme.text.withOpacity(0.1), theme.text.withOpacity(0.02)], ), border: Border.all( color: isSelected ? theme.playerRed : Colors.white.withOpacity(0.1), width: isSelected ? 2 : 1, ), boxShadow: isSelected ? [BoxShadow(color: theme.playerRed.withOpacity(0.5), blurRadius: 15, spreadRadius: 1)] : [ BoxShadow(color: Colors.black.withOpacity(0.4), blurRadius: 6, offset: const Offset(2, 4)), BoxShadow(color: Colors.white.withOpacity(0.05), blurRadius: 2, offset: const Offset(-1, -1)), ], ), child: Center( child: Text(label, style: _getTextStyle(themeType, TextStyle(color: isSelected ? Colors.white : theme.text.withOpacity(0.6), fontSize: 14, fontWeight: isSelected ? FontWeight.w900 : FontWeight.bold))), ), ), ); } } class _NeonTimeSwitch extends StatelessWidget { final bool isTimeMode; final ThemeColors theme; final AppThemeType themeType; final VoidCallback onTap; const _NeonTimeSwitch({required this.isTimeMode, required this.theme, required this.themeType, required this.onTap}); @override Widget build(BuildContext context) { if (themeType == AppThemeType.doodle) { Color doodleColor = Colors.orange.shade200; Color inkColor = const Color(0xFF111122); return Transform.rotate( angle: -0.015, child: GestureDetector( onTap: onTap, child: CustomPaint( painter: _DoodleBackgroundPainter( fillColor: isTimeMode ? doodleColor : Colors.white.withOpacity(0.8), strokeColor: inkColor, seed: 42, ), child: Container( padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12), child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(isTimeMode ? Icons.timer : Icons.timer_off, color: inkColor, size: 28), const SizedBox(width: 12), Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Text(isTimeMode ? 'A TEMPO' : 'RELAX', style: _getTextStyle(themeType, TextStyle(color: inkColor, fontWeight: FontWeight.w900, fontSize: 16, letterSpacing: 2.0))), Text(isTimeMode ? '15 sec a mossa' : 'Nessun limite', style: _getTextStyle(themeType, TextStyle(color: inkColor.withOpacity(0.8), fontSize: 13, fontWeight: FontWeight.bold))), ], ), ], ), ), ), ), ); } return GestureDetector( onTap: onTap, child: AnimatedContainer( duration: const Duration(milliseconds: 300), curve: Curves.easeInOut, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: isTimeMode ? [Colors.amber.withOpacity(0.25), Colors.amber.withOpacity(0.05)] : [theme.text.withOpacity(0.1), theme.text.withOpacity(0.02)], ), border: Border.all( color: isTimeMode ? Colors.amber : Colors.white.withOpacity(0.1), width: isTimeMode ? 2 : 1, ), boxShadow: isTimeMode ? [BoxShadow(color: Colors.amber.withOpacity(0.3), blurRadius: 15, spreadRadius: 2)] : [ BoxShadow(color: Colors.black.withOpacity(0.4), blurRadius: 6, offset: const Offset(2, 4)), BoxShadow(color: Colors.white.withOpacity(0.05), blurRadius: 2, offset: const Offset(-1, -1)), ], ), child: Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(isTimeMode ? Icons.timer : Icons.timer_off, color: isTimeMode ? Colors.amber : theme.text.withOpacity(0.5), size: 28), const SizedBox(width: 12), Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Text(isTimeMode ? 'A TEMPO' : 'RELAX', style: _getTextStyle(themeType, TextStyle(color: isTimeMode ? Colors.white : theme.text.withOpacity(0.5), fontWeight: FontWeight.w900, fontSize: 14, letterSpacing: 1.5))), Text(isTimeMode ? '15 sec a mossa' : 'Nessun limite', style: _getTextStyle(themeType, TextStyle(color: isTimeMode ? Colors.amber.shade200 : theme.text.withOpacity(0.4), fontSize: 11, fontWeight: FontWeight.bold))), ], ), ], ), ), ); } } // --------------------------------------------------------------------------- class HomeScreen extends StatefulWidget { const HomeScreen({super.key}); @override State createState() => _HomeScreenState(); } class _HomeScreenState extends State with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addPostFrameCallback((_) { _checkPlayerName(); }); _checkClipboardForInvite(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { _checkClipboardForInvite(); } } void _checkPlayerName() { if (StorageService.instance.playerName.isEmpty) { _showNameDialog(); } } void _showNameDialog() { final TextEditingController nameController = TextEditingController(text: StorageService.instance.playerName); showDialog( context: context, barrierDismissible: false, barrierColor: Colors.black.withOpacity(0.8), builder: (context) { final themeManager = context.watch(); final theme = themeManager.currentColors; final themeType = themeManager.currentThemeType; Color inkColor = const Color(0xFF111122); Widget dialogContent = themeType == AppThemeType.doodle ? CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.yellow.shade100, strokeColor: inkColor, seed: 100), child: Padding( padding: const EdgeInsets.symmetric(vertical: 40.0, horizontal: 25.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text('BENVENUTO!', style: _getTextStyle(themeType, TextStyle(color: inkColor, fontWeight: FontWeight.w900, fontSize: 28, letterSpacing: 2.0)), textAlign: TextAlign.center), const SizedBox(height: 20), Text('Scegli il tuo nome da battaglia', style: _getTextStyle(themeType, TextStyle(color: inkColor.withOpacity(0.8), fontSize: 18)), textAlign: TextAlign.center), const SizedBox(height: 30), TextField( controller: nameController, textCapitalization: TextCapitalization.characters, textAlign: TextAlign.center, maxLength: 5, style: _getTextStyle(themeType, TextStyle(color: inkColor, fontSize: 36, fontWeight: FontWeight.bold, letterSpacing: 8)), decoration: InputDecoration( hintText: 'NOME', hintStyle: _getTextStyle(themeType, TextStyle(color: inkColor.withOpacity(0.3), letterSpacing: 4)), filled: false, counterText: "", enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: inkColor, width: 3)), focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.red.shade200, width: 5)), ), ), const SizedBox(height: 40), GestureDetector( onTap: () { final name = nameController.text.trim(); if (name.isNotEmpty) { StorageService.instance.savePlayerName(name); Navigator.of(context).pop(); setState(() {}); } }, child: CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.green.shade200, strokeColor: inkColor, seed: 101), child: Container( width: double.infinity, height: 60, alignment: Alignment.center, child: Text('SALVA E GIOCA', style: _getTextStyle(themeType, TextStyle(color: inkColor, fontSize: 22, fontWeight: FontWeight.bold, letterSpacing: 1.5))), ), ), ), ], ), ), ) : Container( decoration: BoxDecoration( color: theme.background, borderRadius: BorderRadius.circular(25), border: Border.all(color: theme.playerBlue.withOpacity(0.5), width: 2), boxShadow: [BoxShadow(color: theme.playerBlue.withOpacity(0.3), blurRadius: 20, spreadRadius: 5)] ), child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.symmetric(vertical: 30.0, horizontal: 25.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text('BENVENUTO IN TETRAQ!', style: _getTextStyle(themeType, TextStyle(color: theme.text, fontWeight: FontWeight.w900, fontSize: 24, letterSpacing: 1.5)), textAlign: TextAlign.center), const SizedBox(height: 20), Text('Scegli il tuo nome da battaglia per sfidare i tuoi amici online.', style: _getTextStyle(themeType, TextStyle(color: theme.text.withOpacity(0.8), fontSize: 16)), textAlign: TextAlign.center), const SizedBox(height: 40), TextField( controller: nameController, textCapitalization: TextCapitalization.characters, textAlign: TextAlign.center, maxLength: 5, style: _getTextStyle(themeType, TextStyle(color: theme.text, fontSize: 32, fontWeight: FontWeight.bold, letterSpacing: 8)), decoration: InputDecoration( hintText: 'NOME', hintStyle: _getTextStyle(themeType, TextStyle(color: theme.text.withOpacity(0.3), letterSpacing: 4)), filled: true, fillColor: theme.text.withOpacity(0.05), counterText: "", enabledBorder: OutlineInputBorder(borderSide: BorderSide(color: theme.gridLine.withOpacity(0.5), width: 2), borderRadius: BorderRadius.circular(15)), focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: theme.playerBlue, width: 3), borderRadius: BorderRadius.circular(15)), ), ), const SizedBox(height: 40), SizedBox( width: double.infinity, height: 55, child: ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: theme.playerBlue, foregroundColor: Colors.white, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15))), onPressed: () { final name = nameController.text.trim(); if (name.isNotEmpty) { StorageService.instance.savePlayerName(name); Navigator.of(context).pop(); setState(() {}); } }, child: Text('SALVA E GIOCA', style: _getTextStyle(themeType, const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, letterSpacing: 1.5))), ), ), ], ), ), ), ); if (themeType == AppThemeType.cyberpunk) dialogContent = _AnimatedCyberBorder(child: dialogContent); return Dialog(backgroundColor: Colors.transparent, insetPadding: const EdgeInsets.all(20), child: dialogContent); }, ); } Future _checkClipboardForInvite() async { try { ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain); String? text = data?.text; if (text != null && text.contains("TetraQ") && text.contains("codice:")) { RegExp regExp = RegExp(r'codice:\s*([A-Z0-9]{5})', caseSensitive: false); Match? match = regExp.firstMatch(text); if (match != null) { String roomCode = match.group(1)!.toUpperCase(); await Clipboard.setData(const ClipboardData(text: '')); if (mounted && ModalRoute.of(context)?.isCurrent == true) { _promptJoinRoom(roomCode); } } } } catch (e) { debugPrint("Errore lettura appunti: $e"); } } void _promptJoinRoom(String roomCode) { showDialog( context: context, builder: (context) { final theme = context.watch().currentColors; final themeType = context.read().currentThemeType; return AlertDialog( backgroundColor: themeType == AppThemeType.doodle ? Colors.white : theme.background, shape: themeType == AppThemeType.doodle ? RoundedRectangleBorder(borderRadius: BorderRadius.circular(15), side: BorderSide(color: theme.text, width: 2)) : null, title: Text("Invito Trovato!", style: _getTextStyle(themeType, TextStyle(color: theme.text, fontWeight: FontWeight.bold))), content: Text("Vuoi unirti alla stanza $roomCode?", style: _getTextStyle(themeType, TextStyle(color: theme.text))), actions: [ TextButton(onPressed: () => Navigator.pop(context), child: Text("No", style: _getTextStyle(themeType, const TextStyle(color: Colors.red)))), ElevatedButton( style: ElevatedButton.styleFrom( backgroundColor: themeType == AppThemeType.doodle ? Colors.transparent : theme.playerBlue, elevation: 0, side: themeType == AppThemeType.doodle ? BorderSide(color: theme.text, width: 1.5) : BorderSide.none, ), onPressed: () { Navigator.of(context).pop(); Navigator.push(context, MaterialPageRoute(builder: (_) => LobbyScreen(initialRoomCode: roomCode))); }, child: Text("Unisciti", style: _getTextStyle(themeType, TextStyle(color: themeType == AppThemeType.doodle ? theme.text : Colors.white, fontWeight: FontWeight.bold))), ), ], ); } ); } // --- MENU SETUP PARTITA (Popup per CPU e Locale) --- void _showMatchSetupDialog(bool isVsCPU) { int localRadius = 4; ArenaShape localShape = ArenaShape.classic; bool localTimeMode = true; int cpuLevel = StorageService.instance.cpuLevel; bool isChaosUnlocked = cpuLevel >= 10; showDialog( context: context, barrierColor: Colors.black.withOpacity(0.8), builder: (ctx) { final themeManager = ctx.watch(); final theme = themeManager.currentColors; final themeType = themeManager.currentThemeType; Color inkColor = const Color(0xFF111122); return StatefulBuilder( builder: (context, setStateDialog) { Widget dialogContent = themeType == AppThemeType.doodle ? Transform.rotate( angle: 0.015, child: CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.white.withOpacity(0.95), strokeColor: inkColor, seed: 200), child: SingleChildScrollView( physics: const BouncingScrollPhysics(), child: Padding( padding: const EdgeInsets.all(25.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(isVsCPU ? "SFIDA IA" : "MODALITÀ LOCALE", style: _getTextStyle(themeType, TextStyle(fontSize: 26, fontWeight: FontWeight.w900, color: inkColor, letterSpacing: 2))), const SizedBox(height: 25), Text("FORMA ARENA", style: _getTextStyle(themeType, TextStyle(fontSize: 14, fontWeight: FontWeight.w900, color: inkColor.withOpacity(0.6), letterSpacing: 1.5))), const SizedBox(height: 15), Wrap( spacing: 12, runSpacing: 12, alignment: WrapAlignment.center, children: [ _NeonShapeButton(icon: Icons.diamond_outlined, label: 'Rombo', isSelected: localShape == ArenaShape.classic, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.classic)), _NeonShapeButton(icon: Icons.add, label: 'Croce', isSelected: localShape == ArenaShape.cross, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.cross)), _NeonShapeButton(icon: Icons.donut_large, label: 'Buco', isSelected: localShape == ArenaShape.donut, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.donut)), _NeonShapeButton(icon: Icons.hourglass_bottom, label: 'Clessidra', isSelected: localShape == ArenaShape.hourglass, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.hourglass)), _NeonShapeButton(icon: Icons.all_inclusive, label: 'Caos', isSelected: localShape == ArenaShape.chaos, theme: theme, themeType: themeType, isSpecial: true, isLocked: !isChaosUnlocked, onTap: () => setStateDialog(() => localShape = ArenaShape.chaos)), ], ), const SizedBox(height: 25), Divider(color: inkColor.withOpacity(0.3), thickness: 2.5), const SizedBox(height: 20), Text("GRANDEZZA", style: _getTextStyle(themeType, TextStyle(fontSize: 14, fontWeight: FontWeight.w900, color: inkColor.withOpacity(0.6), letterSpacing: 1.5))), const SizedBox(height: 15), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ _NeonSizeButton(label: 'S', isSelected: localRadius == 3, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 3)), _NeonSizeButton(label: 'M', isSelected: localRadius == 4, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 4)), _NeonSizeButton(label: 'L', isSelected: localRadius == 5, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 5)), _NeonSizeButton(label: 'MAX', isSelected: localRadius == 6, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 6)), ], ), const SizedBox(height: 25), Divider(color: inkColor.withOpacity(0.3), thickness: 2.5), const SizedBox(height: 20), Text("TEMPO", style: _getTextStyle(themeType, TextStyle(fontSize: 14, fontWeight: FontWeight.w900, color: inkColor.withOpacity(0.6), letterSpacing: 1.5))), const SizedBox(height: 10), _NeonTimeSwitch(isTimeMode: localTimeMode, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localTimeMode = !localTimeMode)), const SizedBox(height: 35), Transform.rotate( angle: -0.02, child: GestureDetector( onTap: () { Navigator.pop(ctx); context.read().startNewGame(localRadius, vsCPU: isVsCPU, shape: localShape, timeMode: localTimeMode); Navigator.push(context, MaterialPageRoute(builder: (_) => const GameScreen())); }, child: CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.green.shade200, strokeColor: inkColor, seed: 300), child: Container( height: 65, width: double.infinity, alignment: Alignment.center, child: Text("AVVIA PARTITA", style: _getTextStyle(themeType, TextStyle(fontSize: 22, fontWeight: FontWeight.w900, letterSpacing: 3.0, color: inkColor))), ), ), ), ) ], ), ), ), ), ) : Container( decoration: BoxDecoration( gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [theme.background.withOpacity(0.95), theme.background.withOpacity(0.8)]), borderRadius: BorderRadius.circular(25), border: themeType == AppThemeType.cyberpunk ? null : Border.all(color: Colors.white.withOpacity(0.15), width: 1.5), boxShadow: themeType == AppThemeType.cyberpunk ? [] : [BoxShadow(color: Colors.black.withOpacity(0.5), blurRadius: 20, offset: const Offset(4, 10))], ), child: SingleChildScrollView( physics: const BouncingScrollPhysics(), child: Padding( padding: const EdgeInsets.all(20.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(isVsCPU ? "SFIDA IA" : "MODALITÀ LOCALE", style: _getTextStyle(themeType, TextStyle(fontSize: 24, fontWeight: FontWeight.w900, color: theme.text, letterSpacing: 2))), const SizedBox(height: 20), Text("FORMA ARENA", style: _getTextStyle(themeType, TextStyle(fontSize: 12, fontWeight: FontWeight.w900, color: theme.text.withOpacity(0.5), letterSpacing: 1.5))), const SizedBox(height: 10), Wrap( spacing: 10, runSpacing: 10, alignment: WrapAlignment.center, children: [ _NeonShapeButton(icon: Icons.diamond_outlined, label: 'Rombo', isSelected: localShape == ArenaShape.classic, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.classic)), _NeonShapeButton(icon: Icons.add, label: 'Croce', isSelected: localShape == ArenaShape.cross, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.cross)), _NeonShapeButton(icon: Icons.donut_large, label: 'Buco', isSelected: localShape == ArenaShape.donut, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.donut)), _NeonShapeButton(icon: Icons.hourglass_bottom, label: 'Clessidra', isSelected: localShape == ArenaShape.hourglass, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localShape = ArenaShape.hourglass)), _NeonShapeButton(icon: Icons.all_inclusive, label: 'Caos', isSelected: localShape == ArenaShape.chaos, theme: theme, themeType: themeType, isSpecial: true, isLocked: !isChaosUnlocked, onTap: () => setStateDialog(() => localShape = ArenaShape.chaos)), ], ), const SizedBox(height: 20), Divider(color: Colors.white.withOpacity(0.05), thickness: 2), const SizedBox(height: 20), Text("GRANDEZZA", style: _getTextStyle(themeType, TextStyle(fontSize: 12, fontWeight: FontWeight.w900, color: theme.text.withOpacity(0.5), letterSpacing: 1.5))), const SizedBox(height: 10), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ _NeonSizeButton(label: 'S', isSelected: localRadius == 3, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 3)), _NeonSizeButton(label: 'M', isSelected: localRadius == 4, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 4)), _NeonSizeButton(label: 'L', isSelected: localRadius == 5, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 5)), _NeonSizeButton(label: 'MAX', isSelected: localRadius == 6, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localRadius = 6)), ], ), const SizedBox(height: 20), Divider(color: Colors.white.withOpacity(0.05), thickness: 2), const SizedBox(height: 20), Text("TEMPO", style: _getTextStyle(themeType, TextStyle(fontSize: 12, fontWeight: FontWeight.w900, color: theme.text.withOpacity(0.5), letterSpacing: 1.5))), const SizedBox(height: 10), _NeonTimeSwitch(isTimeMode: localTimeMode, theme: theme, themeType: themeType, onTap: () => setStateDialog(() => localTimeMode = !localTimeMode)), const SizedBox(height: 30), SizedBox( width: double.infinity, height: 60, child: ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: isVsCPU ? Colors.purple.shade400 : theme.playerRed, foregroundColor: Colors.white, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))), onPressed: () { Navigator.pop(ctx); context.read().startNewGame(localRadius, vsCPU: isVsCPU, shape: localShape, timeMode: localTimeMode); Navigator.push(context, MaterialPageRoute(builder: (_) => const GameScreen())); }, child: const Text("AVVIA PARTITA", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w900, letterSpacing: 2)), ), ) ], ), ), ), ); if (themeType == AppThemeType.cyberpunk) { dialogContent = _AnimatedCyberBorder(child: dialogContent); } return Dialog(backgroundColor: Colors.transparent, insetPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 20), child: dialogContent); }, ); } ); } // --- MENU TUTORIAL (TESTO AGGIORNATO PER TETRAQ) --- void _showTutorialDialog() { showDialog( context: context, barrierColor: Colors.black.withOpacity(0.8), builder: (ctx) { final themeManager = ctx.watch(); final theme = themeManager.currentColors; final themeType = themeManager.currentThemeType; Color inkColor = const Color(0xFF111122); Widget dialogContent = themeType == AppThemeType.doodle ? Transform.rotate( angle: -0.01, child: CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.yellow.shade100, strokeColor: inkColor, seed: 400), child: Padding( padding: const EdgeInsets.all(25.0), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text("COME GIOCARE", style: _getTextStyle(themeType, TextStyle(fontSize: 28, fontWeight: FontWeight.w900, color: inkColor, letterSpacing: 2))), const SizedBox(height: 20), // TESTO MODIFICATO PER EVIDENZIARE IL POSIZIONAMENTO LIBERO _TutorialStep(icon: Icons.line_axis, text: "Lo scopo del gioco è chiudere i 4 lati di un quadrato per conquistare un punto.", themeType: themeType, inkColor: inkColor, theme: theme), const SizedBox(height: 15), // TESTO MODIFICATO PER L'EFFETTO TOROIDALE _TutorialStep(icon: Icons.all_out, text: "durante il gioco troverai dei bonus e dei malus impara a trarne profitto", themeType: themeType, inkColor: inkColor, theme: theme), const SizedBox(height: 15), // TESTO MODIFICATO PER LA VITTORIA E LE FORME GestureDetector( onTap: () => Navigator.pop(ctx), child: CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.red.shade200, strokeColor: inkColor, seed: 401), child: Container( height: 50, width: 150, alignment: Alignment.center, child: Text("HO CAPITO!", style: _getTextStyle(themeType, TextStyle(fontSize: 18, fontWeight: FontWeight.w900, color: inkColor))), ), ), ) ], ), ), ), ) : Container( padding: const EdgeInsets.all(25.0), decoration: BoxDecoration( gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [theme.background.withOpacity(0.95), theme.background.withOpacity(0.8)]), borderRadius: BorderRadius.circular(25), border: themeType == AppThemeType.cyberpunk ? null : Border.all(color: Colors.white.withOpacity(0.15), width: 1.5), boxShadow: themeType == AppThemeType.cyberpunk ? [] : [BoxShadow(color: Colors.black.withOpacity(0.5), blurRadius: 20, offset: const Offset(4, 10))], ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text("COME GIOCARE", style: _getTextStyle(themeType, TextStyle(fontSize: 24, fontWeight: FontWeight.w900, color: theme.text, letterSpacing: 2))), const SizedBox(height: 20), // TESTO MODIFICATO _TutorialStep(icon: Icons.line_axis, text: "Lo scopo del gioco è chiudere i 4 lati di un quadrato per conquistare un punto.", themeType: themeType, inkColor: inkColor, theme: theme), const SizedBox(height: 15), // TESTO MODIFICATO PER L'EFFETTO TOROIDALE _TutorialStep(icon: Icons.all_out, text: "durante il gioco troverai dei bonus e dei malus impara a trarne profitto", themeType: themeType, inkColor: inkColor, theme: theme), const SizedBox(height: 15), SizedBox( width: double.infinity, height: 50, child: ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: theme.playerBlue, foregroundColor: Colors.white, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15))), onPressed: () => Navigator.pop(ctx), child: const Text("CHIUDI", style: TextStyle(fontSize: 16, fontWeight: FontWeight.w900, letterSpacing: 2)), ), ) ], ), ); if (themeType == AppThemeType.cyberpunk) dialogContent = _AnimatedCyberBorder(child: dialogContent); return Dialog(backgroundColor: Colors.transparent, insetPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20), child: dialogContent); }, ); } Widget _buildCyberCard(Widget card, AppThemeType themeType) { if (themeType == AppThemeType.cyberpunk) { return _AnimatedCyberBorder(child: card); } return card; } @override Widget build(BuildContext context) { SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky); SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]); final themeManager = context.watch(); final themeType = themeManager.currentThemeType; final theme = themeManager.currentColors; Color inkColor = const Color(0xFF111122); String? bgImage; if (themeType == AppThemeType.wood) bgImage = 'assets/images/wood_bg.jpg'; if (themeType == AppThemeType.doodle) bgImage = 'assets/images/doodle_bg.jpg'; if (themeType == AppThemeType.cyberpunk) bgImage = 'assets/images/cyber_bg.jpg'; int wins = StorageService.instance.wins; int losses = StorageService.instance.losses; String playerName = StorageService.instance.playerName; if (playerName.isEmpty) playerName = "GUEST"; Widget uiContent = SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 20.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // --- HEADER --- Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ GestureDetector( onTap: _showNameDialog, child: Row( children: [ themeType == AppThemeType.doodle ? CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.white.withOpacity(0.8), strokeColor: inkColor, seed: 1, isCircle: true), child: SizedBox(width: 50, height: 50, child: Icon(Icons.person, color: inkColor, size: 30)), ) : Container( decoration: BoxDecoration(shape: BoxShape.circle, boxShadow: [BoxShadow(color: theme.playerBlue.withOpacity(0.3), blurRadius: 10, offset: const Offset(0, 4))]), child: CircleAvatar(backgroundColor: theme.playerBlue.withOpacity(0.2), radius: 25, child: Icon(Icons.person, color: theme.playerBlue, size: 30)), ), const SizedBox(width: 12), Text(playerName, style: _getTextStyle(themeType, TextStyle(color: themeType == AppThemeType.doodle ? inkColor : theme.text, fontSize: 26, fontWeight: FontWeight.w900, letterSpacing: 1.5, shadows: themeType == AppThemeType.doodle ? [] : [Shadow(color: Colors.black.withOpacity(0.5), offset: const Offset(1, 2), blurRadius: 2)]))), ], ), ), GestureDetector( onTap: () => Navigator.push(context, MaterialPageRoute(builder: (_) => const HistoryScreen())), child: themeType == AppThemeType.doodle ? Transform.rotate( angle: 0.04, child: CustomPaint( painter: _DoodleBackgroundPainter(fillColor: Colors.yellow.shade100, strokeColor: inkColor, seed: 2), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), child: Row( children: [ Icon(Icons.emoji_events, color: inkColor, size: 20), const SizedBox(width: 6), Text("$wins", style: _getTextStyle(themeType, TextStyle(color: inkColor, fontWeight: FontWeight.w900))), const SizedBox(width: 12), Icon(Icons.sentiment_very_dissatisfied, color: inkColor, size: 20), const SizedBox(width: 6), Text("$losses", style: _getTextStyle(themeType, TextStyle(color: inkColor, fontWeight: FontWeight.w900))), ], ), ), ), ) : Container( padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10), decoration: BoxDecoration( gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [theme.text.withOpacity(0.15), theme.text.withOpacity(0.02)]), borderRadius: BorderRadius.circular(20), border: Border.all(color: Colors.white.withOpacity(0.1), width: 1.5), boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.3), offset: const Offset(2, 4), blurRadius: 8), BoxShadow(color: Colors.white.withOpacity(0.05), offset: const Offset(-1, -1), blurRadius: 2)], ), child: Row( children: [ Icon(Icons.emoji_events, color: Colors.amber.shade600, size: 20), const SizedBox(width: 6), Text("$wins", style: _getTextStyle(themeType, const TextStyle(color: Colors.white, fontWeight: FontWeight.w900))), const SizedBox(width: 12), Icon(Icons.sentiment_very_dissatisfied, color: theme.playerRed.withOpacity(0.8), size: 20), const SizedBox(width: 6), Text("$losses", style: _getTextStyle(themeType, const TextStyle(color: Colors.white, fontWeight: FontWeight.w900))), ], ), ), ) ], ), const Spacer(), Center( child: Transform.rotate( angle: themeType == AppThemeType.doodle ? -0.04 : 0, child: Text( "TETRAQ", style: _getTextStyle(themeType, TextStyle( fontSize: 65, fontWeight: FontWeight.w900, color: themeType == AppThemeType.doodle ? inkColor : theme.text, letterSpacing: 10, shadows: themeType == AppThemeType.doodle ? [] : [ BoxShadow(color: Colors.black.withOpacity(0.6), offset: const Offset(3, 6), blurRadius: 8), BoxShadow(color: theme.playerBlue.withOpacity(0.4), offset: const Offset(0, 0), blurRadius: 20), ] )) ), ), ), const Spacer(), // --- MENU PRINCIPALE CON COLORI PASTELLO --- Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ _buildCyberCard( _FeatureCard( title: "ONLINE", subtitle: "Sfida il mondo", icon: Icons.public, color: Colors.lightBlue.shade200, theme: theme, themeType: themeType, isFeatured: true, onTap: () { Navigator.push(context, MaterialPageRoute(builder: (_) => const LobbyScreen())); }, ), themeType ), const SizedBox(height: 14), _buildCyberCard( _FeatureCard( title: "VS CPU", subtitle: "Allenati con l'IA", icon: Icons.smart_toy, color: Colors.purple.shade200, theme: theme, themeType: themeType, onTap: () => _showMatchSetupDialog(true), ), themeType ), const SizedBox(height: 14), _buildCyberCard( _FeatureCard( title: "LOCALE", subtitle: "Stesso schermo", icon: Icons.people_alt, color: Colors.red.shade200, theme: theme, themeType: themeType, onTap: () => _showMatchSetupDialog(false), ), themeType ), const SizedBox(height: 14), // --- NUOVO BOTTONE TUTORIAL --- _buildCyberCard( _FeatureCard( title: "TUTORIAL", subtitle: "Come giocare", icon: Icons.school, color: Colors.indigo.shade200, theme: theme, themeType: themeType, onTap: _showTutorialDialog, ), themeType ), const SizedBox(height: 14), _buildCyberCard( _FeatureCard( title: "TEMI", subtitle: "Personalizza", icon: Icons.palette, color: Colors.teal.shade200, theme: theme, themeType: themeType, onTap: () => Navigator.push(context, MaterialPageRoute(builder: (_) => const SettingsScreen())), ), themeType ), ], ), const SizedBox(height: 10), ], ), ), ); return Scaffold( backgroundColor: bgImage != null ? Colors.transparent : theme.background, body: Stack( children: [ Container(color: theme.background), if (bgImage != null) Positioned.fill( child: Image.asset(bgImage, fit: BoxFit.cover, alignment: Alignment.center), ), if (bgImage != null) Positioned.fill( child: themeType == AppThemeType.cyberpunk ? Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Colors.black.withOpacity(0.2), Colors.black.withOpacity(0.7)] ) ), ) : const SizedBox(), ), Positioned.fill(child: uiContent), ], ), ); } } // --- HELPER PER IL TESTO DEL TUTORIAL --- class _TutorialStep extends StatelessWidget { final IconData icon; final String text; final AppThemeType themeType; final Color inkColor; final ThemeColors theme; const _TutorialStep({required this.icon, required this.text, required this.themeType, required this.inkColor, required this.theme}); @override Widget build(BuildContext context) { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Icon(icon, color: themeType == AppThemeType.doodle ? inkColor : theme.playerBlue, size: 28), const SizedBox(width: 15), Expanded( child: Text(text, style: _getTextStyle(themeType, TextStyle(fontSize: 16, color: themeType == AppThemeType.doodle ? inkColor : theme.text.withOpacity(0.8), height: 1.3))), ), ], ); } } // --- FEATURE CARD --- class _FeatureCard extends StatelessWidget { final String title; final String subtitle; final IconData icon; final Color color; final ThemeColors theme; final AppThemeType themeType; final VoidCallback onTap; final bool isFeatured; const _FeatureCard({required this.title, required this.subtitle, required this.icon, required this.color, required this.theme, required this.themeType, required this.onTap, this.isFeatured = false}); @override Widget build(BuildContext context) { if (themeType == AppThemeType.doodle) { double tilt = (title.length % 2 == 0) ? -0.015 : 0.02; Color inkColor = const Color(0xFF111122); return Transform.rotate( angle: tilt, child: GestureDetector( onTap: onTap, child: CustomPaint( painter: _DoodleBackgroundPainter( fillColor: color, strokeColor: inkColor, seed: title.length * 5, ), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 22.0, vertical: 16.0), child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(icon, color: inkColor, size: 32), const SizedBox(width: 20), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text(title, style: _getTextStyle(themeType, TextStyle(color: inkColor, fontSize: 24, fontWeight: FontWeight.w900))), const SizedBox(height: 2), Text(subtitle, style: _getTextStyle(themeType, TextStyle(color: inkColor.withOpacity(0.8), fontSize: 14, fontWeight: FontWeight.bold))), ], ), ), Icon(Icons.chevron_right_rounded, color: inkColor.withOpacity(0.6), size: 32), ], ), ), ), ), ); } // --- STILE STANDARD --- return GestureDetector( onTap: onTap, child: Container( padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 14.0), decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: isFeatured ? [color.withOpacity(0.9), color.withOpacity(0.6)] : [theme.background.withOpacity(0.9), theme.background.withOpacity(0.5)], ), borderRadius: BorderRadius.circular(20), border: Border.all(color: Colors.white.withOpacity(isFeatured ? 0.3 : 0.1), width: 1.5), boxShadow: [ BoxShadow(color: Colors.black.withOpacity(0.6), offset: const Offset(0, 8), blurRadius: 15), BoxShadow(color: Colors.white.withOpacity(isFeatured ? 0.2 : 0.05), offset: const Offset(-1, -1), blurRadius: 5), ] ), child: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.white.withOpacity(0.3), Colors.white.withOpacity(0.05)], ), shape: BoxShape.circle, border: Border.all(color: Colors.white.withOpacity(0.2)), boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.2), blurRadius: 5, offset: const Offset(2, 4))] ), child: Icon(icon, color: isFeatured ? Colors.white : color, size: 26), ), const SizedBox(width: 20), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ Text(title, style: TextStyle(color: isFeatured ? Colors.white : theme.text, fontSize: 18, fontWeight: FontWeight.w900, shadows: [Shadow(color: Colors.black.withOpacity(0.5), offset: const Offset(1, 2), blurRadius: 2)])), const SizedBox(height: 2), Text(subtitle, style: TextStyle(color: isFeatured ? Colors.white.withOpacity(0.8) : theme.text.withOpacity(0.5), fontSize: 12, fontWeight: FontWeight.bold)), ], ), ), Icon(Icons.chevron_right_rounded, color: isFeatured ? Colors.white.withOpacity(0.7) : theme.text.withOpacity(0.3), size: 30), ], ), ), ); } } class _AnimatedCyberBorder extends StatefulWidget { final Widget child; const _AnimatedCyberBorder({required this.child}); @override State<_AnimatedCyberBorder> createState() => _AnimatedCyberBorderState(); } class _AnimatedCyberBorderState extends State<_AnimatedCyberBorder> with SingleTickerProviderStateMixin { late AnimationController _controller; @override void initState() { super.initState(); _controller = AnimationController(vsync: this, duration: const Duration(seconds: 3))..repeat(); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final theme = context.watch().currentColors; return AnimatedBuilder( animation: _controller, builder: (context, child) { return CustomPaint( painter: _CyberBorderPainter(animationValue: _controller.value, color1: theme.playerBlue, color2: theme.playerRed), child: Container( decoration: BoxDecoration(color: theme.background.withOpacity(0.9), borderRadius: BorderRadius.circular(20), boxShadow: [BoxShadow(color: theme.playerBlue.withOpacity(0.3), blurRadius: 25, spreadRadius: 2)]), padding: const EdgeInsets.all(3), child: widget.child, ), ); }, child: widget.child, ); } } class _CyberBorderPainter extends CustomPainter { final double animationValue; final Color color1; final Color color2; _CyberBorderPainter({required this.animationValue, required this.color1, required this.color2}); @override void paint(Canvas canvas, Size size) { final rect = Offset.zero & size; final RRect rrect = RRect.fromRectAndRadius(rect, const Radius.circular(20)); final Paint paint = Paint() ..shader = SweepGradient(colors: [color1, color2, color1, color2, color1], stops: const [0.0, 0.25, 0.5, 0.75, 1.0], transform: GradientRotation(animationValue * 2 * math.pi)).createShader(rect) ..style = PaintingStyle.stroke ..strokeWidth = 4.0 ..maskFilter = const MaskFilter.blur(BlurStyle.solid, 4); canvas.drawRRect(rrect, paint); } @override bool shouldRepaint(covariant _CyberBorderPainter oldDelegate) => oldDelegate.animationValue != animationValue; }