| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import '../l10n/app_localizations.dart';
- import '../utils/colors.dart';
- import '../utils/haptic_utils.dart';
- import '../utils/settings_manager.dart';
- import '../utils/score_manager.dart';
- import '../utils/theme_notifier.dart';
- import '../game/audio/audio_manager.dart';
- import 'game_screen.dart';
- import 'settings_screen.dart';
- import 'stats_screen.dart';
- import 'components/animated_background.dart';
- import 'components/tutorial_overlay.dart';
- class MainMenu extends StatefulWidget {
- const MainMenu({super.key});
- @override
- State<MainMenu> createState() => _MainMenuState();
- }
- class _MainMenuState extends State<MainMenu> {
- bool _showTutorial = false;
- int _todayScore = 0;
- @override
- void initState() {
- super.initState();
- _checkTutorial();
- _initializeAndLoadScore();
- // Start menu music when entering main menu
- WidgetsBinding.instance.addPostFrameCallback((_) {
- AudioManager().playMenuMusic();
- });
- }
- @override
- void didChangeDependencies() {
- super.didChangeDependencies();
- // Refresh score when orientation changes or when returning from other screens
- _loadTodayScore();
- }
- void _initializeAndLoadScore() async {
- // Initialize score manager first
- await ScoreManager.initialize();
- _loadTodayScore();
- }
- void _loadTodayScore() {
- setState(() {
- _todayScore = ScoreManager.todayScore;
- });
- }
- void _checkTutorial() {
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (!SettingsManager.isTutorialShown) {
- setState(() {
- _showTutorial = true;
- });
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return ThemeAwareBuilder(
- builder: (context, theme) {
- return Scaffold(
- backgroundColor: ZenColors.currentAppBackground,
- body: OrientationBuilder(
- builder: (context, orientation) {
- return AnimatedBackground(
- child: Stack(
- children: [
- SafeArea(
- child: Padding(
- padding: const EdgeInsets.all(20.0),
- child:
- orientation == Orientation.portrait
- ? _buildPortraitLayout()
- : _buildLandscapeLayout(),
- ),
- ),
- // Tutorial overlay
- if (_showTutorial)
- TutorialOverlay(
- onComplete: () {
- setState(() {
- _showTutorial = false;
- });
- },
- ),
- ],
- ),
- );
- },
- ),
- );
- },
- );
- }
- Widget _buildPortraitLayout() {
- return Column(
- children: [
- // Main content
- Expanded(
- child: SingleChildScrollView(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const SizedBox(height: 20),
- _buildGameTitle(),
- const SizedBox(height: 40),
- _buildTodayScore(),
- const SizedBox(height: 40),
- _buildMenuButtons(),
- const SizedBox(height: 40),
- _buildSettingsHint(),
- const SizedBox(height: 20),
- ],
- ),
- ),
- ),
- ],
- );
- }
- Widget _buildLandscapeLayout() {
- return Row(
- children: [
- // Left side - Title and score
- Expanded(
- flex: 2,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- _buildGameTitle(),
- const SizedBox(height: 20),
- _buildTodayScore(),
- const SizedBox(height: 20),
- _buildSettingsHint(),
- ],
- ),
- ),
- // Right side - Menu buttons
- Expanded(
- flex: 3,
- child: SingleChildScrollView(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const SizedBox(height: 20),
- _buildResponsiveMenuButtons(),
- const SizedBox(height: 20),
- ],
- ),
- ),
- ),
- ],
- );
- }
- Widget _buildGameTitle() {
- return Column(
- children: [
- // Game Title
- Text(
- 'ZenTap',
- style: TextStyle(
- color: ZenColors.currentPrimaryText,
- fontSize: 48,
- fontWeight: FontWeight.bold,
- letterSpacing: 2.0,
- ),
- ),
- const SizedBox(height: 10),
- // Subtitle
- Text(
- AppLocalizations.of(context)!.appSubtitle,
- style: TextStyle(
- color: ZenColors.currentSecondaryText,
- fontSize: 18,
- fontStyle: FontStyle.italic,
- ),
- ),
- ],
- );
- }
- Widget _buildTodayScore() {
- return Container(
- padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
- decoration: BoxDecoration(
- color: ZenColors.black.withValues(alpha: 0.3),
- borderRadius: BorderRadius.circular(15),
- border: Border.all(
- color: ZenColors.currentButtonBackground.withValues(alpha: 0.5),
- width: 1,
- ),
- ),
- child: Text(
- AppLocalizations.of(context)!.todayRelaxationPoints(_todayScore),
- style: TextStyle(
- color: ZenColors.currentPrimaryText,
- fontSize: 18,
- fontWeight: FontWeight.w600,
- ),
- ),
- );
- }
- Widget _buildMenuButtons() {
- final l10n = AppLocalizations.of(context)!;
- return Column(
- children: [
- // Play Button
- _buildMenuButton(
- context,
- l10n.play,
- l10n.playDescription,
- Icons.play_arrow,
- () => _navigateToGame(context, false),
- ),
- const SizedBox(height: 20),
- // Zen Mode Button
- _buildMenuButton(
- context,
- l10n.zenMode,
- l10n.zenModeDescription,
- Icons.self_improvement,
- () => _navigateToGame(context, true),
- ),
- const SizedBox(height: 20),
- // Stats Button
- _buildMenuButton(
- context,
- l10n.stats,
- l10n.statisticsDescription,
- Icons.analytics,
- _openStats,
- ),
- const SizedBox(height: 20),
- // Settings Button
- _buildMenuButton(
- context,
- l10n.settings,
- 'Customize your experience',
- Icons.settings,
- _openSettings,
- ),
- const SizedBox(height: 20),
- // Exit Button
- _buildExitButton(
- context,
- l10n.exit,
- l10n.exitDescription,
- Icons.exit_to_app,
- _exitApp,
- ),
- ],
- );
- }
- Widget _buildSettingsHint() {
- return Text(
- AppLocalizations.of(context)!.tapToFeelCalm,
- style: TextStyle(color: ZenColors.currentMutedText, fontSize: 14),
- );
- }
- Widget _buildResponsiveMenuButtons() {
- return OrientationBuilder(
- builder: (context, orientation) {
- final buttonSpacing = orientation == Orientation.portrait ? 20.0 : 12.0;
- final l10n = AppLocalizations.of(context)!;
- return Column(
- children: [
- // Play Button
- _buildMenuButton(
- context,
- l10n.play,
- l10n.playDescription,
- Icons.play_arrow,
- () => _navigateToGame(context, false),
- ),
- SizedBox(height: buttonSpacing),
- // Zen Mode Button
- _buildMenuButton(
- context,
- l10n.zenMode,
- l10n.zenModeDescription,
- Icons.self_improvement,
- () => _navigateToGame(context, true),
- ),
- SizedBox(height: buttonSpacing),
- // Stats Button
- _buildMenuButton(
- context,
- l10n.stats,
- l10n.statisticsDescription,
- Icons.analytics,
- _openStats,
- ),
- SizedBox(height: buttonSpacing),
- // Settings Button
- _buildMenuButton(
- context,
- l10n.settings,
- 'Customize your experience',
- Icons.settings,
- _openSettings,
- ),
- SizedBox(height: buttonSpacing),
- // Exit Button
- _buildExitButton(
- context,
- l10n.exit,
- l10n.exitDescription,
- Icons.exit_to_app,
- _exitApp,
- ),
- ],
- );
- },
- );
- }
- Widget _buildMenuButton(
- BuildContext context,
- String title,
- String subtitle,
- IconData icon,
- VoidCallback onPressed,
- ) {
- return Container(
- width: double.infinity,
- margin: const EdgeInsets.symmetric(horizontal: 20),
- child: ElevatedButton(
- onPressed: onPressed,
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.currentButtonBackground,
- foregroundColor: ZenColors.currentButtonText,
- padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 30),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(15),
- ),
- elevation: 8,
- ),
- child: Row(
- children: [
- Icon(icon, size: 32, color: ZenColors.currentButtonText),
- const SizedBox(width: 20),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- title,
- style: TextStyle(
- fontSize: 22,
- fontWeight: FontWeight.bold,
- color: ZenColors.currentButtonText,
- ),
- ),
- const SizedBox(height: 4),
- Text(
- subtitle,
- style: TextStyle(
- fontSize: 14,
- color: ZenColors.currentButtonText.withValues(alpha: 0.8),
- ),
- ),
- ],
- ),
- ),
- Icon(
- Icons.arrow_forward_ios,
- color: ZenColors.currentButtonText.withValues(alpha: 0.7),
- size: 20,
- ),
- ],
- ),
- ),
- );
- }
- Widget _buildExitButton(
- BuildContext context,
- String title,
- String subtitle,
- IconData icon,
- VoidCallback onPressed,
- ) {
- return Container(
- width: double.infinity,
- margin: const EdgeInsets.symmetric(horizontal: 20),
- child: ElevatedButton(
- onPressed: onPressed,
- style: ElevatedButton.styleFrom(
- backgroundColor: ZenColors.red.withValues(alpha: 0.8),
- foregroundColor: ZenColors.white,
- padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 30),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(15),
- ),
- elevation: 8,
- ),
- child: Row(
- children: [
- Icon(icon, size: 32, color: ZenColors.white),
- const SizedBox(width: 20),
- Expanded(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- title,
- style: const TextStyle(
- fontSize: 22,
- fontWeight: FontWeight.bold,
- color: ZenColors.white,
- ),
- ),
- const SizedBox(height: 4),
- Text(
- subtitle,
- style: TextStyle(
- fontSize: 14,
- color: ZenColors.white.withValues(alpha: 0.8),
- ),
- ),
- ],
- ),
- ),
- Icon(
- Icons.arrow_forward_ios,
- color: ZenColors.white.withValues(alpha: 0.7),
- size: 20,
- ),
- ],
- ),
- ),
- );
- }
- void _exitApp() async {
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 50);
- }
- // Close the app
- SystemNavigator.pop();
- }
- void _openSettings() async {
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 50);
- }
- if (!mounted) return;
- Navigator.of(
- context,
- ).push(MaterialPageRoute(builder: (context) => const SettingsScreen()));
- }
- void _openStats() async {
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 50);
- }
- if (!mounted) return;
- final result = await Navigator.of(
- context,
- ).push(MaterialPageRoute(builder: (context) => const StatsScreen()));
- // Refresh score when returning from stats
- if (result == true) {
- _loadTodayScore();
- }
- }
- void _navigateToGame(BuildContext ctx, bool isZenMode) async {
- if (SettingsManager.isHapticsEnabled) {
- await HapticUtils.vibrate(duration: 50);
- }
- if (!mounted) return;
- final result = await Navigator.of(context).push(
- MaterialPageRoute(builder: (context) => GameScreen(isZenMode: isZenMode)),
- );
- // Refresh score when returning from game
- if (mounted && result == true) {
- _loadTodayScore();
- }
- }
- }
|