Advertisement
Guest User

Flutter Error

a guest
May 17th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.45 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3.  
  4. import 'package:app/auth/login_or_register.dart';
  5. import 'package:app/pages/shared/login/login_page.dart';
  6. import 'package:app/pages/shared/widgets/button_global.dart';
  7. import 'package:app/pages/shared/widgets/custom_snackbar.dart';
  8. import 'package:app/pages/shared/widgets/text_form_global.dart';
  9. import 'package:app/pages/shared/widgets/text_global.dart';
  10. import 'package:app/utils/dio_routes.dart';
  11. import 'package:app/utils/global.colors.dart';
  12. import 'package:dio/dio.dart';
  13. import 'package:firebase_core/firebase_core.dart';
  14. import 'package:flutter/foundation.dart';
  15. import 'package:flutter/material.dart';
  16. import 'package:firebase_auth/firebase_auth.dart';
  17.  
  18. class SignUpPage extends StatefulWidget {
  19. final void Function()? onTap;
  20.  
  21. const SignUpPage({super.key, required this.onTap});
  22.  
  23. @override
  24. _SignUpPageState createState() => _SignUpPageState();
  25. }
  26.  
  27. class _SignUpPageState extends State<SignUpPage> {
  28. bool _isLoading = false;
  29. bool _redirecting = false;
  30. late final TextEditingController _emailController = TextEditingController();
  31. late final TextEditingController _passwordController =
  32. TextEditingController();
  33. late final TextEditingController _firstNameController =
  34. TextEditingController();
  35. late final TextEditingController _lastNameController =
  36. TextEditingController();
  37.  
  38.  
  39. /* void register() async {
  40. // show loading screen
  41. showDialog(
  42. context: context,
  43. builder: (context) => const Center(
  44. child: CircularProgressIndicator(),
  45. ),
  46. );
  47.  
  48. // try create the user
  49. try {
  50. UserCredential? userCredential = await FirebaseAuth.instance
  51. .createUserWithEmailAndPassword(
  52. email: _emailController.text, password: _passwordController.text);
  53.  
  54. if (mounted) {
  55. // create user document and send it to api
  56. await createUserData(context, userCredential.user!.uid);
  57. Navigator.pop(context);
  58. } else {
  59. // you can't use the context
  60. }
  61.  
  62. // pop loading circle
  63. } on FirebaseException catch (e) {
  64. // pop loading circle
  65. //Navigator.pop(context);
  66.  
  67. // display error message to user
  68. displayNotification(context, e.code);
  69. }
  70. }
  71.  
  72. Future createUserData(BuildContext context, String uid) async {
  73. try {
  74. var data = {
  75. "userId": uid,
  76. "clientName": _firstNameController.text.toString(),
  77. "email": _emailController.text.toString(),
  78. };
  79.  
  80. Response response =
  81. await Dio().post(createAccountEndpoint, data: jsonEncode(data));
  82.  
  83. if (response.statusCode == 200) {
  84. // display error message
  85. displayNotification(context, "Your account was created successfully!");
  86. Navigator.push(
  87. context,
  88. MaterialPageRoute(builder: (context) => const LoginOrRegister()),
  89. );
  90. } else {
  91. displayNotification(context, "Error!");
  92. }
  93. } on DioException catch (e) {
  94. print("Create Account ERROR -> ${e.message}");
  95. }
  96. } */
  97.  
  98. void register() async {
  99. // show loading screen
  100. showDialog(
  101. context: context,
  102. builder: (context) => const Center(
  103. child: CircularProgressIndicator(),
  104. ),
  105. );
  106.  
  107. // try create the user
  108. final result = await createUser();
  109.  
  110. displayNotification(context, result.values.first);
  111.  
  112. Navigator.pop(context);
  113.  
  114. if (result.keys.first) {
  115. Navigator.push(
  116. context,
  117. MaterialPageRoute(builder: (context) => const LoginOrRegister()),
  118. );
  119. }
  120. }
  121.  
  122. Future<Map<bool, String>> createUser() async {
  123. try {
  124. UserCredential? userCredential = await FirebaseAuth.instance
  125. .createUserWithEmailAndPassword(
  126. email: _emailController.text, password: _passwordController.text);
  127.  
  128. final uid = userCredential.user!.uid;
  129.  
  130. var data = {
  131. "userId": uid,
  132. "clientName": _firstNameController.text.toString(),
  133. "email": _emailController.text.toString(),
  134. };
  135.  
  136. Response response =
  137. await Dio().post(createAccountEndpoint, data: jsonEncode(data));
  138.  
  139. if (response.statusCode == 200) {
  140. return {true: "Your account was created successfully!"};
  141. }
  142. return {false: "Error!"};
  143. } on FirebaseException catch (e) {
  144. return {false: "${e.code}"};
  145. } on DioException catch (e) {
  146. print("Create Account ERROR -> ${e.message}");
  147. return {false: "Error!"};
  148. }
  149. }
  150.  
  151. @override
  152. Widget build(BuildContext context) {
  153. return Scaffold(
  154. body: SingleChildScrollView(
  155. child: SafeArea(
  156. child: Container(
  157. width: double.infinity,
  158. padding: const EdgeInsets.all(15.0),
  159. child: Column(
  160. crossAxisAlignment: CrossAxisAlignment.start,
  161. children: [
  162. const SizedBox(
  163. height: 20,
  164. ),
  165. Container(
  166. alignment: Alignment.center,
  167. child: GlobalText(
  168. text: "Logo",
  169. textAlign: TextAlign.center,
  170. fontSize: 25,
  171. fontWeight: FontWeight.bold,
  172. color: GlobalColors.titleColor,
  173. ),
  174. ),
  175. const SizedBox(
  176. height: 50,
  177. ),
  178. GlobalText(
  179. text: "Let's get started!\nCreate your account",
  180. textAlign: TextAlign.start,
  181. fontSize: 15,
  182. fontWeight: FontWeight.normal,
  183. color: GlobalColors.titleColor,
  184. ),
  185. const SizedBox(
  186. height: 50,
  187. ),
  188.  
  189. // first name input
  190.  
  191. GlobalText(
  192. text: "First Name",
  193. textAlign: TextAlign.start,
  194. fontSize: 15,
  195. fontWeight: FontWeight.bold,
  196. color: GlobalColors.titleColor,
  197. ),
  198. const SizedBox(
  199. height: 10,
  200. ),
  201. TextFormGlobal(
  202. controller: _firstNameController,
  203. isObscured: false,
  204. text: "Enter First Name",
  205. textInputType: TextInputType.name,
  206. onTap: () {},
  207. prefixIcon: Icon(Icons.person),
  208. ),
  209.  
  210. const SizedBox(
  211. height: 15,
  212. ),
  213.  
  214. // last name input
  215. GlobalText(
  216. text: "Last Name",
  217. textAlign: TextAlign.start,
  218. fontSize: 15,
  219. fontWeight: FontWeight.bold,
  220. color: GlobalColors.titleColor,
  221. ),
  222. const SizedBox(
  223. height: 10,
  224. ),
  225. TextFormGlobal(
  226. controller: _lastNameController,
  227. isObscured: false,
  228. text: "Enter Last Name",
  229. textInputType: TextInputType.name,
  230. onTap: () {},
  231. prefixIcon: Icon(Icons.person),
  232. ),
  233.  
  234. const SizedBox(
  235. height: 15,
  236. ),
  237.  
  238. // email input
  239. GlobalText(
  240. text: "Email",
  241. textAlign: TextAlign.start,
  242. fontSize: 15,
  243. fontWeight: FontWeight.bold,
  244. color: GlobalColors.titleColor,
  245. ),
  246. const SizedBox(
  247. height: 10,
  248. ),
  249. TextFormGlobal(
  250. controller: _emailController,
  251. isObscured: false,
  252. text: "Enter Email",
  253. textInputType: TextInputType.emailAddress,
  254. onTap: () {},
  255. prefixIcon: Icon(Icons.email),
  256. ),
  257. const SizedBox(
  258. height: 15,
  259. ),
  260.  
  261. // password input
  262. GlobalText(
  263. text: "Password",
  264. textAlign: TextAlign.start,
  265. fontSize: 15,
  266. fontWeight: FontWeight.bold,
  267. color: GlobalColors.titleColor,
  268. ),
  269. const SizedBox(
  270. height: 10,
  271. ),
  272. TextFormGlobal(
  273. controller: _passwordController,
  274. isObscured: true,
  275. text: "Enter Password",
  276. textInputType: TextInputType.visiblePassword,
  277. onTap: () {},
  278. prefixIcon: Icon(Icons.password),
  279. ),
  280. const SizedBox(
  281. height: 30,
  282. ),
  283.  
  284. // login button
  285. ButtonGlobal(
  286. text: "Create Account",
  287. onTap: register,
  288. ),
  289. ],
  290. ),
  291. ))),
  292. // don't have an account?
  293. bottomNavigationBar: Container(
  294. height: 50,
  295. alignment: Alignment.center,
  296. child: Row(
  297. mainAxisAlignment: MainAxisAlignment.center,
  298. children: [
  299. GlobalText(
  300. text: "Have an account?",
  301. textAlign: TextAlign.end,
  302. fontSize: 15,
  303. fontWeight: FontWeight.normal,
  304. color: GlobalColors.titleColor),
  305. GestureDetector(
  306. onTap: widget.onTap,
  307. child: GlobalText(
  308. text: " Sign In",
  309. textAlign: TextAlign.end,
  310. fontSize: 15,
  311. fontWeight: FontWeight.bold,
  312. color: GlobalColors.titleColor),
  313. )
  314. ],
  315. ),
  316. ),
  317. );
  318. }
  319. }
  320.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement