Untitled

 avatar
unknown
plain_text
a year ago
9.0 kB
6
Indexable
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();


     register(context) 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}");
    }
  }  

 

  @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(context),
                    ),
                  ],
                ),
              ))),
      // 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),
            )
          ],
        ),
      ),
    );
  }
}
Editor is loading...
Leave a Comment