153 lines
5.1 KiB
Dart
153 lines
5.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import '../../../core/config/app_config.dart';
|
|
import '../../../core/di/injection.dart';
|
|
import '../../../core/theme/app_colors.dart';
|
|
import '../../../l10n/app_strings.dart';
|
|
import '../../../shared/widgets/prediqo_logo.dart';
|
|
import '../../auth/bloc/auth_bloc.dart';
|
|
|
|
/// Branded splash while session and light bootstrap checks run.
|
|
class SplashScreen extends StatefulWidget {
|
|
const SplashScreen({super.key});
|
|
|
|
@override
|
|
State<SplashScreen> createState() => _SplashScreenState();
|
|
}
|
|
|
|
class _SplashScreenState extends State<SplashScreen> {
|
|
bool _navigated = false;
|
|
bool _updateAvailable = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_bootstrap();
|
|
}
|
|
|
|
Future<void> _bootstrap() async {
|
|
final minDelay = Future<void>.delayed(const Duration(milliseconds: 1200));
|
|
final versionCheck = _checkAppUpdate();
|
|
|
|
final authBloc = context.read<AuthBloc>();
|
|
Future<void> waitAuth() async {
|
|
if (authBloc.state.status == AuthStatus.loading ||
|
|
authBloc.state.status == AuthStatus.initial) {
|
|
await authBloc.stream.firstWhere(
|
|
(s) => s.status != AuthStatus.loading && s.status != AuthStatus.initial,
|
|
);
|
|
}
|
|
}
|
|
|
|
await Future.wait([minDelay, versionCheck, waitAuth()]);
|
|
if (!mounted || _navigated) return;
|
|
_navigated = true;
|
|
final signedIn = authBloc.state.isSignedIn;
|
|
context.go(signedIn ? '/home' : '/welcome');
|
|
}
|
|
|
|
Future<void> _checkAppUpdate() async {
|
|
final config = sl.isRegistered<AppConfig>() ? sl<AppConfig>() : null;
|
|
final latest = config?.latestAppVersion?.trim();
|
|
if (latest == null || latest.isEmpty) return;
|
|
try {
|
|
final info = await PackageInfo.fromPlatform();
|
|
if (!mounted) return;
|
|
setState(() => _updateAvailable = _isNewerVersion(latest, info.version));
|
|
} catch (_) {}
|
|
}
|
|
|
|
bool _isNewerVersion(String latest, String current) {
|
|
List<int> parts(String v) =>
|
|
v.split('.').map((e) => int.tryParse(e.replaceAll(RegExp(r'[^0-9]'), '')) ?? 0).toList();
|
|
final a = parts(latest);
|
|
final b = parts(current);
|
|
final len = a.length > b.length ? a.length : b.length;
|
|
for (var i = 0; i < len; i++) {
|
|
final x = i < a.length ? a[i] : 0;
|
|
final y = i < b.length ? b[i] : 0;
|
|
if (x > y) return true;
|
|
if (x < y) return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = context.strings;
|
|
final isDark = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
return Scaffold(
|
|
key: const Key('splash_screen'),
|
|
body: Container(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
decoration: BoxDecoration(gradient: AppColors.heroGradient(isDark)),
|
|
child: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
const Spacer(flex: 2),
|
|
PrediqoLogo(
|
|
size: 88,
|
|
showWordmark: false,
|
|
)
|
|
.animate()
|
|
.fadeIn(duration: 500.ms)
|
|
.scale(begin: const Offset(0.85, 0.85), end: const Offset(1, 1)),
|
|
const SizedBox(height: 20),
|
|
Text(
|
|
s.appName,
|
|
style: Theme.of(context).textTheme.headlineLarge?.copyWith(
|
|
fontWeight: FontWeight.w900,
|
|
letterSpacing: 0.5,
|
|
),
|
|
).animate().fadeIn(delay: 120.ms, duration: 400.ms),
|
|
const SizedBox(height: 10),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 40),
|
|
child: Text(
|
|
s.tagline,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
color: AppColors.textSecondary,
|
|
),
|
|
),
|
|
).animate().fadeIn(delay: 200.ms, duration: 400.ms),
|
|
if (_updateAvailable) ...[
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
s.updateAvailableHint,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
|
color: AppColors.warning,
|
|
),
|
|
),
|
|
],
|
|
const Spacer(flex: 2),
|
|
const SizedBox(
|
|
width: 28,
|
|
height: 28,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2.5,
|
|
color: AppColors.primary,
|
|
),
|
|
).animate().fadeIn(delay: 300.ms),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
s.splashLoading,
|
|
style: Theme.of(context).textTheme.labelMedium?.copyWith(
|
|
color: AppColors.textMuted,
|
|
),
|
|
),
|
|
const Spacer(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|