Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
18 kB
1
Indexable
Never
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gravid_mobile_app/bloc/cubit/home_screen_cubit.dart';
import 'package:gravid_mobile_app/constants/dimens.dart';
import 'package:gravid_mobile_app/constants/strings.dart';
import 'package:gravid_mobile_app/routes/route_names.dart';
import 'package:gravid_mobile_app/ui/widgets/constant_widget.dart';
import 'package:gravid_mobile_app/utils/utils.dart';
import 'package:otp_text_field/otp_field.dart';
import 'package:otp_text_field/style.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../bloc/cubit/register_screen_cubit.dart';
import '../../constants/color_palette.dart';
import '../../data/remote/apis.dart';
import '../../helper/sharedPreferences.dart';

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

  @override
  State<VerifyScreen> createState() => _VerifyScreen();
}

class _VerifyScreen extends State<VerifyScreen> {
  ///Creating a cubit instance
  RegisterScreenCubit? _verifyScreenCubit;
  String? _phoneNumber;
  String? _otp;
  Map<String, dynamic> formData = <String, dynamic>{};

  @override
  initState()  {
    print("i am here ");
    super.initState();


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

//   @override
//   void dispose() {
//     ///Closing the cubit at dispose function
// //_homeScreenCubit!.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 homeScreenState) {
        if (homeScreenState is LoadingFailed) {
          Utils.showSnackBar(
              context: context, message: homeScreenState.message);
        }
        else if(homeScreenState is LoadingSuccessful){
          if (homeScreenState.posts == "Success") {
            print("hello world");
            Navigator.pushReplacementNamed(context, RouteNames.verifyScreen,
                arguments: Strings.homeScreenText);
          }else{
          }
        }
        else if (homeScreenState is Loading) {
          SizedBox(
            height: Utils.getScreenHeight(context) / 8,
            child: const Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
        else if(homeScreenState is RegisterScreenLoaded){
          Container(
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: Utils.getScreenHeight(context) / 15.0,
                  ),
                  Center(
                    child: ConstantWidget.imageWidget(
                        'lib/assets/images/verify_logo.PNG', 300.0, 350.0,
                        Shape: BoxShape.rectangle),
                  ),
                  SizedBox(height: 20.0),
                  Center(child: _textWidget()),
                  SizedBox(height: 25.0),
                  Center(
                      child: RichText(
                        text: TextSpan(children: [
                          TextSpan(
                            text: "We have send the code verification to ",
                            style: TextStyle(color: Colors.black),
                          ),
                          TextSpan(
                            text: "Your \n Mobile Number                 +919359969518",
                            style: TextStyle(
                                height: 1.2,
                                color: Colors.black54,
                                fontWeight: FontWeight.w900),
                          ),
                        ]),
                      )),
                  SizedBox(height: 40.0),
                  OTPTextField(
                    length: 4,
                    width: 250,
                    fieldWidth: 40,
                    style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                    textFieldAlignment: MainAxisAlignment.spaceAround,
                    fieldStyle: FieldStyle.box,
                    onCompleted: (pin) {
                      print("Completed: " + pin);
                      _otp=pin;
                    },
                  ),
                  SizedBox(height: 40.0),
                  Center(
                    child: ConstantWidget.elevationButton(

                        "SUBMIT", () {
                      Navigator.pushReplacementNamed(
                          context, RouteNames.magzinesScreen,
                          arguments: Strings.homeScreenText);
                    }, width: 300, radius: 10),
                  ),
                  SizedBox(height: 25.0),
                  Center(
                      child: RichText(
                        text: TextSpan(children: [
                          TextSpan(
                            text: "Didn't receive an OTP ?",
                            style: TextStyle(color: Colors.black26),
                          ),
                          TextSpan(
                            text: "\n         Resend OTP",
                            style: TextStyle(
                              height: 2.2,
                              color: Colors.pinkAccent,
                            ),
                          ),
                        ]),
                      )),
                ],
              ));
        }
        else{
          SizedBox(
            height: Utils.getScreenHeight(context) / 8,
            child: const Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
      },
      builder: (BuildContext context, RegisterScreenState state) {
        print("---------------------------");

        print(state);
        print("---------------------------");
        bool isChecked = false;
        // if (state is Initialized) {
        //   ///Creating list view with the data only after data is loaded
        //   return Container(
        //       child: Column(
        //         children: <Widget>[
        //           SizedBox(
        //             height: Utils.getScreenHeight(context) / 15.0,
        //           ),
        //           Center(
        //             child: ConstantWidget.imageWidget(
        //                 'lib/assets/images/verify_logo.PNG', 300.0, 350.0,
        //                 Shape: BoxShape.rectangle),
        //           ),
        //           SizedBox(height: 20.0),
        //           Center(child: _textWidget()),
        //           SizedBox(height: 25.0),
        //           Center(
        //               child: RichText(
        //                 text: TextSpan(children: [
        //                   TextSpan(
        //                     text: "We have send the code verification to ",
        //                     style: TextStyle(color: Colors.black),
        //                   ),
        //                   TextSpan(
        //                     text: "Your \n Mobile Number                 $_phoneNumber",
        //                     style: TextStyle(
        //                         height: 1.2,
        //                         color: Colors.black54,
        //                         fontWeight: FontWeight.w900),
        //                   ),
        //                 ]),
        //               )),
        //           SizedBox(height: 40.0),
        //           OTPTextField(
        //             length: 4,
        //             width: 250,
        //             fieldWidth: 40,
        //             style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
        //             textFieldAlignment: MainAxisAlignment.spaceAround,
        //             fieldStyle: FieldStyle.box,
        //             onCompleted: (pin) {
        //               print("Completed: " + pin);
        //             },
        //           ),
        //           SizedBox(height: 40.0),
        //           Center(
        //             child: ConstantWidget.elevationButton(
        //
        //                 "SUBMIT", () {
        //               Navigator.pushReplacementNamed(
        //                   context, RouteNames.magzinesScreen,
        //                   arguments: Strings.homeScreenText);
        //             }, width: 300, radius: 10),
        //           ),
        //           SizedBox(height: 25.0),
        //           Center(
        //               child: RichText(
        //                 text: TextSpan(children: [
        //                   TextSpan(
        //                     text: "Didn't receive an OTP ?",
        //                     style: TextStyle(color: Colors.black26),
        //                   ),
        //                   TextSpan(
        //                     text: "\n         Resend OTP",
        //                     style: TextStyle(
        //                       height: 2.2,
        //                       color: Colors.pinkAccent,
        //                     ),
        //                   ),
        //                 ]),
        //               )),
        //         ],
        //       ));
        // } else if (state is Loading) {
        //   return SizedBox(
        //     height: Utils.getScreenHeight(context) / 8,
        //     child: const Center(
        //       child: CircularProgressIndicator(),
        //     ),
        //   );
        // } else if (state is RegisterScreenLoaded) {
        //   ///Creating list view with the data only after data is loaded
        //   return Container(
        //       child: Column(
        //     children: <Widget>[
        //       SizedBox(
        //         height: Utils.getScreenHeight(context) / 15.0,
        //       ),
        //       Center(
        //         child: ConstantWidget.imageWidget(
        //             'lib/assets/images/verify_logo.PNG', 300.0, 350.0,
        //             Shape: BoxShape.rectangle),
        //       ),
        //       SizedBox(height: 20.0),
        //       Center(child: _textWidget()),
        //       SizedBox(height: 25.0),
        //       Center(
        //           child: RichText(
        //         text: TextSpan(children: [
        //           TextSpan(
        //             text: "We have send the code verification to ",
        //             style: TextStyle(color: Colors.black),
        //           ),
        //           TextSpan(
        //             text: "Your \n Mobile Number                 +919359969518",
        //             style: TextStyle(
        //                 height: 1.2,
        //                 color: Colors.black54,
        //                 fontWeight: FontWeight.w900),
        //           ),
        //         ]),
        //       )),
        //       SizedBox(height: 40.0),
        //       OTPTextField(
        //         length: 4,
        //         width: 250,
        //         fieldWidth: 40,
        //         style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
        //         textFieldAlignment: MainAxisAlignment.spaceAround,
        //         fieldStyle: FieldStyle.box,
        //         onCompleted: (pin) {
        //           print("Completed: " + pin);
        //         },
        //       ),
        //       SizedBox(height: 40.0),
        //       Center(
        //         child: ConstantWidget.elevationButton(
        //
        //             "SUBMIT", () {
        //           Navigator.pushReplacementNamed(
        //               context, RouteNames.magzinesScreen,
        //               arguments: Strings.homeScreenText);
        //         }, width: 300, radius: 10),
        //       ),
        //       SizedBox(height: 25.0),
        //       Center(
        //           child: RichText(
        //         text: TextSpan(children: [
        //           TextSpan(
        //             text: "Didn't receive an OTP ?",
        //             style: TextStyle(color: Colors.black26),
        //           ),
        //           TextSpan(
        //             text: "\n         Resend OTP",
        //             style: TextStyle(
        //               height: 2.2,
        //               color: Colors.pinkAccent,
        //             ),
        //           ),
        //         ]),
        //       )),
        //     ],
        //   ));
        // }
        // else if(state is LoadingSuccessful){
        //   if (state.posts == "Success") {
        //     print("hello world");
        //     Navigator.pushReplacementNamed(context, RouteNames.verifyScreen,
        //         arguments: Strings.homeScreenText);
        //     return Text("Success");
        //   }else{
        //     return Text("Success");
        //   }
        // }
        // else {
        //   return SizedBox(
        //     height: Utils.getScreenHeight(context) / 8,
        //     child: const Center(
        //       child: CircularProgressIndicator(),
        //     ),
        //   );
        // }
        return getVerifyScreen();
      },
    );
  }

  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 _textWidget() {
    return Text(
      "Verification Code",
      style: TextStyle(
          fontSize: 24,
          fontStyle: FontStyle.normal,
          fontWeight: FontWeight.w600),
    );
//);
  }

  Widget getVerifyScreen()  {
    getPhone();
    return Container(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: Utils.getScreenHeight(context) / 15.0,
            ),
            Center(
              child: ConstantWidget.imageWidget(
                  'lib/assets/images/verify_logo.PNG', 300.0, 350.0,
                  Shape: BoxShape.rectangle),
            ),
            SizedBox(height: 20.0),
            Center(child: _textWidget()),
            SizedBox(height: 25.0),
            Center(
                child: RichText(
                  text: TextSpan(children: [
                    TextSpan(
                      text: "We have send the code verification to ",
                      style: TextStyle(color: Colors.black),
                    ),
                    TextSpan(
                      text: "Your \n Mobile Number                 $_phoneNumber",
                      style: TextStyle(
                          height: 1.2,
                          color: Colors.black54,
                          fontWeight: FontWeight.w900),
                    ),
                  ]),
                )),
            SizedBox(height: 40.0),
            OTPTextField(
              length: 4,
              width: 250,
              fieldWidth: 40,
              style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
              textFieldAlignment: MainAxisAlignment.spaceAround,
              fieldStyle: FieldStyle.box,
              onCompleted: (pin) {
                print("Completed: " + pin);
                _otp=pin;
              },
            ),
            SizedBox(height: 40.0),
            Center(
              child: ConstantWidget.elevationButton(

                  "SUBMIT", onLoginTap, width: 300, radius: 10),
            ),
            SizedBox(height: 25.0),
            Center(
                child: RichText(
                  text: TextSpan(children: [
                    TextSpan(
                      text: "Didn't receive an OTP ?",
                      style: TextStyle(color: Colors.black26),
                    ),
                    TextSpan(
                      text: "\n         Resend OTP",
                      style: TextStyle(
                        height: 2.2,
                        color: Colors.pinkAccent,
                      ),
                    ),
                  ]),
                )),
          ],
        ));
  }

  Future<void> getPhone() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _phoneNumber = prefs.getString(SharedPref.Phone);
  }

  Future<void>  onLoginTap() async {
    // var phoneNumber = formData["countryCode"] + formData["number"];
    // // SharedPreferences prefs = await SharedPreferences.getInstance();
    // prefs?.setString(SharedPref.Phone, formData["number"]);
    print(_phoneNumber);
    print(_otp);
    _verifyScreenCubit?.postRequest(Apis.verifyOTP1 +"91"+ _phoneNumber!+Apis.verifyOTP2+_otp!,formData );
    return ;
  }
}