Files
BlockSeasons/lib/ui/screens/home_screen.dart
T
airkjw 1a028b9852 feat(ui): 7-day daily-reward popup on home
Presentational DailyRewardSheet (7 cells, today highlighted, reward icons
from the calendar table, claim + watch-ad-2x buttons). HomeScreen becomes a
ConsumerStatefulWidget that shows it once per mount via a post-frame guard;
the 2x path grants the doubled reward only if the rewarded ad was earned,
else the base reward. Guards the throwing saveRepositoryProvider default so
a repo-less mount is a no-op.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 19:26:43 +09:00

232 lines
8.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../game/models/season.dart';
import '../../game/models/stage.dart';
import '../../l10n/gen/app_localizations.dart';
import '../../state/providers.dart';
import '../widgets/banner_ad_slot.dart';
import '../widgets/daily_reward_sheet.dart';
import '../widgets/fade_route.dart';
import '../widgets/pressable_scale.dart';
import '../widgets/season_background.dart';
import 'game_screen.dart';
import 'season_map_screen.dart';
import 'settings_screen.dart';
class HomeScreen extends ConsumerStatefulWidget {
const HomeScreen({super.key});
@override
ConsumerState<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends ConsumerState<HomeScreen> {
static const _logoColors = [
Color(0xFFFF7EB3),
Color(0xFFFFD166),
Color(0xFF6FCDF5),
Color(0xFF7EDB9C),
];
bool _dailyChecked = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _maybeShowDailyReward());
}
/// Once per home mount, surface the 7-day login reward if it is claimable
/// today. Guards the deliberately-throwing [saveRepositoryProvider] default
/// so a repo-less mount (e.g. a widget test) is a no-op rather than a crash.
void _maybeShowDailyReward() {
if (_dailyChecked || !mounted) return;
_dailyChecked = true;
try {
ref.read(saveRepositoryProvider);
} catch (_) {
return;
}
final daily = ref.read(dailyRewardProvider);
if (!daily.claimable) return;
showDialog<void>(
context: context,
builder: (dialogContext) => Dialog(
child: DailyRewardSheet(
day: daily.day,
onClaim: (doubled) async {
final notifier = ref.read(dailyRewardProvider.notifier);
if (doubled) {
// Watch an ad to double; if no ad was earned the base reward is
// still granted so the player is never left empty-handed.
final earned = await ref.read(adServiceProvider).showRewarded();
await notifier.claim(doubled: earned);
} else {
await notifier.claim();
}
if (dialogContext.mounted) Navigator.of(dialogContext).pop();
},
),
),
);
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final streak = ref.watch(streakProvider);
final best = ref.watch(endlessBestProvider);
return Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
fit: StackFit.expand,
children: [
const SeasonBackground(theme: SeasonTheme.fallback),
SafeArea(
child: Stack(
children: [
Column(
children: [
Expanded(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_logoMark(),
const SizedBox(height: 18),
Text(
l10n.appTitle,
style: Theme.of(context)
.textTheme
.displaySmall
?.copyWith(fontWeight: FontWeight.w900),
),
if (streak.current > 0) ...[
const SizedBox(height: 10),
Chip(
avatar: const Icon(
Icons.local_fire_department,
color: Colors.deepOrange,
size: 20,
),
label: Text(
'${streak.current}',
style: Theme.of(context).textTheme.titleMedium,
),
),
],
const SizedBox(height: 44),
PressableScale(
child: FilledButton(
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 56, vertical: 18),
textStyle: Theme.of(context).textTheme.titleLarge,
),
onPressed: () {
if (!(ModalRoute.of(context)?.isCurrent ?? true)) return;
Navigator.of(context).push(
fadeRoute(const SeasonMapScreen()),
);
},
child: Text(l10n.adventure),
),
),
const SizedBox(height: 14),
PressableScale(
child: OutlinedButton(
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 40, vertical: 14),
textStyle: Theme.of(context).textTheme.titleMedium,
),
onPressed: () {
if (!(ModalRoute.of(context)?.isCurrent ?? true)) return;
ref.read(seasonFlowProvider.notifier).clear();
ref.read(analyticsProvider).endlessStart();
ref.read(gameSessionProvider.notifier).startStage(
StageConfig.endless(
seed: DateTime.now().millisecondsSinceEpoch,
),
);
Navigator.of(context).push(
fadeRoute(const GameScreen()),
);
},
child: Text(l10n.classic),
),
),
if (best > 0) ...[
const SizedBox(height: 10),
Text(
l10n.bestScore(best),
style: TextStyle(
color: Colors.white.withValues(alpha: 0.55),
),
),
],
],
),
),
),
const BannerAdSlot(),
],
),
Positioned(
top: 8,
right: 8,
child: IconButton(
icon: const Icon(Icons.settings, color: Colors.white70),
onPressed: () => Navigator.of(context).push(
fadeRoute(const SettingsScreen()),
),
),
),
],
),
),
],
),
);
}
Widget _logoMark() {
return SizedBox(
width: 96,
height: 96,
child: GridView.count(
crossAxisCount: 2,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
physics: const NeverScrollableScrollPhysics(),
children: [
for (final color in _logoColors)
DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(11),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color.lerp(color, Colors.white, 0.28)!,
color,
Color.lerp(color, Colors.black, 0.22)!,
],
),
boxShadow: [
BoxShadow(
color: color.withValues(alpha: 0.45),
blurRadius: 14,
),
],
),
),
],
),
);
}
}