Advertisement
rifki_cs29

SubmitOTP

Mar 1st, 2022
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 18.13 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'package:flutter_svg/svg.dart';
  4. import 'package:google_fonts/google_fonts.dart';
  5. import 'package:module_auth/domain/entities/register_access_email_success.dart';
  6. import 'package:module_auth/domain/entities/register_access_phone_success.dart';
  7. import 'package:module_auth/presentation/bloc/register_verification_email_bloc/register_verification_email_bloc.dart';
  8. import 'package:module_auth/presentation/bloc/register_verification_phone_bloc/register_verification_phone_bloc.dart';
  9. import 'package:module_auth/presentation/bloc/register_verify_email_bloc/register_verify_email_bloc.dart';
  10. import 'package:module_auth/presentation/bloc/register_verify_phone/register_verify_phone_bloc.dart';
  11. import 'package:module_auth/presentation/pages/register/complete_register_email_page.dart';
  12. import 'package:module_auth/presentation/widgets/register_resend_otp.dart';
  13. import 'package:module_core/common/state_enum.dart';
  14. import 'package:module_core/common/string_extensions.dart';
  15. import 'package:module_core/custom_widgets/custom_toast/custom_toast.dart';
  16. import 'package:module_core/utils/app_colors.dart';
  17. import 'package:module_core/utils/app_image.dart';
  18.  
  19. import 'complete_register_phone_page.dart';
  20.  
  21. class SubmitOtpPage extends StatefulWidget {
  22.   final RegisterAccessEmailSuccess? dataEmail;
  23.   final RegisterAccessPhoneSuccess? dataPhone;
  24.   SubmitOtpPage(this.dataEmail, this.dataPhone);
  25.  
  26.   @override
  27.   State<SubmitOtpPage> createState() => _SubmitOtpPageState();
  28. }
  29.  
  30. class _SubmitOtpPageState extends State<SubmitOtpPage> {
  31.   final TextEditingController _fieldController =
  32.       TextEditingController(text: '');
  33.  
  34.   bool _isValid = false;
  35.  
  36.   @override
  37.   void initState() {
  38.     _isValid = false;
  39.     super.initState();
  40.   }
  41.  
  42.   @override
  43.   Widget build(BuildContext context) {
  44.     final String c = widget.dataEmail?.response.data?.c ?? '';
  45.     final String dEmail = widget.dataEmail?.response.data?.d ?? '';
  46.     final String h = widget.dataPhone?.response.data?.h ?? '';
  47.     final String dPhone = widget.dataPhone?.response.data?.d ?? '';
  48.     final String? type =
  49.         widget.dataEmail?.response.verificationMethods?[0].verificationType;
  50.     final String? email =
  51.         widget.dataEmail?.response.verificationMethods?[0].text ?? '';
  52.  
  53.     return MultiBlocListener(
  54.       listeners: [
  55.         BlocListener<RegisterVerifyEmailBloc, RegisterVerifyEmailState>(
  56.           listener: (context, state) {
  57.             if (state.state == RequestState.Loaded) {
  58.               Navigator.of(context).push(MaterialPageRoute(
  59.                 builder: (context) =>
  60.                     CompleteRegisterEmailPage(c: c, d: dEmail, email: email),
  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.         BlocListener<RegisterVerifyPhoneBloc, RegisterVerifyPhoneState>(
  72.           listener: (context, state) {
  73.             if (state.state == RequestState.Loaded) {
  74.               Navigator.of(context).push(MaterialPageRoute(
  75.                 builder: (context) =>
  76.                     CompleteRegisterPhonePage(h: h, d: dPhone, phone: type),
  77.               ));
  78.             } else if (state.state == RequestState.Error) {
  79.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  80.                   context: context);
  81.             }
  82.           },
  83.           listenWhen: (previousState, currentState) =>
  84.               currentState.state == RequestState.Loaded ||
  85.               currentState.state == RequestState.Error,
  86.         ),
  87.         BlocListener<RegisterVerificationEmailBloc,
  88.             RegisterVerificationEmailState>(
  89.           listener: (context, state) {
  90.             if (state.state == RequestState.Loaded) {
  91.               CustomToast.ShowCustomWidgetToast('OTP Berhasil Dikirim Ulang',
  92.                   context: context);
  93.             } else if (state.state == RequestState.Error) {
  94.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  95.                   context: context);
  96.             }
  97.           },
  98.           listenWhen: (previousState, currentState) =>
  99.               currentState.state == RequestState.Loaded ||
  100.               currentState.state == RequestState.Error,
  101.         ),
  102.         BlocListener<RegisterVerificationPhoneBloc,
  103.             RegisterVerificationPhoneState>(
  104.           listener: (context, state) {
  105.             if (state.state == RequestState.Loaded) {
  106.               CustomToast.ShowCustomWidgetToast('OTP Berhasil Dikirim Ulang',
  107.                   context: context);
  108.             } else if (state.state == RequestState.Error) {
  109.               CustomToast.ShowCustomWidgetToast('${state.errorMessage}',
  110.                   context: context);
  111.             }
  112.           },
  113.           listenWhen: (previousState, currentState) =>
  114.               currentState.state == RequestState.Loaded ||
  115.               currentState.state == RequestState.Error,
  116.         ),
  117.       ],
  118.       child: Scaffold(
  119.         backgroundColor: Colors.white,
  120.         body: BlocBuilder<RegisterVerifyEmailBloc, RegisterVerifyEmailState>(
  121.           builder: (context, state) {
  122.             if (state.state == RequestState.Loading) {
  123.               return Center(
  124.                   child: Container(
  125.                 height: 250,
  126.                 width: double.infinity,
  127.                 child: Image.asset(AppImage.loadingGif),
  128.               ));
  129.             } else {
  130.               return BlocBuilder<RegisterVerifyPhoneBloc,
  131.                   RegisterVerifyPhoneState>(
  132.                 builder: (context, state) {
  133.                   if (state.state == RequestState.Loading) {
  134.                     return Center(
  135.                         child: Container(
  136.                       height: 250,
  137.                       width: double.infinity,
  138.                       child: Image.asset(AppImage.loadingGif),
  139.                     ));
  140.                   } else {
  141.                     return SafeArea(
  142.                       child: ListView(
  143.                         children: [
  144.                           Stack(
  145.                             children: [
  146.                               Container(
  147.                                 height: 72,
  148.                                 decoration: BoxDecoration(
  149.                                   border: Border(
  150.                                     bottom: BorderSide(
  151.                                         width: 1.0, color: Color(0xffC4C4C4)),
  152.                                   ),
  153.                                   color: Colors.white,
  154.                                 ),
  155.                                 child: Padding(
  156.                                   padding:
  157.                                       const EdgeInsets.only(left: 30, top: 18),
  158.                                   child: Row(
  159.                                     children: [
  160.                                       GestureDetector(
  161.                                         onTap: () => Navigator.pop(context),
  162.                                         child: Container(
  163.                                           width: 32,
  164.                                           height: 32,
  165.                                           decoration: BoxDecoration(
  166.                                             color: AppColors.greyLight,
  167.                                             borderRadius:
  168.                                                 BorderRadius.circular(18),
  169.                                           ),
  170.                                           child: SvgPicture.asset(
  171.                                               AppImage.back_button,
  172.                                               height: 24,
  173.                                               width: 24,
  174.                                               fit: BoxFit.scaleDown),
  175.                                         ),
  176.                                       ),
  177.                                     ],
  178.                                   ),
  179.                                 ),
  180.                               ),
  181.                               Center(
  182.                                 child: Padding(
  183.                                   padding: const EdgeInsets.only(top: 32),
  184.                                   child: Text(
  185.                                     'Verifikasi',
  186.                                     style: GoogleFonts.poppins(
  187.                                       fontSize: 16,
  188.                                       fontWeight: FontWeight.w600,
  189.                                       color: Color(0xff2D2F2E),
  190.                                     ),
  191.                                     textAlign: TextAlign.center,
  192.                                   ),
  193.                                 ),
  194.                               ),
  195.                             ],
  196.                           ),
  197.                           SizedBox(height: 24),
  198.                           Column(
  199.                             children: [
  200.                               Text(
  201.                                 'Masukkan Kode Verifikasi',
  202.                                 style: GoogleFonts.roboto(
  203.                                   fontSize: 14,
  204.                                   fontWeight: FontWeight.w500,
  205.                                   color: Color(0xff2D2F2E),
  206.                                 ),
  207.                               ),
  208.                               SizedBox(height: 8),
  209.                               Text(
  210.                                 (type == 'email')
  211.                                     ? 'Kode verifikasi telah dikirim melalui email ke'
  212.                                     : 'Kode verifikasi telah dikirim melalui SMS ke',
  213.                                 textAlign: TextAlign.center,
  214.                                 style: GoogleFonts.roboto(
  215.                                   fontSize: 12,
  216.                                   fontWeight: FontWeight.w400,
  217.                                   color: Color(0xff2D2F2E),
  218.                                 ),
  219.                               ),
  220.                               SizedBox(height: 6),
  221.                               Text(
  222.                                 (type == 'email')
  223.                                     ? (widget.dataEmail?.response
  224.                                             .verificationMethods?[0].text ??
  225.                                         'email@email.com')
  226.                                     : (widget
  227.                                             .dataPhone
  228.                                             ?.response
  229.                                             .verificationMethods?[0]
  230.                                             .verificationType ??
  231.                                         '08123456789'),
  232.                                 textAlign: TextAlign.center,
  233.                                 style: GoogleFonts.roboto(
  234.                                   fontSize: 12,
  235.                                   fontWeight: FontWeight.w500,
  236.                                   color: Color(0xff2D2F2E),
  237.                                 ),
  238.                               ),
  239.                               Padding(
  240.                                 padding: const EdgeInsets.only(top: 24),
  241.                                 child: Container(
  242.                                   height: 110,
  243.                                   width: 110,
  244.                                   decoration: BoxDecoration(
  245.                                     image: DecorationImage(
  246.                                       image: AssetImage(AppImage.submit_otp),
  247.                                       fit: BoxFit.cover,
  248.                                     ),
  249.                                   ),
  250.                                 ),
  251.                               ),
  252.                               Container(
  253.                                 margin: EdgeInsets.symmetric(
  254.                                     horizontal: 54, vertical: 36),
  255.                                 child: Column(
  256.                                   crossAxisAlignment: CrossAxisAlignment.start,
  257.                                   children: [
  258.                                     TextFormField(
  259.                                       textCapitalization:
  260.                                           TextCapitalization.characters,
  261.                                       inputFormatters: [
  262.                                         UpperCaseTextFormatter(),
  263.                                       ],
  264.                                       autofocus: false,
  265.                                       controller: _fieldController,
  266.                                       cursorColor: Colors.black,
  267.                                       textAlign: TextAlign.center,
  268.                                       onChanged: (value) {
  269.                                         if (value.length == 6) {
  270.                                           setState(() {
  271.                                             _isValid = true;
  272.                                           });
  273.                                         } else {
  274.                                           setState(() {
  275.                                             _isValid = false;
  276.                                           });
  277.                                         }
  278.                                       },
  279.                                       style: GoogleFonts.roboto(
  280.                                           fontSize: 16,
  281.                                           color: Color(0xffAEAFB2)),
  282.                                       decoration: InputDecoration(
  283.                                         filled: true,
  284.                                         fillColor:
  285.                                             Color(0xffC6E4EC).withOpacity(0.5),
  286.                                         hintText: 'Kode OTP',
  287.                                         border: OutlineInputBorder(),
  288.                                         focusedBorder: OutlineInputBorder(
  289.                                           borderSide: BorderSide(
  290.                                             color: AppColors.orangeUkur,
  291.                                           ),
  292.                                         ),
  293.                                         enabledBorder: OutlineInputBorder(
  294.                                           borderSide: BorderSide(
  295.                                               color: Color(0xffAEAFB2)
  296.                                                   .withOpacity(0.5)),
  297.                                         ),
  298.                                       ),
  299.                                     ),
  300.                                   ],
  301.                                 ),
  302.                               ),
  303.                               SizedBox(height: 4),
  304.                               Container(
  305.                                 width: double.infinity,
  306.                                 height: 48,
  307.                                 margin: EdgeInsets.symmetric(horizontal: 30),
  308.                                 child: TextButton(
  309.                                   onPressed: () {
  310.                                     if (_isValid == true) {
  311.                                       (type == 'email')
  312.                                           ? BlocProvider.of<
  313.                                                       RegisterVerifyEmailBloc>(
  314.                                                   context)
  315.                                               .add(OnClickRegisterVerifyEmail(
  316.                                                   c,
  317.                                                   dEmail,
  318.                                                   _fieldController.text))
  319.                                           : BlocProvider.of<
  320.                                                       RegisterVerifyPhoneBloc>(
  321.                                                   context)
  322.                                               .add(OnClickRegisterVerifyPhone(
  323.                                                   h,
  324.                                                   dPhone,
  325.                                                   _fieldController.text));
  326.                                     }
  327.                                   },
  328.                                   style: TextButton.styleFrom(
  329.                                     backgroundColor: _isValid == true
  330.                                         ? Color(0xffF7B200)
  331.                                         : Colors.grey,
  332.                                     shape: RoundedRectangleBorder(
  333.                                       borderRadius: BorderRadius.circular(20),
  334.                                     ),
  335.                                   ),
  336.                                   child: Row(
  337.                                     mainAxisAlignment: MainAxisAlignment.center,
  338.                                     children: [
  339.                                       Icon(Icons.check_circle_outline,
  340.                                           color: Colors.white, size: 20),
  341.                                       SizedBox(width: 8),
  342.                                       Text(
  343.                                         'VERIFIKASI',
  344.                                         style: GoogleFonts.roboto(
  345.                                           fontSize: 16,
  346.                                           fontWeight: FontWeight.w500,
  347.                                           color: Colors.white,
  348.                                         ),
  349.                                       ),
  350.                                     ],
  351.                                   ),
  352.                                 ),
  353.                               ),
  354.                               RegisterResendOtp(
  355.                                   widget.dataEmail, widget.dataPhone),
  356.                             ],
  357.                           ),
  358.                         ],
  359.                       ),
  360.                     );
  361.                   }
  362.                 },
  363.               );
  364.             }
  365.           },
  366.         ),
  367.       ),
  368.     );
  369.   }
  370.  
  371.   @override
  372.   void dispose() {
  373.     _fieldController.dispose();
  374.     super.dispose();
  375.   }
  376. }
  377.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement