inventory_mobile/lib/screens/splash_screen.dart
2026-01-13 06:26:37 +08:00

167 lines
5.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:inventory_system/services/auth_service.dart';
import 'package:inventory_system/services/session_manager.dart';
import 'package:inventory_system/screens/admin/home_screen/home_screen.dart';
import 'package:inventory_system/screens/user/home_screen/home_screen_user.dart';
class SplashScreen extends StatefulWidget {
const SplashScreen({super.key});
@override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _fadeAnimation;
late Animation<double> _scaleAnimation;
late Animation<Offset> _slideAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 2000),
vsync: this,
);
_fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.6, curve: Curves.easeOut)),
);
_scaleAnimation = Tween<double>(begin: 0.5, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.8, curve: Curves.elasticOut)),
);
_slideAnimation =
Tween<Offset>(begin: const Offset(0, 0.5), end: Offset.zero).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.3, 1.0, curve: Curves.easeOutBack)),
);
_controller.forward();
_checkSession();
}
Future<void> _checkSession() async {
// Wait for the animation to be mostly done so the splash isn't skipped too fast
await Future.delayed(const Duration(seconds: 3));
final prefs = await SharedPreferences.getInstance();
final rememberMe = prefs.getBool('remember_me') ?? false;
final email = prefs.getString('saved_email');
final password = prefs.getString('saved_password');
if (rememberMe && email != null && password != null && email.isNotEmpty && password.isNotEmpty) {
// Attempt silent login
try {
final authService = AuthService();
final result = await authService.signIn(usernameOrEmail: email, password: password);
if (!mounted) return;
if (result['success'] == true) {
SessionManager.instance.startSession(result['data']);
final bool isAdmin = result['data']?['isAdmin'] ?? false;
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => isAdmin ? const HomeScreen() : const UserHomeScreen(),
),
);
return;
}
} catch (e) {
// Login failed, proceed to login screen
}
}
if (!mounted) return;
Navigator.pushReplacementNamed(context, '/login');
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF0D47A1),
Color(0xFF1976D2),
Color(0xFF42A5F5),
Color(0xFF90CAF9),
],
stops: [0.0, 0.3, 0.7, 1.0],
),
),
child: SafeArea(
child: Center(
child: FadeTransition(
opacity: _fadeAnimation,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
ScaleTransition(
scale: _scaleAnimation,
child: Image.asset(
'assets/images/logo.png',
width: 380,
height: 380,
fit: BoxFit.contain,
gaplessPlayback: true,
),
),
Transform.translate(
offset: const Offset(0, -140),
child: SlideTransition(
position: _slideAnimation,
child: Text(
'INVENTORY\nSYSTEM',
textAlign: TextAlign.center,
style: GoogleFonts.roboto(
fontSize: 36,
fontWeight: FontWeight.w900,
letterSpacing: 4,
color: Colors.white,
height: 1.0,
shadows: [
Shadow(
offset: const Offset(2, 2),
blurRadius: 6,
color: Colors.black.withOpacity(0.4),
),
],
),
),
),
),
],
),
),
),
),
),
);
}
}