show2fa static method

Future<String?> show2fa(
  1. BuildContext context, {
  2. Future<String?> onVerify(
    1. String
    )?,
  3. FormFieldValidator<String>? validator,
})

Implementation

static Future<String?> show2fa(
  BuildContext context, {
  Future<String?> Function(String)? onVerify,
  FormFieldValidator<String>? validator,
}) {
  final GlobalKey<_OtpShadInputState> otpKey =
      GlobalKey<_OtpShadInputState>();

  return showGeneralDialog<String>(
    context: context,
    barrierDismissible: false,
    barrierLabel: '',
    barrierColor: ColorSchemes.lightColor.background.withValues(alpha: 0),
    transitionDuration: const Duration(milliseconds: 300),
    pageBuilder: (dialogContext, animation, secondaryAnimation) {
      return Scaffold(
        backgroundColor: ColorSchemes.lightColor.background,
        appBar: AppBar(
          backgroundColor: ColorSchemes.lightColor.background,
          elevation: 0,
          leading: IconButton(
            icon: Iconify(
              Lucide.arrow_left,
              color: ColorSchemes.lightColor.foreground,
            ),
            onPressed: () => Navigator.of(dialogContext).pop(),
          ),
          title: Text(
            'การยืนยันตัวตนแบบสองชั้น',
            style: StyleText.fontBodyLargeProminent(),
          ),
          centerTitle: true,
        ),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(24),
            child: Column(
              children: [
                Text(
                  'ป้อนรหัสยืนยันที่คุณเห็นในแอปยืนยันตัวตน',
                  style: StyleText.fontBodyMedium(),
                ),
                const SizedBox(height: 32),
                OtpShadInput(
                  key: otpKey,
                  autofocus: true,
                  onCompleted: (value) async {
                    if (onVerify != null) {
                      final String? errorMessage = await onVerify(value);

                      if (errorMessage == null) {
                        if (dialogContext.mounted) {
                          Navigator.of(dialogContext).pop(value);
                        }
                      } else {
                        otpKey.currentState?.showError(errorMessage);
                      }
                    } else {
                      if (dialogContext.mounted) {
                        Navigator.of(dialogContext).pop(value);
                      }
                    }
                  },
                  validator:
                      validator ??
                      (value) {
                        if (value == null || value.isEmpty) {
                          return 'กรุณากรอกรหัส OTP';
                        }
                        if (value.length < 6) {
                          return 'กรุณากรอกรหัสให้ครบ 6 หลัก';
                        }
                        return null;
                      },
                ),
              ],
            ),
          ),
        ),
      );
    },
    transitionBuilder: (context, animation, secondaryAnimation, child) {
      const begin = Offset(1.0, 0.0);
      const end = Offset.zero;
      const curve = Curves.easeInOut;
      var tween = Tween(
        begin: begin,
        end: end,
      ).chain(CurveTween(curve: curve));
      return SlideTransition(position: animation.drive(tween), child: child);
    },
  );
}