Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:app/auth/login_or_register.dart';
- import 'package:app/pages/shared/login/login_page.dart';
- import 'package:app/pages/shared/widgets/button_global.dart';
- import 'package:app/pages/shared/widgets/custom_snackbar.dart';
- import 'package:app/pages/shared/widgets/text_form_global.dart';
- import 'package:app/pages/shared/widgets/text_global.dart';
- import 'package:app/utils/dio_routes.dart';
- import 'package:app/utils/global.colors.dart';
- import 'package:dio/dio.dart';
- import 'package:firebase_core/firebase_core.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- class SignUpPage extends StatefulWidget {
- final void Function()? onTap;
- const SignUpPage({super.key, required this.onTap});
- @override
- _SignUpPageState createState() => _SignUpPageState();
- }
- class _SignUpPageState extends State<SignUpPage> {
- bool _isLoading = false;
- bool _redirecting = false;
- late final TextEditingController _emailController = TextEditingController();
- late final TextEditingController _passwordController =
- TextEditingController();
- late final TextEditingController _firstNameController =
- TextEditingController();
- late final TextEditingController _lastNameController =
- TextEditingController();
- /* void register() async {
- // show loading screen
- showDialog(
- context: context,
- builder: (context) => const Center(
- child: CircularProgressIndicator(),
- ),
- );
- // try create the user
- try {
- UserCredential? userCredential = await FirebaseAuth.instance
- .createUserWithEmailAndPassword(
- email: _emailController.text, password: _passwordController.text);
- if (mounted) {
- // create user document and send it to api
- await createUserData(context, userCredential.user!.uid);
- Navigator.pop(context);
- } else {
- // you can't use the context
- }
- // pop loading circle
- } on FirebaseException catch (e) {
- // pop loading circle
- //Navigator.pop(context);
- // display error message to user
- displayNotification(context, e.code);
- }
- }
- Future createUserData(BuildContext context, String uid) async {
- try {
- var data = {
- "userId": uid,
- "clientName": _firstNameController.text.toString(),
- "email": _emailController.text.toString(),
- };
- Response response =
- await Dio().post(createAccountEndpoint, data: jsonEncode(data));
- if (response.statusCode == 200) {
- // display error message
- displayNotification(context, "Your account was created successfully!");
- Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => const LoginOrRegister()),
- );
- } else {
- displayNotification(context, "Error!");
- }
- } on DioException catch (e) {
- print("Create Account ERROR -> ${e.message}");
- }
- } */
- void register() async {
- // show loading screen
- showDialog(
- context: context,
- builder: (context) => const Center(
- child: CircularProgressIndicator(),
- ),
- );
- // try create the user
- final result = await createUser();
- displayNotification(context, result.values.first);
- Navigator.pop(context);
- if (result.keys.first) {
- Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => const LoginOrRegister()),
- );
- }
- }
- Future<Map<bool, String>> createUser() async {
- try {
- UserCredential? userCredential = await FirebaseAuth.instance
- .createUserWithEmailAndPassword(
- email: _emailController.text, password: _passwordController.text);
- final uid = userCredential.user!.uid;
- var data = {
- "userId": uid,
- "clientName": _firstNameController.text.toString(),
- "email": _emailController.text.toString(),
- };
- Response response =
- await Dio().post(createAccountEndpoint, data: jsonEncode(data));
- if (response.statusCode == 200) {
- return {true: "Your account was created successfully!"};
- }
- return {false: "Error!"};
- } on FirebaseException catch (e) {
- return {false: "${e.code}"};
- } on DioException catch (e) {
- print("Create Account ERROR -> ${e.message}");
- return {false: "Error!"};
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: SingleChildScrollView(
- child: SafeArea(
- child: Container(
- width: double.infinity,
- padding: const EdgeInsets.all(15.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- const SizedBox(
- height: 20,
- ),
- Container(
- alignment: Alignment.center,
- child: GlobalText(
- text: "Logo",
- textAlign: TextAlign.center,
- fontSize: 25,
- fontWeight: FontWeight.bold,
- color: GlobalColors.titleColor,
- ),
- ),
- const SizedBox(
- height: 50,
- ),
- GlobalText(
- text: "Let's get started!\nCreate your account",
- textAlign: TextAlign.start,
- fontSize: 15,
- fontWeight: FontWeight.normal,
- color: GlobalColors.titleColor,
- ),
- const SizedBox(
- height: 50,
- ),
- // first name input
- GlobalText(
- text: "First Name",
- textAlign: TextAlign.start,
- fontSize: 15,
- fontWeight: FontWeight.bold,
- color: GlobalColors.titleColor,
- ),
- const SizedBox(
- height: 10,
- ),
- TextFormGlobal(
- controller: _firstNameController,
- isObscured: false,
- text: "Enter First Name",
- textInputType: TextInputType.name,
- onTap: () {},
- prefixIcon: Icon(Icons.person),
- ),
- const SizedBox(
- height: 15,
- ),
- // last name input
- GlobalText(
- text: "Last Name",
- textAlign: TextAlign.start,
- fontSize: 15,
- fontWeight: FontWeight.bold,
- color: GlobalColors.titleColor,
- ),
- const SizedBox(
- height: 10,
- ),
- TextFormGlobal(
- controller: _lastNameController,
- isObscured: false,
- text: "Enter Last Name",
- textInputType: TextInputType.name,
- onTap: () {},
- prefixIcon: Icon(Icons.person),
- ),
- const SizedBox(
- height: 15,
- ),
- // email input
- GlobalText(
- text: "Email",
- textAlign: TextAlign.start,
- fontSize: 15,
- fontWeight: FontWeight.bold,
- color: GlobalColors.titleColor,
- ),
- const SizedBox(
- height: 10,
- ),
- TextFormGlobal(
- controller: _emailController,
- isObscured: false,
- text: "Enter Email",
- textInputType: TextInputType.emailAddress,
- onTap: () {},
- prefixIcon: Icon(Icons.email),
- ),
- const SizedBox(
- height: 15,
- ),
- // password input
- GlobalText(
- text: "Password",
- textAlign: TextAlign.start,
- fontSize: 15,
- fontWeight: FontWeight.bold,
- color: GlobalColors.titleColor,
- ),
- const SizedBox(
- height: 10,
- ),
- TextFormGlobal(
- controller: _passwordController,
- isObscured: true,
- text: "Enter Password",
- textInputType: TextInputType.visiblePassword,
- onTap: () {},
- prefixIcon: Icon(Icons.password),
- ),
- const SizedBox(
- height: 30,
- ),
- // login button
- ButtonGlobal(
- text: "Create Account",
- onTap: register,
- ),
- ],
- ),
- ))),
- // don't have an account?
- bottomNavigationBar: Container(
- height: 50,
- alignment: Alignment.center,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- GlobalText(
- text: "Have an account?",
- textAlign: TextAlign.end,
- fontSize: 15,
- fontWeight: FontWeight.normal,
- color: GlobalColors.titleColor),
- GestureDetector(
- onTap: widget.onTap,
- child: GlobalText(
- text: " Sign In",
- textAlign: TextAlign.end,
- fontSize: 15,
- fontWeight: FontWeight.bold,
- color: GlobalColors.titleColor),
- )
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement