Advertisement
rifki_cs29

SubmitOTP

Mar 1st, 2022
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 26.26 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:flutter/gestures.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. import 'package:flutter_svg/svg.dart';
  6. import 'package:google_fonts/google_fonts.dart';
  7. import 'package:module_auth/domain/entities/register_access_email_success.dart';
  8. import 'package:module_auth/domain/entities/register_access_phone_success.dart';
  9. import 'package:module_auth/presentation/bloc/register_verification_email_bloc/register_verification_email_bloc.dart';
  10. import 'package:module_auth/presentation/bloc/register_verification_phone_bloc/register_verification_phone_bloc.dart';
  11. import 'package:module_auth/presentation/bloc/register_verify_email_bloc/register_verify_email_bloc.dart';
  12. import 'package:module_auth/presentation/bloc/register_verify_phone/register_verify_phone_bloc.dart';
  13. import 'package:module_auth/presentation/pages/register/complete_register_email_page.dart';
  14. import 'package:module_core/common/state_enum.dart';
  15. import 'package:module_core/common/string_extensions.dart';
  16. import 'package:module_core/custom_widgets/custom_toast/custom_toast.dart';
  17. import 'package:module_core/utils/app_colors.dart';
  18. import 'package:module_core/utils/app_image.dart';
  19.  
  20. import 'complete_register_phone_page.dart';
  21.  
  22. class SubmitOtpPage extends StatefulWidget {
  23.   final RegisterAccessEmailSuccess? dataEmail;
  24.   final RegisterAccessPhoneSuccess? dataPhone;
  25.   SubmitOtpPage(this.dataEmail, this.dataPhone);
  26.  
  27.   @override
  28.   State<SubmitOtpPage> createState() => _SubmitOtpPageState();
  29. }
  30.  
  31. class _SubmitOtpPageState extends State<SubmitOtpPage> {
  32.   final TextEditingController _fieldController =
  33.       TextEditingController(text: '');
  34.  
  35.   late Timer _timer;
  36.   int _second = 30;
  37.  
  38.   void startTimer() {
  39.     const oneSec = const Duration(seconds: 1);
  40.     _timer = new Timer.periodic(
  41.       oneSec,
  42.       (Timer timer) {
  43.         if (_second == 0) {
  44.           setState(() {
  45.             timer.cancel();
  46.           });
  47.         } else {
  48.           setState(() {
  49.             _second--;
  50.           });
  51.         }
  52.       },
  53.     );
  54.   }
  55.  
  56.   bool _isValid = false;
  57.  
  58.   @override
  59.   void initState() {
  60.     startTimer();
  61.     _isValid = false;
  62.     super.initState();
  63.   }
  64.  
  65.   @override
  66.   Widget build(BuildContext context) {
  67.     final String c = widget.dataEmail?.response.data?.c ?? '';
  68.     final String dEmail = widget.dataEmail?.response.data?.d ?? '';
  69.     final String h = widget.dataPhone?.response.data?.h ?? '';
  70.     final String dPhone = widget.dataPhone?.response.data?.d ?? '';
  71.     final String? type =
  72.         widget.dataEmail?.response.verificationMethods?[0].verificationType;
  73.     final String? email =
  74.         widget.dataEmail?.response.verificationMethods?[0].text ?? '';
  75.  
  76.     return MultiBlocListener(
  77.       listeners: [
  78.         BlocListener<RegisterVerifyEmailBloc, RegisterVerifyEmailState>(
  79.           listener: (context, state) {
  80.             if (state.state == RequestState.Loaded) {
  81.               Navigator.of(context).push(MaterialPageRoute(
  82.                 builder: (context) =>
  83.                     CompleteRegisterEmailPage(c: c, d: dEmail, email: email),
  84.               ));
  85.             } else if (state.state == RequestState.Error) {
  86.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  87.                   context: context);
  88.             }
  89.           },
  90.           listenWhen: (previousState, currentState) =>
  91.               currentState.state == RequestState.Loaded ||
  92.               currentState.state == RequestState.Error,
  93.         ),
  94.         BlocListener<RegisterVerifyPhoneBloc, RegisterVerifyPhoneState>(
  95.           listener: (context, state) {
  96.             if (state.state == RequestState.Loaded) {
  97.               Navigator.of(context).push(MaterialPageRoute(
  98.                 builder: (context) =>
  99.                     CompleteRegisterPhonePage(h: h, d: dPhone, phone: type),
  100.               ));
  101.             } else if (state.state == RequestState.Error) {
  102.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  103.                   context: context);
  104.             }
  105.           },
  106.           listenWhen: (previousState, currentState) =>
  107.               currentState.state == RequestState.Loaded ||
  108.               currentState.state == RequestState.Error,
  109.         ),
  110.       ],
  111.       child: Scaffold(
  112.         backgroundColor: Colors.white,
  113.         body: BlocBuilder<RegisterVerifyEmailBloc, RegisterVerifyEmailState>(
  114.           builder: (context, state) {
  115.             if (state.state == RequestState.Loading) {
  116.               return Center(
  117.                   child: Container(
  118.                 height: 250,
  119.                 width: double.infinity,
  120.                 child: Image.asset(AppImage.loadingGif),
  121.               ));
  122.             } else {
  123.               return BlocBuilder<RegisterVerifyPhoneBloc,
  124.                   RegisterVerifyPhoneState>(
  125.                 builder: (context, state) {
  126.                   if (state.state == RequestState.Loading) {
  127.                     return Center(
  128.                         child: Container(
  129.                       height: 250,
  130.                       width: double.infinity,
  131.                       child: Image.asset(AppImage.loadingGif),
  132.                     ));
  133.                   } else {
  134.                     return SafeArea(
  135.                       child: ListView(
  136.                         children: [
  137.                           Stack(
  138.                             children: [
  139.                               Container(
  140.                                 height: 72,
  141.                                 decoration: BoxDecoration(
  142.                                   border: Border(
  143.                                     bottom: BorderSide(
  144.                                         width: 1.0, color: Color(0xffC4C4C4)),
  145.                                   ),
  146.                                   color: Colors.white,
  147.                                 ),
  148.                                 child: Padding(
  149.                                   padding:
  150.                                       const EdgeInsets.only(left: 30, top: 18),
  151.                                   child: Row(
  152.                                     children: [
  153.                                       GestureDetector(
  154.                                         onTap: () => Navigator.pop(context),
  155.                                         child: Container(
  156.                                           width: 32,
  157.                                           height: 32,
  158.                                           decoration: BoxDecoration(
  159.                                             color: AppColors.greyLight,
  160.                                             borderRadius:
  161.                                                 BorderRadius.circular(18),
  162.                                           ),
  163.                                           child: SvgPicture.asset(
  164.                                               AppImage.back_button,
  165.                                               height: 24,
  166.                                               width: 24,
  167.                                               fit: BoxFit.scaleDown),
  168.                                         ),
  169.                                       ),
  170.                                     ],
  171.                                   ),
  172.                                 ),
  173.                               ),
  174.                               Center(
  175.                                 child: Padding(
  176.                                   padding: const EdgeInsets.only(top: 32),
  177.                                   child: Text(
  178.                                     'Verifikasi',
  179.                                     style: GoogleFonts.poppins(
  180.                                       fontSize: 16,
  181.                                       fontWeight: FontWeight.w600,
  182.                                       color: Color(0xff2D2F2E),
  183.                                     ),
  184.                                     textAlign: TextAlign.center,
  185.                                   ),
  186.                                 ),
  187.                               ),
  188.                             ],
  189.                           ),
  190.                           SizedBox(height: 24),
  191.                           Column(
  192.                             children: [
  193.                               Text(
  194.                                 'Masukkan Kode Verifikasi',
  195.                                 style: GoogleFonts.roboto(
  196.                                   fontSize: 14,
  197.                                   fontWeight: FontWeight.w500,
  198.                                   color: Color(0xff2D2F2E),
  199.                                 ),
  200.                               ),
  201.                               SizedBox(height: 8),
  202.                               Text(
  203.                                 (type == 'email')
  204.                                     ? 'Kode verifikasi telah dikirim melalui email ke'
  205.                                     : 'Kode verifikasi telah dikirim melalui SMS ke',
  206.                                 textAlign: TextAlign.center,
  207.                                 style: GoogleFonts.roboto(
  208.                                   fontSize: 12,
  209.                                   fontWeight: FontWeight.w400,
  210.                                   color: Color(0xff2D2F2E),
  211.                                 ),
  212.                               ),
  213.                               SizedBox(height: 6),
  214.                               Text(
  215.                                 (type == 'email')
  216.                                     ? (widget.dataEmail?.response
  217.                                             .verificationMethods?[0].text ??
  218.                                         'email@email.com')
  219.                                     : (widget
  220.                                             .dataPhone
  221.                                             ?.response
  222.                                             .verificationMethods?[0]
  223.                                             .verificationType ??
  224.                                         '08123456789'),
  225.                                 textAlign: TextAlign.center,
  226.                                 style: GoogleFonts.roboto(
  227.                                   fontSize: 12,
  228.                                   fontWeight: FontWeight.w500,
  229.                                   color: Color(0xff2D2F2E),
  230.                                 ),
  231.                               ),
  232.                               Padding(
  233.                                 padding: const EdgeInsets.only(top: 24),
  234.                                 child: Container(
  235.                                   height: 110,
  236.                                   width: 110,
  237.                                   decoration: BoxDecoration(
  238.                                     image: DecorationImage(
  239.                                       image: AssetImage(AppImage.submit_otp),
  240.                                       fit: BoxFit.cover,
  241.                                     ),
  242.                                   ),
  243.                                 ),
  244.                               ),
  245.                               Container(
  246.                                 margin: EdgeInsets.symmetric(
  247.                                     horizontal: 54, vertical: 36),
  248.                                 child: Column(
  249.                                   crossAxisAlignment: CrossAxisAlignment.start,
  250.                                   children: [
  251.                                     TextFormField(
  252.                                       textCapitalization:
  253.                                           TextCapitalization.characters,
  254.                                       inputFormatters: [
  255.                                         UpperCaseTextFormatter(),
  256.                                       ],
  257.                                       autofocus: false,
  258.                                       controller: _fieldController,
  259.                                       cursorColor: Colors.black,
  260.                                       textAlign: TextAlign.center,
  261.                                       onChanged: (value) {
  262.                                         if (value.length == 6) {
  263.                                           setState(() {
  264.                                             _isValid = true;
  265.                                           });
  266.                                         } else {
  267.                                           setState(() {
  268.                                             _isValid = false;
  269.                                           });
  270.                                         }
  271.                                       },
  272.                                       style: GoogleFonts.roboto(
  273.                                           fontSize: 16,
  274.                                           color: Color(0xffAEAFB2)),
  275.                                       decoration: InputDecoration(
  276.                                         filled: true,
  277.                                         fillColor:
  278.                                             Color(0xffC6E4EC).withOpacity(0.5),
  279.                                         hintText: 'Kode OTP',
  280.                                         border: OutlineInputBorder(),
  281.                                         focusedBorder: OutlineInputBorder(
  282.                                           borderSide: BorderSide(
  283.                                             color: AppColors.orangeUkur,
  284.                                           ),
  285.                                         ),
  286.                                         enabledBorder: OutlineInputBorder(
  287.                                           borderSide: BorderSide(
  288.                                               color: Color(0xffAEAFB2)
  289.                                                   .withOpacity(0.5)),
  290.                                         ),
  291.                                       ),
  292.                                     ),
  293.                                   ],
  294.                                 ),
  295.                               ),
  296.                               SizedBox(height: 4),
  297.                               Container(
  298.                                 width: double.infinity,
  299.                                 height: 48,
  300.                                 margin: EdgeInsets.symmetric(horizontal: 30),
  301.                                 child: TextButton(
  302.                                   onPressed: () {
  303.                                     if (_isValid == true) {
  304.                                       (type == 'email')
  305.                                           ? BlocProvider.of<
  306.                                                       RegisterVerifyEmailBloc>(
  307.                                                   context)
  308.                                               .add(OnClickRegisterVerifyEmail(
  309.                                                   c,
  310.                                                   dEmail,
  311.                                                   _fieldController.text))
  312.                                           : BlocProvider.of<
  313.                                                       RegisterVerifyPhoneBloc>(
  314.                                                   context)
  315.                                               .add(OnClickRegisterVerifyPhone(
  316.                                                   h,
  317.                                                   dPhone,
  318.                                                   _fieldController.text));
  319.                                     }
  320.                                   },
  321.                                   style: TextButton.styleFrom(
  322.                                     backgroundColor: _isValid == true
  323.                                         ? Color(0xffF7B200)
  324.                                         : Colors.grey,
  325.                                     shape: RoundedRectangleBorder(
  326.                                       borderRadius: BorderRadius.circular(20),
  327.                                     ),
  328.                                   ),
  329.                                   child: Row(
  330.                                     mainAxisAlignment: MainAxisAlignment.center,
  331.                                     children: [
  332.                                       Icon(Icons.check_circle_outline,
  333.                                           color: Colors.white, size: 20),
  334.                                       SizedBox(width: 8),
  335.                                       Text(
  336.                                         'VERIFIKASI',
  337.                                         style: GoogleFonts.roboto(
  338.                                           fontSize: 16,
  339.                                           fontWeight: FontWeight.w500,
  340.                                           color: Colors.white,
  341.                                         ),
  342.                                       ),
  343.                                     ],
  344.                                   ),
  345.                                 ),
  346.                               ),
  347.                               Padding(
  348.                                 padding: const EdgeInsets.only(top: 18),
  349.                                 child: (_second != 0)
  350.                                     ? RichText(
  351.                                         textAlign: TextAlign.center,
  352.                                         text: TextSpan(
  353.                                           text:
  354.                                               'Mohon tunggu dalam ${_second} detik untuk ',
  355.                                           style: GoogleFonts.roboto(
  356.                                             fontSize: 12,
  357.                                             fontWeight: FontWeight.w400,
  358.                                             color: Color(0xff2D2F2E),
  359.                                           ),
  360.                                           children: <TextSpan>[
  361.                                             TextSpan(
  362.                                                 text: 'Kirim Ulang',
  363.                                                 style: GoogleFonts.roboto(
  364.                                                   fontSize: 12,
  365.                                                   fontWeight: FontWeight.w400,
  366.                                                   color: Color(0xff2D2F2E),
  367.                                                 )),
  368.                                           ],
  369.                                         ),
  370.                                       )
  371.                                     : MultiBlocListener(
  372.                                         listeners: [
  373.                                           BlocListener<
  374.                                               RegisterVerificationEmailBloc,
  375.                                               RegisterVerificationEmailState>(
  376.                                             listener: (context, state) {
  377.                                               if (state.state ==
  378.                                                   RequestState.Loaded) {
  379.                                                 Navigator.pop(context);
  380.                                                 CustomToast.ShowCustomWidgetToast(
  381.                                                     'OTP Berhasil Dikirim Ulang',
  382.                                                     context: context);
  383.                                               } else if (state.state ==
  384.                                                   RequestState.Error) {
  385.                                                 CustomToast
  386.                                                     .ShowCustomWidgetToast(
  387.                                                         '${state.errorMessage}',
  388.                                                         context: context);
  389.                                               }
  390.                                             },
  391.                                             listenWhen:
  392.                                                 (previousState, currentState) =>
  393.                                                     currentState.state ==
  394.                                                         RequestState.Loaded ||
  395.                                                     currentState.state ==
  396.                                                         RequestState.Error,
  397.                                           ),
  398.                                           BlocListener<
  399.                                               RegisterVerificationPhoneBloc,
  400.                                               RegisterVerificationPhoneState>(
  401.                                             listener: (context, state) {
  402.                                               if (state.state ==
  403.                                                   RequestState.Loaded) {
  404.                                                 Navigator.pop(context);
  405.                                                 CustomToast.ShowCustomWidgetToast(
  406.                                                     'OTP Berhasil Dikirim Ulang',
  407.                                                     context: context);
  408.                                               } else if (state.state ==
  409.                                                   RequestState.Error) {
  410.                                                 CustomToast
  411.                                                     .ShowCustomWidgetToast(
  412.                                                         '${state.errorMessage}',
  413.                                                         context: context);
  414.                                               }
  415.                                             },
  416.                                             listenWhen:
  417.                                                 (previousState, currentState) =>
  418.                                                     currentState.state ==
  419.                                                         RequestState.Loaded ||
  420.                                                     currentState.state ==
  421.                                                         RequestState.Error,
  422.                                           ),
  423.                                         ],
  424.                                         child: BlocBuilder<
  425.                                             RegisterVerificationEmailBloc,
  426.                                             RegisterVerificationEmailState>(
  427.                                           builder: (context, state) {
  428.                                             return BlocBuilder<
  429.                                                 RegisterVerificationPhoneBloc,
  430.                                                 RegisterVerificationPhoneState>(
  431.                                               builder: (context, state) {
  432.                                                 return RichText(
  433.                                                   textAlign: TextAlign.center,
  434.                                                   text: TextSpan(
  435.                                                     text:
  436.                                                         'Tidak Menerima kode? ',
  437.                                                     style: GoogleFonts.roboto(
  438.                                                       fontSize: 12,
  439.                                                       fontWeight:
  440.                                                           FontWeight.w400,
  441.                                                       color: Color(0xff2D2F2E),
  442.                                                     ),
  443.                                                     children: <TextSpan>[
  444.                                                       TextSpan(
  445.                                                           recognizer:
  446.                                                               TapGestureRecognizer()
  447.                                                                 ..onTap = () {
  448.                                                                   _second = 30;
  449.                                                                   startTimer();
  450.                                                                   (type ==
  451.                                                                           'email')
  452.                                                                       ? BlocProvider.of<RegisterVerificationEmailBloc>(
  453.                                                                               context)
  454.                                                                           .add(OnClickRegisterVerificationEmail(
  455.                                                                               c,
  456.                                                                               dEmail))
  457.                                                                       : BlocProvider.of<RegisterVerificationPhoneBloc>(
  458.                                                                               context)
  459.                                                                           .add(OnClickRegisterVerificationPhone(
  460.                                                                               h,
  461.                                                                               dPhone));
  462.                                                                 },
  463.                                                           text: 'Kirim Ulang',
  464.                                                           style: GoogleFonts
  465.                                                               .roboto(
  466.                                                             fontSize: 12,
  467.                                                             fontWeight:
  468.                                                                 FontWeight.w500,
  469.                                                             color: AppColors
  470.                                                                 .grueLight,
  471.                                                           )),
  472.                                                     ],
  473.                                                   ),
  474.                                                 );
  475.                                               },
  476.                                             );
  477.                                           },
  478.                                         ),
  479.                                       ),
  480.                               ),
  481.                             ],
  482.                           ),
  483.                         ],
  484.                       ),
  485.                     );
  486.                   }
  487.                 },
  488.               );
  489.             }
  490.           },
  491.         ),
  492.       ),
  493.     );
  494.   }
  495.  
  496.   @override
  497.   void dispose() {
  498.     _timer.cancel();
  499.     _fieldController.dispose();
  500.     super.dispose();
  501.   }
  502. }
  503.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement