Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
12 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/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/verify_logo.dart';
import 'package:gravid_mobile_app/ui/widgets/constant_widget.dart';
import 'package:gravid_mobile_app/utils/utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../Helper/sharedPreferences.dart';

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

  @override
  State<LoginScreen> createState() => _LoginScreen();
}

class _LoginScreen extends State<LoginScreen> {
  ///Creating a cubit instance
  RegisterScreenCubit? _loginScreenCubit;
  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
     _loginScreenCubit = BlocProvider.of<RegisterScreenCubit>(context);
     _loginScreenCubit!.changeState();

     //_homeScreenCubit?.getPosts();
  }

  // @override
  // void dispose() {
  //   ///Closing the cubit at dispose function
  //   _loginScreenCubit!.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 Loading) {
            SizedBox(
              height: Utils.getScreenHeight(context) / 8,
              child: const Center(
                child: CircularProgressIndicator(),
              ),
            );
        }
        else if(homeScreenState is LoadingSuccessful){
          if (homeScreenState.posts == "Success") {
              print("hello world");
              Navigator.pushReplacementNamed(context, RouteNames.verifyScreen,
                  arguments: Strings.homeScreenText);
              // Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => VerifyScreen(title: "Verify Otp")));
          }
        }
      },
      builder: (BuildContext context, RegisterScreenState state) {
        print("---------------------------");

        print(state);
        print("---------------------------");

        // if (state is Initialized) {
        //   print("stage 1");
        //   return Container(
        //       child: Column(
        //         children: <Widget>[
        //           SizedBox(
        //             height: Utils.getScreenHeight(context) / 15.0,
        //           ),
        //           Center(
        //             child: ConstantWidget.imageWidget(
        //                 'lib/assets/images/login_logo.PNG', 300.0, 350.0,
        //                 Shape: BoxShape.rectangle),
        //           ),
        //           SizedBox(height: 20.0),
        //           Center(child: _textWidget()),
        //           SizedBox(height: 25.0),
        //           Center(
        //             child: Text(
        //               "Enter your Mobile Number to Continue",
        //               style: TextStyle(
        //                   fontSize: 14,
        //                   fontStyle: FontStyle.normal,
        //                   fontWeight: FontWeight.w500),
        //             ),
        //           ),
        //           SizedBox(height: 40.0),
        //           SizedBox(
        //             height: 50,
        //             width: 320,
        //             child: Row(
        //               children: <Widget>[
        //                 Expanded(
        //                   flex: 1,
        //                   child: ConstantWidget.getTextFormBorderlinedField('+91', 'countryCode', formData)
        //                 ),
        //                 Expanded(
        //                     flex: 3,
        //                     child: ConstantWidget.getTextFormBorderlinedField('Phone Number', 'number', formData)),
        //
        //                 // SizedBox(
        //                 //   child: Expanded(
        //                 //       flex: 1,
        //                 //       child: Text("")),
        //                 // )
        //               ],
        //             ),
        //           ),
        //           SizedBox(height: 60.0),
        //           Center(
        //             child: ConstantWidget.elevationButton(
        //                 "NEXT", onLoginTap),
        //           ),
        //         ],
        //       ));
        // } 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
        //   print("stage 3");
        //   return SizedBox(
        //     height: Utils.getScreenHeight(context) / 8,
        //     child: const Center(
        //       child: CircularProgressIndicator(),
        //     ),
        //   );
        //
        // }
        //     // else if(state is LoadingSuccessful){
        //     //   if (state.posts == "Success") {
        //     //     print("hello world");
        //     //     Navigator.pushReplacementNamed(context, RouteNames.registrationScreen,
        //     //         arguments: Strings.homeScreenText);
        //     //     return Text("Success");
        //     //   }else{
        //     //     return Text("failure");
        //     //   }
        //     // }
        //
        //
        // else {
        //   print("stage 2");
        //    return Container(
        //       child: Column(
        //         children: <Widget>[
        //           SizedBox(
        //             height: Utils.getScreenHeight(context) / 15.0,
        //           ),
        //           Center(
        //             child: ConstantWidget.imageWidget(
        //                 'lib/assets/images/login_logo.PNG', 300.0, 350.0,
        //                 Shape: BoxShape.rectangle),
        //           ),
        //           SizedBox(height: 20.0),
        //           Center(child: _textWidget()),
        //           SizedBox(height: 25.0),
        //           Center(
        //             child: Text(
        //               "Enter your Mobile Number to Continue",
        //               style: TextStyle(
        //                   fontSize: 14,
        //                   fontStyle: FontStyle.normal,
        //                   fontWeight: FontWeight.w500),
        //             ),
        //           ),
        //           SizedBox(height: 40.0),
        //           SizedBox(
        //             height: 50,
        //             width: 320,
        //             child: Row(
        //               children: <Widget>[
        //                 Expanded(
        //                     flex: 1,
        //                     child: ConstantWidget.getTextFormBorderlinedField('+91', 'countryCode', formData)
        //                 ),
        //                 Expanded(
        //                     flex: 3,
        //                     child: ConstantWidget.getTextFormBorderlinedField('Phone Number', 'number', formData)),
        //
        //                 // SizedBox(
        //                 //   child: Expanded(
        //                 //       flex: 1,
        //                 //       child: Text("")),
        //                 // )
        //               ],
        //             ),
        //           ),
        //           SizedBox(height: 60.0),
        //           Center(
        //             child: ConstantWidget.elevationButton(
        //                 "NEXT", onLoginTap),
        //           ),
        //         ],
        //       ));
        // }

        return getLoginScreen();
      },
    );
  }

  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 getLoginScreen(){
    return Container(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: Utils.getScreenHeight(context) / 15.0,
            ),
            Center(
              child: ConstantWidget.imageWidget(
                  'lib/assets/images/login_logo.PNG', 300.0, 350.0,
                  Shape: BoxShape.rectangle),
            ),
            SizedBox(height: 20.0),
            Center(child: _textWidget()),
            SizedBox(height: 25.0),
            Center(
              child: Text(
                "Enter your Mobile Number to Continue",
                style: TextStyle(
                    fontSize: 14,
                    fontStyle: FontStyle.normal,
                    fontWeight: FontWeight.w500),
              ),
            ),
            SizedBox(height: 40.0),
            SizedBox(
              height: 50,
              width: 320,
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: ConstantWidget.getTextFormBorderlinedField('+91', 'countryCode', formData)
                  ),
                  Expanded(
                      flex: 3,
                      child: ConstantWidget.getTextFormBorderlinedField('Phone Number', 'number', formData)),

                  // SizedBox(
                  //   child: Expanded(
                  //       flex: 1,
                  //       child: Text("")),
                  // )
                ],
              ),
            ),
            SizedBox(height: 60.0),
            Center(
              child: ConstantWidget.elevationButton(
                  "NEXT", onLoginTap),
            ),
          ],
        ));
  }

  Widget _textWidget() {
    return Text(
      "Sign in",
      style: TextStyle(
          fontSize: 24,
          fontStyle: FontStyle.normal,
          fontWeight: FontWeight.w600),
    );
//);
  }
  Future<void>  onLoginTap() async {
    var phoneNumber = formData["countryCode"] + formData["number"];
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs?.setString(SharedPref.Phone, formData["number"]);
    _loginScreenCubit?.postRequest(Apis.getOtpApi + phoneNumber, formData);
  }
}