Advertisement
rifki_cs29

resendotp

Mar 1st, 2022
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.31 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:google_fonts/google_fonts.dart';
  6. import 'package:module_auth/domain/entities/register_access_email_success.dart';
  7. import 'package:module_auth/domain/entities/register_access_phone_success.dart';
  8. import 'package:module_auth/presentation/bloc/register_verification_email_bloc/register_verification_email_bloc.dart';
  9. import 'package:module_auth/presentation/bloc/register_verification_phone_bloc/register_verification_phone_bloc.dart';
  10. import 'package:module_core/utils/app_colors.dart';
  11.  
  12. class RegisterResendOtp extends StatefulWidget {
  13.   final RegisterAccessEmailSuccess? dataEmail;
  14.   final RegisterAccessPhoneSuccess? dataPhone;
  15.   const RegisterResendOtp(this.dataEmail, this.dataPhone);
  16.  
  17.   @override
  18.   _RegisterResendOtpState createState() => _RegisterResendOtpState();
  19. }
  20.  
  21. class _RegisterResendOtpState extends State<RegisterResendOtp> {
  22.   late Timer _timer;
  23.   int _second = 30;
  24.  
  25.   void startTimer() {
  26.     const oneSec = const Duration(seconds: 1);
  27.     _timer = new Timer.periodic(
  28.       oneSec,
  29.       (Timer timer) {
  30.         if (_second == 0) {
  31.           setState(() {
  32.             timer.cancel();
  33.           });
  34.         } else {
  35.           setState(() {
  36.             _second--;
  37.           });
  38.         }
  39.       },
  40.     );
  41.   }
  42.  
  43.   @override
  44.   void initState() {
  45.     startTimer();
  46.     super.initState();
  47.   }
  48.  
  49.   @override
  50.   Widget build(BuildContext context) {
  51.     final String c = widget.dataEmail?.response.data?.c ?? '';
  52.     final String dEmail = widget.dataEmail?.response.data?.d ?? '';
  53.     final String h = widget.dataPhone?.response.data?.h ?? '';
  54.     final String dPhone = widget.dataPhone?.response.data?.d ?? '';
  55.     final String? type =
  56.         widget.dataEmail?.response.verificationMethods?[0].verificationType;
  57.  
  58.     return Padding(
  59.       padding: const EdgeInsets.only(top: 18),
  60.       child: (_second != 0)
  61.           ? RichText(
  62.               textAlign: TextAlign.center,
  63.               text: TextSpan(
  64.                 text: 'Mohon tunggu dalam ${_second} detik untuk ',
  65.                 style: GoogleFonts.roboto(
  66.                   fontSize: 12,
  67.                   fontWeight: FontWeight.w400,
  68.                   color: Color(0xff2D2F2E),
  69.                 ),
  70.                 children: <TextSpan>[
  71.                   TextSpan(
  72.                       text: 'Kirim Ulang',
  73.                       style: GoogleFonts.roboto(
  74.                         fontSize: 12,
  75.                         fontWeight: FontWeight.w400,
  76.                         color: Color(0xff2D2F2E),
  77.                       )),
  78.                 ],
  79.               ),
  80.             )
  81.           : RichText(
  82.               textAlign: TextAlign.center,
  83.               text: TextSpan(
  84.                 text: 'Tidak Menerima kode? ',
  85.                 style: GoogleFonts.roboto(
  86.                   fontSize: 12,
  87.                   fontWeight: FontWeight.w400,
  88.                   color: Color(0xff2D2F2E),
  89.                 ),
  90.                 children: <TextSpan>[
  91.                   TextSpan(
  92.                       recognizer: TapGestureRecognizer()
  93.                         ..onTap = () {
  94.                           _second = 30;
  95.                           startTimer();
  96.                           (type == 'email')
  97.                               ? context
  98.                                   .read<RegisterVerificationEmailBloc>()
  99.                                   .add(
  100.                                     OnClickRegisterVerificationEmail(c, dEmail),
  101.                                   )
  102.                               : context
  103.                                   .read<RegisterVerificationPhoneBloc>()
  104.                                   .add(
  105.                                     OnClickRegisterVerificationPhone(h, dPhone),
  106.                                   );
  107.                         },
  108.                       text: 'Kirim Ulang',
  109.                       style: GoogleFonts.roboto(
  110.                         fontSize: 12,
  111.                         fontWeight: FontWeight.w500,
  112.                         color: AppColors.grueLight,
  113.                       )),
  114.                 ],
  115.               ),
  116.             ),
  117.     );
  118.   }
  119.  
  120.   @override
  121.   void dispose() {
  122.     _timer.cancel();
  123.     super.dispose();
  124.   }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement