Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
11 kB
1
Indexable
Never
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gravid_mobile_app/bloc/cubit/home_screen_cubit.dart';
import 'package:gravid_mobile_app/bloc/cubit/register_screen_cubit.dart';
import 'package:gravid_mobile_app/constants/color_palette.dart';
import 'package:gravid_mobile_app/constants/dimens.dart';
import 'package:gravid_mobile_app/constants/strings.dart';
import 'package:gravid_mobile_app/data/remote/apis.dart';
import 'package:gravid_mobile_app/routes/route_names.dart';
import 'package:gravid_mobile_app/ui/screens/login_screen.dart';
import 'package:gravid_mobile_app/ui/widgets/constant_widget.dart';
import 'package:gravid_mobile_app/utils/utils.dart';

class RegistrationScreen extends StatefulWidget {
  const RegistrationScreen({super.key, required String title});

  @override
  State<RegistrationScreen> createState() => _RegistrationScreen();
}

class _RegistrationScreen extends State<RegistrationScreen> {
  ///Creating a cubit instance
  RegisterScreenCubit? _registerScreenCubit;
  Map<String, dynamic> formData = <String, dynamic>{};

  @override
  void initState() {
    super.initState();

    ///Initialialising cubit and fetching posts at initstate
    ///Initstate is called before build function in flutter
    _registerScreenCubit = BlocProvider.of<RegisterScreenCubit>(context);
  }

  // @override
  // void dispose() {
  //   ///Closing the cubit at dispose function
  //   _registerScreenCubit!.close();
  //   super.dispose();
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: Dimens.regularPagePadding,

        ///Building a list of data with list got from api
        child: SingleChildScrollView(
          reverse: true,
          child: buildListBlocConsumer(),
        ),
      ),
    );
  }

  BlocConsumer<RegisterScreenCubit, RegisterScreenState>
  buildListBlocConsumer() {
    return BlocConsumer<RegisterScreenCubit, RegisterScreenState>(
      listener:
          (BuildContext context, RegisterScreenState registerScreenState) {
        if (registerScreenState is LoadingFailed) {
          Utils.showSnackBar(
              context: context, message: registerScreenState.message);
        } else if (registerScreenState is LoadingSuccessful) {
          if (registerScreenState.posts == "Success") {
            Navigator.pushReplacementNamed(context, RouteNames.loginScreen,
                arguments: Strings.homeScreenText);
          } else if (registerScreenState is Loading) {
            SizedBox(
              height: Utils.getScreenHeight(context) / 8,
              child: const Center(
                child: CircularProgressIndicator(),
              ),
            );
          }
        }
      },
      builder: (BuildContext context, RegisterScreenState state) {
        bool isChecked = false;

        // if (state is Initialized) {
        //   print("RegisterScreenInitial");
        //   return getRegistrationWidget(isChecked);
        // } else if (state is Loading) {
        //   print("RegisterScreenLoading");
        //   return SizedBox(
        //     height: Utils.getScreenHeight(context) / 8,
        //     child: const Center(
        //       child: CircularProgressIndicator(),
        //     ),
        //   );
        // } else if (state is RegisterScreenLoaded) {
        //   print("RegisterScreenLoaded");
        //
        //   ///Creating list view with the data only after data is loaded
        //   // SchedulerBinding.instance.addPostFrameCallback((_) {
        //   //   Navigator.of(context).push(
        //   //     MaterialPageRoute(
        //   //       builder: (context) {
        //   //         return LoginScreen(title: "");
        //   //       },
        //   //     ),
        //   //   );
        //   //   return ;
        //   // });
        //
        //   return LoginScreen(title: "");
        // } else {
        //   print("else");
        //   return SizedBox(
        //     height: Utils.getScreenHeight(context) / 8,
        //     child: const Center(
        //       child: CircularProgressIndicator(),
        //     ),
        //   );
        // }
        return getRegistrationWidget(isChecked);
      },
    );
  }

  Color getColor(Set<MaterialState> states) {
    const Set<MaterialState> interactiveStates = <MaterialState>{
      MaterialState.pressed,
      MaterialState.hovered,
      MaterialState.focused,
    };
    if (states.any(interactiveStates.contains)) {
      return Colors.blue;
    }
    return Colors.red;
  }

  Widget getRegistrationWidget(bool isChecked) {
    return Container(
      child: Column(
        children: <Widget>[
          SizedBox(
            height: Utils.getScreenHeight(context) / 15.0,
          ),
          Center(
            child: ConstantWidget.imageWidget(
                'lib/assets/images/account_logo.PNG', 300.0, 350.0,
                Shape: BoxShape.rectangle),
          ),
          SizedBox(height: 2.0),
          Center(child: ConstantWidget.getTextWidget("Create Your Account")),
          SizedBox(height: 15.0),
          SizedBox(
            height: 50,
            width: 320,
            child: Row(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: ConstantWidget.imageIconWidget(
                        'lib/assets/images/gravid_icon_account.png')),
                Expanded(
                    flex: 6,
                    child: ConstantWidget.getTextFormUnderlinedField(
                        'First Name', 'name', formData)),
              ],
            ),
          ),
          SizedBox(height: 15.0),
          SizedBox(
            height: 50,
            width: 320,
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: ConstantWidget.imageIconWidget(
                      'lib/assets/images/gravid_icon_account.png'),
                ),
                Expanded(
                    flex: 6,
                    child: ConstantWidget.getTextFormUnderlinedField(
                        'Last Name', 'Last Name', formData)),
              ],
            ),
          ),
          SizedBox(height: 15.0),
          SizedBox(
            height: 50,
            width: 320,
            child: Row(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: ConstantWidget.imageIconWidget(
                        'lib/assets/images/gravid_icon_phone.png')),
                Expanded(
                    flex: 6,
                    child: ConstantWidget.getTextFormUnderlinedField(
                        'Phone Number', 'phone', formData)),
              ],
            ),
          ),
          SizedBox(height: 15.0),
          SizedBox(
            height: 50,
            width: 320,
            child: Row(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: ConstantWidget.imageIconWidget(
                        'lib/assets/images/gravid_icon_email.png')),
                Expanded(
                    flex: 6,
                    child: ConstantWidget.getTextFormUnderlinedField(
                        'Email ID', 'email', formData)),

                // SizedBox(
                //   child: Expanded(
                //       flex: 1,
                //       child: Text("")),
                // )
              ],
            ),
          ),
          SizedBox(height: 10.0),
          SizedBox(
            height: 70,
            width: 320,
            child: Row(
              children: <Widget>[
                Expanded(
                    flex: 1,
                    child: Checkbox(
                      checkColor: Colors.white,
                      fillColor: MaterialStateProperty.resolveWith(getColor),
                      value: isChecked,
                      onChanged: (bool? value) {
                        setState(() {
                          isChecked = value!;
                        });
                      },
                    )),
                Expanded(
                    flex: 6,
                    child: RichText(
                      text: TextSpan(children: [
                        TextSpan(
                          text: "you're agree to our",
                          style: TextStyle(color: Colors.black),
                        ),
                        TextSpan(
                            text: " Terms & Conditions",
                            style: TextStyle(
                                color: Color.fromRGBO(254, 136, 126, 1)),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                print('Login Text Clicked');
                              }),
                        TextSpan(
                          text: " and",
                          style: TextStyle(color: Colors.black),
                        ),
                        TextSpan(
                            text: "\n Privacy Policy",
                            style: TextStyle(
                                height: 1.5,
                                color: Color.fromRGBO(254, 136, 126, 1)),
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                print('Login Text Clicked');
                              }),
                      ]),
                    )),

                // SizedBox(
                //   child: Expanded(
                //       flex: 1,
                //       child: Text("")),
                // )
              ],
            ),
          ),
          Center(
            child: ConstantWidget.elevationButton("SIGN UP", onSignupTap),
          ),
          SizedBox(height: 10.0),
          Center(
              child: RichText(
                text: TextSpan(children: [
                  TextSpan(
                    text: "Already have a account ",
                    style: TextStyle(color: Colors.black),
                  ),
                  TextSpan(
                      text: "Sign in",
                      style: TextStyle(color: Color.fromRGBO(254, 136, 126, 1)),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          print('Login Text Clicked');
                        }),
                ]),
              ))
        ],
      ),
    );
  }

  void onSignupTap() {
    //     () {
    //   _registerScreenCubit?.postRequest(Apis.registerApi, formData);
    // }

    formData["name"] = formData["name"] + ' ' + formData["Last Name"];
    formData["userType"] = 1;
    formData["gender"] = 'f';
    _registerScreenCubit?.postRequest(Apis.registerApi, formData);
  }
}