pat widget a signed in user instead of email

This commit is contained in:
2024-07-25 16:44:07 +02:00
parent 14b92185e3
commit 50d7f09873

View File

@@ -1,18 +1,14 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:patient_manager/env/env.dart';
import 'package:patient_manager/main.dart'; import 'package:patient_manager/main.dart';
import 'package:supertokens_flutter/http.dart' as http;
import 'package:patient_manager/objects/appUser.dart'; import 'package:patient_manager/objects/appUser.dart';
import 'package:supertokens_flutter/supertokens.dart'; import 'package:supertokens_flutter/supertokens.dart';
class PatManAppDrawer extends StatefulWidget { class PatManAppDrawer extends StatefulWidget {
final String userEmail; final AppUser signedInUser;
final Image logo; final Image logo;
const PatManAppDrawer({ const PatManAppDrawer({
super.key, super.key,
required this.userEmail, required this.signedInUser,
required this.logo, required this.logo,
}); });
@@ -21,27 +17,27 @@ class PatManAppDrawer extends StatefulWidget {
} }
class _PatManAppDrawerState extends State<PatManAppDrawer> { class _PatManAppDrawerState extends State<PatManAppDrawer> {
String endpointUserData = "${AppEnviroment.baseApiUrl}/users/profile/"; //String endpointUserData = "${AppEnviroment.baseApiUrl}/users/profile/";
late Future<AppUser> signedInUser; //late Future<AppUser> signedInUser;
//late Image logo; //late Image logo;
Future<AppUser> getUserDetails() async { // Future<AppUser> getUserDetails() async {
//print("pat man drawer: " + endpointUserData + widget.userEmail); // //print("pat man drawer: " + endpointUserData + widget.userEmail);
var response = // var response =
await http.get(Uri.parse(endpointUserData + widget.userEmail)); // await http.get(Uri.parse(endpointUserData + widget.signedInUser));
// print(response.statusCode); // // print(response.statusCode);
//print(response.body); // //print(response.body);
if (response.statusCode == 200) { // if (response.statusCode == 200) {
//print("here"); // //print("here");
String body = response.body; // String body = response.body;
var decodedData = jsonDecode(body); // var decodedData = jsonDecode(body);
AppUser u = AppUser.fromJson(decodedData as Map<String, dynamic>); // AppUser u = AppUser.fromJson(decodedData as Map<String, dynamic>);
//print(u.email); // //print(u.email);
return u; // return u;
} else { // } else {
throw Exception("Error: GetUserData status code ${response.statusCode}"); // throw Exception("Error: GetUserData status code ${response.statusCode}");
} // }
} // }
Future<bool> signOut() async { Future<bool> signOut() async {
await SuperTokens.signOut(completionHandler: (error) { await SuperTokens.signOut(completionHandler: (error) {
@@ -52,7 +48,7 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
@override @override
void initState() { void initState() {
signedInUser = getUserDetails(); //signedInUser = getUserDetails();
super.initState(); super.initState();
} }
@@ -60,14 +56,6 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
// precacheImage( // precacheImage(
// MzanziInnovationHub.of(context)!.theme.logoImage().image, context); // MzanziInnovationHub.of(context)!.theme.logoImage().image, context);
return FutureBuilder(
future: signedInUser,
builder: (BuildContext context, AsyncSnapshot<AppUser> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.hasData) {
return Drawer( return Drawer(
//backgroundColor: MzanziInnovationHub.of(context)!.theme.primaryColor(), //backgroundColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
child: Stack(children: [ child: Stack(children: [
@@ -76,9 +64,7 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
children: [ children: [
DrawerHeader( DrawerHeader(
decoration: BoxDecoration( decoration: BoxDecoration(
color: MzanziInnovationHub.of(context)! color: MzanziInnovationHub.of(context)!.theme.secondaryColor(),
.theme
.secondaryColor(),
), ),
child: SizedBox( child: SizedBox(
height: 400, height: 400,
@@ -105,7 +91,7 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
), ),
const SizedBox(width: 15), const SizedBox(width: 15),
Text( Text(
"${snapshot.data?.username}", widget.signedInUser.username,
style: TextStyle( style: TextStyle(
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)! color: MzanziInnovationHub.of(context)!
@@ -125,9 +111,8 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
children: [ children: [
Icon( Icon(
Icons.home_outlined, Icons.home_outlined,
color: MzanziInnovationHub.of(context)! color:
.theme MzanziInnovationHub.of(context)!.theme.secondaryColor(),
.secondaryColor(),
), ),
const SizedBox(width: 25.0), const SizedBox(width: 25.0),
Text( Text(
@@ -152,9 +137,8 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
children: [ children: [
Icon( Icon(
Icons.perm_identity, Icons.perm_identity,
color: MzanziInnovationHub.of(context)! color:
.theme MzanziInnovationHub.of(context)!.theme.secondaryColor(),
.secondaryColor(),
), ),
const SizedBox(width: 25.0), const SizedBox(width: 25.0),
Text( Text(
@@ -172,7 +156,7 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
//signedInUser = snapshot.data!; //signedInUser = snapshot.data!;
//print("PatManAppDrawer: ${signedInUser.runtimeType}"); //print("PatManAppDrawer: ${signedInUser.runtimeType}");
Navigator.of(context) Navigator.of(context)
.pushNamed('/profile', arguments: snapshot.data); .pushNamed('/profile', arguments: widget.signedInUser);
}, },
), ),
ListTile( ListTile(
@@ -181,9 +165,8 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
children: [ children: [
Icon( Icon(
Icons.logout, Icons.logout,
color: MzanziInnovationHub.of(context)! color:
.theme MzanziInnovationHub.of(context)!.theme.secondaryColor(),
.secondaryColor(),
), ),
const SizedBox(width: 25.0), const SizedBox(width: 25.0),
Text( Text(
@@ -216,16 +199,13 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
child: IconButton( child: IconButton(
onPressed: () { onPressed: () {
setState(() { setState(() {
if (MzanziInnovationHub.of(context)?.theme.mode == if (MzanziInnovationHub.of(context)?.theme.mode == "Dark") {
"Dark") {
//darkm = !darkm; //darkm = !darkm;
MzanziInnovationHub.of(context)! MzanziInnovationHub.of(context)!.changeTheme(ThemeMode.light);
.changeTheme(ThemeMode.light);
//print("Dark Mode: $darkm"); //print("Dark Mode: $darkm");
} else { } else {
//darkm = !darkm; //darkm = !darkm;
MzanziInnovationHub.of(context)! MzanziInnovationHub.of(context)!.changeTheme(ThemeMode.dark);
.changeTheme(ThemeMode.dark);
//print("Dark Mode: $darkm"); //print("Dark Mode: $darkm");
} }
Navigator.of(context).pushNamed('/home'); Navigator.of(context).pushNamed('/home');
@@ -233,20 +213,12 @@ class _PatManAppDrawerState extends State<PatManAppDrawer> {
}, },
icon: Icon( icon: Icon(
Icons.light_mode, Icons.light_mode,
color: color: MzanziInnovationHub.of(context)!.theme.primaryColor(),
MzanziInnovationHub.of(context)!.theme.primaryColor(),
size: 35, size: 35,
), ),
), ),
), ),
]), ]),
); );
} else {
return const Center(
child: Text("Error Loading profile"),
);
}
},
);
} }
} }