MIF_E31230892/sim_mobile/lib/features/splash/splash_screen.dart

101 lines
2.6 KiB
Dart

// lib/features/splash/splash_screen.dart
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../core/api/api_service.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
final _api = ApiService();
@override
void initState() {
super.initState();
_checkAuth();
}
Future<void> _checkAuth() async {
// Minimal delay untuk UX (500ms saja, tidak terlalu lama)
await Future.delayed(const Duration(milliseconds: 500));
final prefs = await SharedPreferences.getInstance();
final token = prefs.getString('token');
if (!mounted) return;
if (token == null) {
// Tidak ada token → Ke Login
Navigator.pushReplacementNamed(context, '/login');
} else {
// Ada token → Validasi ke server
final isValid = await _api.isTokenValid();
if (!mounted) return;
if (isValid) {
// Token valid → Ke Dashboard
Navigator.pushReplacementNamed(context, '/dashboard');
} else {
// Token invalid → Clear & ke Login
await prefs.clear();
Navigator.pushReplacementNamed(context, '/login');
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.deepPurple,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Logo atau Icon
Icon(
Icons.school_rounded,
size: 80,
color: Colors.white,
),
const SizedBox(height: 20),
// Nama App
const Text(
'SIM-PKPPS',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: 2,
),
),
const SizedBox(height: 8),
const Text(
'Mobile',
style: TextStyle(
fontSize: 16,
color: Colors.white70,
letterSpacing: 1,
),
),
const SizedBox(height: 40),
// Loading indicator
const SizedBox(
width: 30,
height: 30,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
strokeWidth: 3,
),
),
],
),
),
);
}
}