|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:loading_animation_widget/loading_animation_widget.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:siopas/models/user_model.dart';
|
|
|
|
import 'package:siopas/theme.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import '../providers/auth_provider.dart';
|
|
|
|
|
|
|
|
class SplashPage extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<SplashPage> createState() => _SplashPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SplashPageState extends State<SplashPage> {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
autoLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void autoLogin() async {
|
|
|
|
try {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
String? token = prefs.getString('token');
|
|
|
|
|
|
|
|
AuthProvider authProvider =
|
|
|
|
Provider.of<AuthProvider>(context, listen: false);
|
|
|
|
|
|
|
|
if (token != null) {
|
|
|
|
bool loginSuccess = await authProvider.autoLogin();
|
|
|
|
if (loginSuccess) {
|
|
|
|
Navigator.pushReplacementNamed(context, '/home');
|
|
|
|
print('Berhasil Login (otomatis splash screen)');
|
|
|
|
} else {
|
|
|
|
redirectToLogin();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
redirectToLogin();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
print('Terjadi kesalahan: $error');
|
|
|
|
redirectToLogin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void redirectToLogin() {
|
|
|
|
// Tambahkan penundaan 3 detik sebelum pindah ke halaman login
|
|
|
|
Future.delayed(Duration(seconds: 3), () {
|
|
|
|
if (mounted) {
|
|
|
|
Navigator.pushReplacementNamed(context, '/sign-in');
|
|
|
|
print('Pindah ke Halaman Login setelah penundaan');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
backgroundColor: backgroundColor1,
|
|
|
|
body: Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
width: 130,
|
|
|
|
height: 150,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
image: DecorationImage(
|
|
|
|
image: AssetImage('assets/img/logo_siopas.png'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
SizedBox(height: 16),
|
|
|
|
LoadingAnimationWidget.staggeredDotsWave(
|
|
|
|
color: Colors.indigo,
|
|
|
|
size: 40,
|
|
|
|
),
|
|
|
|
SizedBox(height: 16),
|
|
|
|
Text(
|
|
|
|
'Memuat...',
|
|
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|