Advertisement
rifki_cs29

LoginPage

May 9th, 2023
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.72 KB | None | 0 0
  1. import 'dart:io';
  2.  
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:module_auth/presentation/cubit/validate_login_cubit.dart';
  7. import 'package:module_auth/presentation/pages/forgot_password/forgot_password_check_page.dart';
  8. import 'package:module_core/module_core.dart';
  9. import 'package:module_core/module_core.dart' as di;
  10. import 'package:module_dependencies/module_dependencies.dart';
  11. import 'package:module_dependencies/module_dependencies_ui.dart';
  12.  
  13. class LoginPage extends StatefulWidget {
  14.   const LoginPage({super.key});
  15.   static const routeName = '/login';
  16.  
  17.   @override
  18.   State<LoginPage> createState() => _LoginPageState();
  19. }
  20.  
  21. class _LoginPageState extends State<LoginPage> {
  22.   final _formKey = GlobalKey<FormState>();
  23.   final TextEditingController _mobileNumberController = TextEditingController();
  24.   final TextEditingController _passwordController = TextEditingController();
  25.   final _validateLoginCubit = ValidateLoginCubit();
  26.  
  27.   @override
  28.   void dispose() {
  29.     _mobileNumberController.dispose();
  30.     _passwordController.dispose();
  31.     super.dispose();
  32.   }
  33.  
  34.   @override
  35.   Widget build(BuildContext context) {
  36.     return BlocProvider(
  37.       create: (_) => di.locator<LoginBloc>(),
  38.       child: Scaffold(
  39.         appBar: CustomAppBar(
  40.           titleText: 'MASUK',
  41.           leading: InkWell(
  42.             hoverColor: colorPrimary,
  43.             onTap: () => Navigator.pop(context),
  44.             child: const Icon(
  45.               CupertinoIcons.back,
  46.               color: Colors.white,
  47.               size: 24,
  48.             ),
  49.           ),
  50.         ),
  51.         body: SingleChildScrollView(
  52.           padding: EdgeInsets.symmetric(
  53.             horizontal: defaultMargin,
  54.           ),
  55.           child: Form(
  56.             key: _formKey,
  57.             child: Column(
  58.               mainAxisAlignment: MainAxisAlignment.center,
  59.               children: [
  60.                 Gap(50.h),
  61.                 Image.asset(
  62.                   Assets.png.myagentDesc.path,
  63.                   height: context.width / 2,
  64.                 ),
  65.                 Gap(24.h),
  66.                 CustomTextField(
  67.                   controller: _mobileNumberController,
  68.                   textName: 'Nomor Ponsel',
  69.                   hintText: 'Nomor Ponsel',
  70.                   maxLines: 1,
  71.                   keyboardType: Platform.isIOS
  72.                       ? const TextInputType.numberWithOptions(
  73.                           signed: true,
  74.                         )
  75.                       : TextInputType.number,
  76.                   inputFormatters: [
  77.                     FilteringTextInputFormatter.digitsOnly,
  78.                   ],
  79.                   validator: (value) {
  80.                     if (value != null) {
  81.                       if (value.isEmpty) {
  82.                         return 'Nomor ponsel tidak boleh kosong';
  83.                       }
  84.                       if (value.length < 9) {
  85.                         return 'Nomor ponsel minimal 9 karakter';
  86.                       }
  87.                       if (value.length > 15) {
  88.                         return 'Nomor ponsel maksimal 15 karakter';
  89.                       }
  90.                       if (value.length > 2) {
  91.                         if (value.substring(0, 2) != '08') {
  92.                           return 'Nomor ponsel tidak valid';
  93.                         }
  94.                       }
  95.                     }
  96.                     return null;
  97.                   },
  98.                   onChanged: (value) {
  99.                     _validateLoginCubit
  100.                         .update(_formKey.currentState?.validate() ?? false);
  101.                   },
  102.                 ),
  103.                 const Gap(25),
  104.                 CustomTextField(
  105.                   controller: _passwordController,
  106.                   textName: 'Password',
  107.                   hintText: 'Password',
  108.                   isObsecure: true,
  109.                   maxLines: 1,
  110.                   validator: (value) {
  111.                     if (value != null) {
  112.                       if (value.isEmpty) {
  113.                         return 'Password tidak boleh kosong';
  114.                       }
  115.                     }
  116.                     return null;
  117.                   },
  118.                   onChanged: (value) {
  119.                     _validateLoginCubit
  120.                         .update(_formKey.currentState?.validate() ?? false);
  121.                   },
  122.                 ),
  123.                 const Gap(30),
  124.                 BlocBuilder<ValidateLoginCubit, bool>(
  125.                   bloc: _validateLoginCubit,
  126.                   builder: (context, state) {
  127.                     return BlocConsumer<LoginBloc, LoginState>(
  128.                       listener: (context, stateLogin) {
  129.                         if (stateLogin.state == RequestState.loaded) {
  130.                           Navigator.of(context).pushNamedAndRemoveUntil(
  131.                             MainPage.routeName,
  132.                             (_) => false,
  133.                           );
  134.                           CustomToast.showSuccess(
  135.                             'Login berhasil',
  136.                             context: context,
  137.                           );
  138.                         } else if (stateLogin.state == RequestState.error) {
  139.                           if (stateLogin.message ==
  140.                               'No Handphone belum terdaftar') {
  141.                             showDialog(
  142.                               context: context,
  143.                               builder: (context) {
  144.                                 return ErrorMessageDialog(
  145.                                   errorMessage: stateLogin.message,
  146.                                   onTap: () {
  147.                                     Navigator.pop(context);
  148.                                     Navigator.pushNamed(
  149.                                       context,
  150.                                       IntroductionRegisterPage.routeName,
  151.                                     );
  152.                                   },
  153.                                   buttonLabel: 'DAFTAR AKUN BARU',
  154.                                 );
  155.                               },
  156.                             );
  157.                           } else {
  158.                             showDialog(
  159.                               context: context,
  160.                               builder: (context) {
  161.                                 return ErrorMessageDialog(
  162.                                   errorMessage: stateLogin.message,
  163.                                 );
  164.                               },
  165.                             );
  166.                           }
  167.                         }
  168.                       },
  169.                       builder: (context, stateLogin) {
  170.                         if (stateLogin.state == RequestState.loading) {
  171.                           return SizedBox(
  172.                             height: 42,
  173.                             width: double.infinity,
  174.                             child: LoadingShimmer(radius: defaultRadius),
  175.                           );
  176.                         }
  177.                         return CustomButton.buttonRounded(
  178.                           radius: defaultRadius,
  179.                           onTap: state
  180.                               ? () {
  181.                                   if (FocusScope.of(context).hasFocus) {
  182.                                     FocusManager.instance.primaryFocus
  183.                                         ?.unfocus();
  184.                                   }
  185.                                   BlocProvider.of<LoginBloc>(
  186.                                     context,
  187.                                   ).add(
  188.                                     PostLoginEvent(
  189.                                       _mobileNumberController.text,
  190.                                       _passwordController.text,
  191.                                     ),
  192.                                   );
  193.                                 }
  194.                               : null,
  195.                           text: "MASUK",
  196.                           color: state ? colorPrimary : colorGrey,
  197.                         );
  198.                       },
  199.                     );
  200.                   },
  201.                 ),
  202.                 Gap(16.h),
  203.                 InkWell(
  204.                   onTap: () {
  205.                     showModalBottomSheet(
  206.                       context: context,
  207.                       isScrollControlled: true,
  208.                       useRootNavigator: true,
  209.                       shape: const RoundedRectangleBorder(
  210.                         borderRadius: BorderRadius.vertical(
  211.                           top: Radius.circular(24.0),
  212.                         ),
  213.                       ),
  214.                       builder: (_) {
  215.                         return LayoutBuilder(
  216.                           builder: (context, _) {
  217.                             return AnimatedPadding(
  218.                               padding: MediaQuery.of(
  219.                                 context,
  220.                               ).viewInsets,
  221.                               duration: const Duration(
  222.                                 milliseconds: 100,
  223.                               ),
  224.                               child: const ForgotPasswordCheckPage(),
  225.                             );
  226.                           },
  227.                         );
  228.                       },
  229.                     );
  230.                   },
  231.                   child: Text(
  232.                     'Lupa kata sandi',
  233.                     textAlign: TextAlign.center,
  234.                     style: blackTextStyle.copyWith(
  235.                       fontSize: 12.sp,
  236.                       fontWeight: regular,
  237.                       height: 1.5,
  238.                     ),
  239.                   ),
  240.                 ),
  241.               ],
  242.             ),
  243.           ),
  244.         ),
  245.       ),
  246.     );
  247.   }
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement