Files
FocusBuddy/lib/screens/home_screen.dart
2025-11-27 13:37:10 +08:00

334 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import '../l10n/app_localizations.dart';
import '../theme/app_colors.dart';
import '../theme/app_text_styles.dart';
import '../services/encouragement_service.dart';
import '../services/storage_service.dart';
import '../services/di.dart';
import 'focus_screen.dart';
import 'history_screen.dart';
import 'settings_screen.dart';
import 'profile_screen.dart';
/// Home Screen - Loads default duration from settings
class HomeScreen extends StatefulWidget {
final EncouragementService encouragementService;
const HomeScreen({
super.key,
required this.encouragementService,
});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int _defaultDuration = 25;
final StorageService _storageService = getIt<StorageService>();
@override
void initState() {
super.initState();
_loadDefaultDuration();
}
Future<void> _loadDefaultDuration() async {
try {
final duration = await SettingsScreen.getDefaultDuration();
setState(() {
_defaultDuration = duration;
});
} catch (e) {
// Use default duration if loading fails
setState(() {
_defaultDuration = 25;
});
}
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final progress = _storageService.getUserProgress();
return Scaffold(
backgroundColor: AppColors.background,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
children: [
// Points Card at the top
_buildPointsCard(context, progress),
const SizedBox(height: 32),
// App Title
Text(
l10n.appTitle,
style: AppTextStyles.appTitle,
),
const SizedBox(height: 60),
// Duration Display
Container(
padding: const EdgeInsets.symmetric(
horizontal: 32,
vertical: 16,
),
decoration: BoxDecoration(
color: AppColors.white,
borderRadius: BorderRadius.circular(16),
),
child: Text(
l10n.minutesValue(_defaultDuration, l10n.minutes(_defaultDuration)),
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 28,
fontWeight: FontWeight.w600,
color: AppColors.textPrimary,
),
),
),
const SizedBox(height: 60),
// Start Focusing Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FocusScreen(
durationMinutes: _defaultDuration,
encouragementService: widget.encouragementService,
),
),
);
// Reload duration and refresh points when returning
if (result == true || mounted) {
_loadDefaultDuration();
setState(() {}); // Refresh to show updated points
}
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(l10n.startFocusing),
const SizedBox(width: 8),
Icon(
Icons.play_arrow,
color: AppColors.white,
),
],
),
),
),
const SizedBox(height: 24),
// Helper Text
Text(
l10n.tapDistractionAnytime,
style: AppTextStyles.helperText,
textAlign: TextAlign.center,
),
const Spacer(),
// Bottom Navigation (simplified for MVP)
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const HistoryScreen(),
),
);
},
icon: const Icon(Icons.bar_chart),
label: Text(l10n.history),
),
TextButton.icon(
onPressed: () async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SettingsScreen(),
),
);
// Reload duration after settings
_loadDefaultDuration();
},
icon: const Icon(Icons.settings),
label: Text(l10n.settings),
),
],
),
],
),
),
),
);
}
/// Build points card widget
Widget _buildPointsCard(BuildContext context, progress) {
final l10n = AppLocalizations.of(context)!;
return GestureDetector(
onTap: () async {
// Navigate to ProfileScreen
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ProfileScreen(),
),
);
// Refresh points when returning from ProfileScreen
setState(() {});
},
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
AppColors.primary.withValues(alpha: 0.1),
AppColors.primary.withValues(alpha: 0.05),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: AppColors.primary.withValues(alpha: 0.2),
width: 1,
),
),
child: Row(
children: [
// Left side: Points and Level
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
// Points
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Text(
'',
style: TextStyle(fontSize: 20),
),
const SizedBox(width: 4),
Text(
'${progress.totalPoints}',
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 24,
fontWeight: FontWeight.bold,
color: AppColors.primary,
),
),
],
),
Text(
l10n.points,
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 12,
color: AppColors.textSecondary,
),
),
],
),
// Level
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Text(
'🎖️',
style: TextStyle(fontSize: 20),
),
const SizedBox(width: 4),
Text(
'Lv ${progress.level}',
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 24,
fontWeight: FontWeight.bold,
color: AppColors.primary,
),
),
],
),
Text(
l10n.level,
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 12,
color: AppColors.textSecondary,
),
),
],
),
],
),
),
// Right side: Check-in status
Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: progress.hasCheckedInToday
? AppColors.success.withValues(alpha: 0.1)
: AppColors.white.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(12),
),
child: Column(
children: [
Text(
progress.hasCheckedInToday ? '' : '📅',
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 4),
Text(
progress.hasCheckedInToday ? l10n.checked : l10n.checkIn,
style: TextStyle(
fontFamily: 'Nunito',
fontSize: 10,
color: progress.hasCheckedInToday
? AppColors.success
: AppColors.textSecondary,
fontWeight: FontWeight.w600,
),
),
if (progress.consecutiveCheckIns > 0)
Text(
'🔥 ${progress.consecutiveCheckIns}',
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 12,
fontWeight: FontWeight.bold,
color: AppColors.primary,
),
),
],
),
),
],
),
),
);
}
}