baligh/lib/screens/profile_screen.dart
Abdullah Salah e3ea8335d1 First Commit
2024-12-26 09:05:13 +03:00

145 lines
4.0 KiB
Dart

import 'package:baligh/screens/login_screen.dart';
import 'package:baligh/widgets/app_toast.dart';
import 'package:baligh/widgets/custom_button.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
enum ProfileState { loading, error, loaded }
class ProfileScreen extends StatefulWidget {
const ProfileScreen({super.key});
@override
State<ProfileScreen> createState() => _ProfileScreenState();
}
class _ProfileScreenState extends State<ProfileScreen> {
ProfileState _profileState = ProfileState.loading;
Map<String, dynamic>? _userData;
bool isLogOutLoading = false;
@override
void initState() {
super.initState();
_loadUserData();
}
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
"المعلومات الشخصية",
style: Theme.of(context).textTheme.headlineLarge,
),
backgroundColor: Colors.transparent,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(15),
child: _buildContent(),
),
),
),
);
}
Future<void> _loadUserData() async {
setState(() {
_profileState = ProfileState.loading;
});
try {
final user = FirebaseAuth.instance.currentUser;
if (user == null) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
(x) => false);
}
// get user data from firestore in a coolection called 'users'
final userData = await FirebaseFirestore.instance
.collection('users')
.doc(user!.uid)
.get();
setState(() {
_userData = userData.data();
_profileState = ProfileState.loaded;
});
} catch (e) {
setState(() {
_profileState = ProfileState.error;
});
}
}
Future<void> logOut() async {
setState(() {
isLogOutLoading = true;
});
try {
await FirebaseAuth.instance.signOut();
setState(() {
isLogOutLoading = false;
});
if (!mounted) return;
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const LoginScreen()),
(x) => false);
} catch (e) {
setState(() {
isLogOutLoading = false;
});
BotToast.showCustomText(toastBuilder: (_) {
return const AppToast(text: "حدث خطأ ما");
});
}
}
Widget _buildContent() {
switch (_profileState) {
case ProfileState.loading:
return const CircularProgressIndicator();
case ProfileState.error:
return const Text("حدث خطأ ما");
case ProfileState.loaded:
return ListView(
children: [
Text('الاسم: ${_userData!['name']}'),
const SizedBox(height: 5),
Text('رقم الهاتف: 0${_userData!['phone'].toString().split("+964").last}'),
const SizedBox(height: 5),
Text('العمر: ${_userData!['age']}'),
const SizedBox(height: 5),
Text('رقم البطاقة الوطنية: ${_userData!['nationalIdNumber']}'),
const SizedBox(height: 5),
Text('المدرسة: ${_userData!['schoolName']}'),
const SizedBox(height: 5),
Text('المرحلة الدراسية: ${_userData!['stage']}'),
const SizedBox(height: 5),
Text('العنوان: ${_userData!['address']}'),
const SizedBox(height: 20),
CustomButton(
label: "تسجيل الخروج",
isElevated: true,
onPressed: logOut,
isLoading: isLogOutLoading,
),
],
);
default:
return const SizedBox.shrink();
}
}
}