84 lines
2.3 KiB
Dart
84 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
|
import 'onboarding_screen.dart';
|
|
import 'main_screen.dart';
|
|
import '../../core/di/injection_container.dart';
|
|
import '../../data/datasources/user_local_data_source.dart';
|
|
|
|
class SplashScreen extends StatefulWidget {
|
|
const SplashScreen({super.key});
|
|
|
|
@override
|
|
State<SplashScreen> createState() => _SplashScreenState();
|
|
}
|
|
|
|
class _SplashScreenState extends State<SplashScreen> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
FlutterNativeSplash.remove();
|
|
_checkTokenAndNavigate();
|
|
}
|
|
|
|
Future<void> _checkTokenAndNavigate() async {
|
|
try {
|
|
// Wait for splash screen display
|
|
await Future.delayed(const Duration(seconds: 2));
|
|
|
|
if (!mounted) return;
|
|
|
|
// Check if token exists in cache
|
|
final token = await sl<UserLocalDataSource>().getCachedUserToken();
|
|
|
|
if (token != null && token.isNotEmpty) {
|
|
// Token exists — go to MainPage (theme already loaded in main.dart)
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const MainPage()),
|
|
);
|
|
} else {
|
|
// No token, navigate to OnboardingScreen
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const OnboardingScreen()),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
debugPrint('Error in _checkTokenAndNavigate: $e');
|
|
if (mounted) {
|
|
// Fallback to onboarding if anything fails
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const OnboardingScreen()),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Container(
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(
|
|
image: AssetImage("assets/images/splash.png"),
|
|
fit: BoxFit.cover,
|
|
),
|
|
),
|
|
// child: Center(child: Image.asset("assets/images/logo.png", width: 200)),
|
|
child: const Center(
|
|
child: Text(
|
|
'LOGO',
|
|
style: TextStyle(
|
|
fontSize: 36,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
letterSpacing: 2,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|