80 lines
2.1 KiB
Dart
80 lines
2.1 KiB
Dart
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:lba/screens/auth/login_page.dart';
|
|
import 'package:lba/screens/auth/onboarding_page.dart';
|
|
import 'package:lba/screens/mains/navigation/navigation.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class SimpleAuthGate extends StatefulWidget {
|
|
const SimpleAuthGate({super.key});
|
|
|
|
@override
|
|
State<SimpleAuthGate> createState() => _SimpleAuthGateState();
|
|
}
|
|
|
|
class _SimpleAuthGateState extends State<SimpleAuthGate> {
|
|
bool? _isFirstTime;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_checkFirstTime();
|
|
}
|
|
|
|
Future<void> _checkFirstTime() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final hasSeenOnboarding = prefs.getBool('hasSeenOnboarding') ?? false;
|
|
setState(() {
|
|
_isFirstTime = !hasSeenOnboarding;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_isFirstTime == null) {
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
|
|
return StreamBuilder<User?>(
|
|
stream: FirebaseAuth.instance.authStateChanges(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.error, size: 64, color: Colors.red),
|
|
SizedBox(height: 16),
|
|
Text('An error occurred. Please try again later.'),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
final user = snapshot.data;
|
|
final isLoggedIn = user != null;
|
|
|
|
if (isLoggedIn) {
|
|
return const MainScreen();
|
|
}
|
|
|
|
return _isFirstTime! ? const OnboardingPage() : const LoginPage();
|
|
},
|
|
);
|
|
}
|
|
}
|