Advertisement
rifki_cs29

RegisterPage

Feb 25th, 2022
1,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 17.67 KB | None | 0 0
  1. import 'package:email_validator/email_validator.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_bloc/flutter_bloc.dart';
  4. import 'package:flutter_svg/svg.dart';
  5. import 'package:google_fonts/google_fonts.dart';
  6. import 'package:module_auth/presentation/bloc/register_access_email_bloc/register_access_email_bloc.dart';
  7. import 'package:module_auth/presentation/bloc/register_access_phone_bloc/register_access_phone_bloc.dart';
  8. import 'package:module_auth/presentation/pages/forgot_password/forgot_password_access_page.dart';
  9. import 'package:module_auth/presentation/pages/register/send_register_otp_page.dart';
  10. import 'package:module_core/common/state_enum.dart';
  11. import 'package:module_core/custom_widgets/custom_button/custom_button.dart';
  12. import 'package:module_core/custom_widgets/custom_dialog_register/custom_dialog_register.dart';
  13. import 'package:module_core/custom_widgets/custom_textfield/custom_textfield.dart';
  14. import 'package:module_core/custom_widgets/custom_toast/custom_toast.dart';
  15. import 'package:module_core/utils/app_colors.dart';
  16. import 'package:module_core/utils/app_image.dart';
  17.  
  18. class RegisterPage extends StatefulWidget {
  19.   RegisterPage({Key? key}) : super(key: key);
  20.  
  21.   @override
  22.   State<RegisterPage> createState() => _RegisterPageState();
  23. }
  24.  
  25. class _RegisterPageState extends State<RegisterPage> {
  26.   final TextEditingController _fieldController =
  27.       TextEditingController(text: '');
  28.  
  29.   bool _isValid = false;
  30.  
  31.   @override
  32.   void initState() {
  33.     _isValid = false;
  34.     super.initState();
  35.   }
  36.  
  37.   @override
  38.   Widget build(BuildContext context) {
  39.     return MultiBlocListener(
  40.       listeners: [
  41.         BlocListener<RegisterAccessEmailBloc, RegisterAccessEmailState>(
  42.           listener: (context, state) {
  43.             if (state.state == RequestState.Loaded) {
  44.               Navigator.of(context).push(MaterialPageRoute(
  45.                 builder: (context) => SendRegisterOtpPage(state.data, null),
  46.               ));
  47.             } else if (state.state == RequestState.Error) {
  48.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  49.                   context: context);
  50.             }
  51.           },
  52.           listenWhen: (previousState, currentState) =>
  53.               currentState.state == RequestState.Loaded ||
  54.               currentState.state == RequestState.Error,
  55.         ),
  56.         BlocListener<RegisterAccessPhoneBloc, RegisterAccessPhoneState>(
  57.           listener: (context, state) {
  58.             if (state.state == RequestState.Loaded) {
  59.               Navigator.of(context).push(MaterialPageRoute(
  60.                 builder: (context) => SendRegisterOtpPage(null, state.data),
  61.               ));
  62.             } else if (state.state == RequestState.Error) {
  63.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  64.                   context: context);
  65.             }
  66.           },
  67.           listenWhen: (previousState, currentState) =>
  68.               currentState.state == RequestState.Loaded ||
  69.               currentState.state == RequestState.Error,
  70.         ),
  71.       ],
  72.       child: Scaffold(body:
  73.           BlocBuilder<RegisterAccessEmailBloc, RegisterAccessEmailState>(
  74.               builder: (context, state) {
  75.         return BlocBuilder<RegisterAccessPhoneBloc, RegisterAccessPhoneState>(
  76.             builder: (context, state) {
  77.           return SafeArea(
  78.               child: ListView(children: [
  79.             Padding(
  80.               padding: const EdgeInsets.only(left: 30, top: 30),
  81.               child: Row(
  82.                 children: [
  83.                   GestureDetector(
  84.                     onTap: () => print('Back'),
  85.                     child: Container(
  86.                       width: 32,
  87.                       height: 32,
  88.                       decoration: BoxDecoration(
  89.                         color: AppColors.greyLight,
  90.                         borderRadius: BorderRadius.circular(18),
  91.                       ),
  92.                       child: SvgPicture.asset(AppImage.back_button,
  93.                           height: 24, width: 24, fit: BoxFit.scaleDown),
  94.                     ),
  95.                   ),
  96.                 ],
  97.               ),
  98.             ),
  99.             SizedBox(height: 8),
  100.             Padding(
  101.               padding: const EdgeInsets.symmetric(horizontal: 30),
  102.               child: AspectRatio(
  103.                 aspectRatio: 3 / 2,
  104.                 child: Container(
  105.                   height: 196,
  106.                   decoration: BoxDecoration(
  107.                     image: DecorationImage(
  108.                       image: AssetImage(AppImage.register_image),
  109.                       fit: BoxFit.cover,
  110.                     ),
  111.                   ),
  112.                 ),
  113.               ),
  114.             ),
  115.             SizedBox(height: 16),
  116.             Row(
  117.               mainAxisAlignment: MainAxisAlignment.center,
  118.               children: [
  119.                 Text(
  120.                   'Buat Akunmu Sekarang',
  121.                   style: GoogleFonts.poppins(
  122.                     fontSize: 16,
  123.                     fontWeight: FontWeight.w600,
  124.                     color: Color(0xff2D2F2E),
  125.                   ),
  126.                 ),
  127.               ],
  128.             ),
  129.             SizedBox(height: 6),
  130.             Row(
  131.               mainAxisAlignment: MainAxisAlignment.center,
  132.               children: [
  133.                 Text(
  134.                   'Sudah Punya Akun?',
  135.                   style: GoogleFonts.roboto(
  136.                     fontSize: 12,
  137.                     fontWeight: FontWeight.w400,
  138.                     color: Color(0xff2D2F2E),
  139.                   ),
  140.                 ),
  141.                 SizedBox(width: 2),
  142.                 GestureDetector(
  143.                   onTap: () => print('Masuk'),
  144.                   child: Text(
  145.                     'Masuk',
  146.                     style: GoogleFonts.roboto(
  147.                       fontSize: 12,
  148.                       fontWeight: FontWeight.w500,
  149.                       color: Color(0xff2D2F2E),
  150.                     ),
  151.                   ),
  152.                 ),
  153.               ],
  154.             ),
  155.             SizedBox(height: 18),
  156.             Divider(
  157.               color: Color(0xffC4C4C4),
  158.               thickness: 1,
  159.             ),
  160.             Padding(
  161.               padding: const EdgeInsets.fromLTRB(30, 18, 30, 0),
  162.               child: CustomTextField(
  163.                 title: 'Nomor Handphone atau Email anda',
  164.                 hintText: 'Nomor Handphone atau Email anda',
  165.                 controller: _fieldController,
  166.                 onChanged: (value) {
  167.                   if ((EmailValidator.validate(value) ||
  168.                           isValidPhoneNumber(value)) &&
  169.                       value.length > 9) {
  170.                     setState(() {
  171.                       _isValid = true;
  172.                     });
  173.                   } else {
  174.                     setState(() {
  175.                       _isValid = false;
  176.                     });
  177.                   }
  178.                   print(_isValid);
  179.                 },
  180.               ),
  181.             ),
  182.             Padding(
  183.               padding: const EdgeInsets.only(left: 42),
  184.               child: Text('Contoh: account@email.com',
  185.                   style: GoogleFonts.roboto(
  186.                       fontSize: 12,
  187.                       fontWeight: FontWeight.w400,
  188.                       color: Color(0xff505050))),
  189.             ),
  190.             Padding(
  191.                 padding: const EdgeInsets.fromLTRB(30, 18, 30, 0),
  192.                 child: CustomButton(
  193.                     title: 'Daftar',
  194.                     color: _isValid == true
  195.                         ? AppColors.grueLight
  196.                         : AppColors.greyLight,
  197.                     onPressed: () {
  198.                       if (_isValid == true) {
  199.                         showDialog(
  200.                             context: context,
  201.                             builder: (BuildContext context) {
  202.                               return CustomDialogRegister(
  203.                                 title: (EmailValidator.validate(
  204.                                         _fieldController.text))
  205.                                     ? _fieldController.text
  206.                                     : isValidPhoneNumber(_fieldController.text)
  207.                                         ? _fieldController.text
  208.                                         : _fieldController.text,
  209.                                 descriptions: (EmailValidator.validate(
  210.                                         _fieldController.text))
  211.                                     ? 'Apakah email yang anda masukkan sudah benar?'
  212.                                     : isValidPhoneNumber(_fieldController.text)
  213.                                         ? 'Apakah nomor telepon yang anda masukkan sudah benar?'
  214.                                         : 'Apakah Anda Manusia ?',
  215.                                 textYes: 'Ya, benar',
  216.                                 textNo: "Ubah",
  217.                                 onPressedYes: () {
  218.                                   if (EmailValidator.validate(
  219.                                       _fieldController.text)) {
  220.                                     BlocProvider.of<RegisterAccessEmailBloc>(
  221.                                             context)
  222.                                         .add(OnClickRegisterAccessEmail(
  223.                                             _fieldController.text));
  224.                                     Navigator.pop(context);
  225.                                   } else if (isValidPhoneNumber(
  226.                                       _fieldController.text)) {
  227.                                     BlocProvider.of<RegisterAccessPhoneBloc>(
  228.                                             context)
  229.                                         .add(OnClickRegisterAccess(
  230.                                             _fieldController.text));
  231.                                     Navigator.pop(context);
  232.                                   } else {
  233.                                     print('Invalid Email Or Phone Number');
  234.                                   }
  235.                                 },
  236.                                 onPressedNo: () {
  237.                                   Navigator.pop(context);
  238.                                 },
  239.                                 image: (EmailValidator.validate(
  240.                                         _fieldController.text))
  241.                                     ? AppImage.register_email
  242.                                     : isValidPhoneNumber(_fieldController.text)
  243.                                         ? AppImage.register_phone
  244.                                         : AppImage.register_email,
  245.                               );
  246.                             });
  247.                       }
  248.                     })),
  249.             Padding(
  250.               padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 10),
  251.               child: RichText(
  252.                 textAlign: TextAlign.center,
  253.                 text: TextSpan(
  254.                   text: 'Dengan mendaftar, saya menyetujui ',
  255.                   style: GoogleFonts.roboto(
  256.                     fontSize: 12,
  257.                     fontWeight: FontWeight.w400,
  258.                     color: Color(0xff2D2F2E),
  259.                   ),
  260.                   children: <TextSpan>[
  261.                     TextSpan(
  262.                         text: 'Syarat dan ketentuan ',
  263.                         style: GoogleFonts.roboto(
  264.                           fontSize: 12,
  265.                           fontWeight: FontWeight.w500,
  266.                           color: Color(0xff2D2F2E),
  267.                         )),
  268.                     TextSpan(
  269.                       text: ' yang berlaku di sistem kami',
  270.                       style: GoogleFonts.roboto(
  271.                         fontSize: 12,
  272.                         fontWeight: FontWeight.w400,
  273.                         color: Color(0xff2D2F2E),
  274.                       ),
  275.                     ),
  276.                   ],
  277.                 ),
  278.               ),
  279.             ),
  280.             Padding(
  281.               padding: const EdgeInsets.fromLTRB(30, 10, 30, 20),
  282.               child: IntrinsicHeight(
  283.                 child: Row(
  284.                   children: [
  285.                     Expanded(
  286.                       child: Container(
  287.                         margin: EdgeInsets.only(right: 10),
  288.                         height: 1,
  289.                         color: Color(0xffC4C4C4),
  290.                       ),
  291.                     ),
  292.                     Text(
  293.                       'atau daftar dengan metode lain',
  294.                       style: GoogleFonts.roboto(
  295.                         fontSize: 10,
  296.                         fontWeight: FontWeight.w400,
  297.                         color: Color(0xff2D2F2E),
  298.                       ),
  299.                     ),
  300.                     Expanded(
  301.                       child: Container(
  302.                         height: 1,
  303.                         margin: EdgeInsets.only(left: 10),
  304.                         color: Color(0xffC4C4C4),
  305.                       ),
  306.                     ),
  307.                   ],
  308.                 ),
  309.               ),
  310.             ),
  311.             Padding(
  312.               padding: EdgeInsets.symmetric(horizontal: 30),
  313.               child: Row(
  314.                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
  315.                   children: [
  316.                     GestureDetector(
  317.                       onTap: () {
  318.                         showModalBottomSheet(
  319.                             context: context,
  320.                             isScrollControlled: true,
  321.                             shape: RoundedRectangleBorder(
  322.                                 borderRadius: BorderRadius.vertical(
  323.                                     top: Radius.circular(24.0))),
  324.                             builder: (context) {
  325.                               return AnimatedPadding(
  326.                                 padding: MediaQuery.of(context).viewInsets,
  327.                                 duration: Duration(milliseconds: 100),
  328.                                 child: ForgotPasswordAccessPage(),
  329.                               );
  330.                             });
  331.                       },
  332.                       child: Container(
  333.                         height: 40,
  334.                         width: MediaQuery.of(context).size.width / 2 - 40,
  335.                         decoration: BoxDecoration(
  336.                             borderRadius: BorderRadius.circular(8),
  337.                             color: Color(0xffFEFEFE),
  338.                             boxShadow: [
  339.                               BoxShadow(
  340.                                   color: Colors.black12,
  341.                                   blurRadius: 4,
  342.                                   spreadRadius: 1,
  343.                                   offset: Offset(4, 4)) // Shadow ),
  344.                             ]),
  345.                         child: Padding(
  346.                           padding: const EdgeInsets.symmetric(
  347.                               horizontal: 24, vertical: 12),
  348.                           child: Row(
  349.                             mainAxisAlignment: MainAxisAlignment.center,
  350.                             children: [
  351.                               SvgPicture.asset(AppImage.google,
  352.                                   height: 24, width: 24, fit: BoxFit.scaleDown),
  353.                               SizedBox(width: 8),
  354.                               Text(
  355.                                 'Google',
  356.                                 style: GoogleFonts.roboto(
  357.                                   fontSize: 14,
  358.                                   fontWeight: FontWeight.w500,
  359.                                   color: Color(0xff2D2F2E),
  360.                                 ),
  361.                               ),
  362.                             ],
  363.                           ),
  364.                         ),
  365.                       ),
  366.                     ),
  367.                     Container(
  368.                       height: 40,
  369.                       width: MediaQuery.of(context).size.width / 2 - 40,
  370.                       decoration: BoxDecoration(
  371.                           borderRadius: BorderRadius.circular(8),
  372.                           color: Color(0xff395185),
  373.                           boxShadow: [
  374.                             BoxShadow(
  375.                                 color: Colors.black12,
  376.                                 blurRadius: 4,
  377.                                 spreadRadius: 1,
  378.                                 offset: Offset(4, 4)) // Shadow ),
  379.                           ]),
  380.                       child: Padding(
  381.                         padding: const EdgeInsets.symmetric(
  382.                             horizontal: 24, vertical: 12),
  383.                         child: Row(
  384.                           mainAxisAlignment: MainAxisAlignment.center,
  385.                           children: [
  386.                             SvgPicture.asset(AppImage.facebook,
  387.                                 height: 30, width: 30, fit: BoxFit.scaleDown),
  388.                             SizedBox(width: 8),
  389.                             Text(
  390.                               'Facebook',
  391.                               style: GoogleFonts.roboto(
  392.                                 fontSize: 14,
  393.                                 fontWeight: FontWeight.w500,
  394.                                 color: Color(0xffFEFEFE),
  395.                               ),
  396.                             ),
  397.                           ],
  398.                         ),
  399.                       ),
  400.                     )
  401.                   ]),
  402.             )
  403.           ]));
  404.         });
  405.       })),
  406.     );
  407.   }
  408.  
  409.   bool isValidPhoneNumber(String? string) {
  410.     if (string == null || string.isEmpty || string.length < 10) {
  411.       return false;
  412.     }
  413.  
  414.     const pattern = r'^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$';
  415.     final regExp = RegExp(pattern);
  416.  
  417.     if (!regExp.hasMatch(string)) {
  418.       return false;
  419.     }
  420.     return true;
  421.   }
  422. }
  423.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement