From 575d2e73fe78e7e2e1214018be0c6ee14a84ff2f Mon Sep 17 00:00:00 2001 From: Mikolaj Date: Sun, 22 Oct 2023 00:24:24 +0200 Subject: [PATCH] Adding mechanism to fix copy paste behaviour for otp input --- .../lib/view/widgets/welcome/otp_input.dart | 19 +++++++++++++++++++ .../view/widgets/welcome/otp_input_field.dart | 4 +++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/recipients_app/lib/view/widgets/welcome/otp_input.dart b/recipients_app/lib/view/widgets/welcome/otp_input.dart index 2c1633c01..d8413b032 100644 --- a/recipients_app/lib/view/widgets/welcome/otp_input.dart +++ b/recipients_app/lib/view/widgets/welcome/otp_input.dart @@ -27,6 +27,24 @@ class _OtpInputState extends State { digit4Controller = TextEditingController(); digit5Controller = TextEditingController(); digit6Controller = TextEditingController(); + + final controllers = [ + digit1Controller, + digit2Controller, + digit3Controller, + digit4Controller, + digit5Controller, + digit6Controller, + ]; + + digit1Controller.addListener(() { + final text = digit1Controller.text; + if (text.length > 1) { + for (int i = 0; i < text.length; i++) { + controllers[i].text = text[i]; + } + } + }); } @override @@ -54,6 +72,7 @@ class _OtpInputState extends State { } _checkCodeStatus(); }, + isFirstField: true, ), OtpInputField( controller: digit2Controller, diff --git a/recipients_app/lib/view/widgets/welcome/otp_input_field.dart b/recipients_app/lib/view/widgets/welcome/otp_input_field.dart index 372cf3d6c..95433649c 100644 --- a/recipients_app/lib/view/widgets/welcome/otp_input_field.dart +++ b/recipients_app/lib/view/widgets/welcome/otp_input_field.dart @@ -4,11 +4,13 @@ import "package:flutter/material.dart"; class OtpInputField extends StatelessWidget { final TextEditingController? controller; final Function(String?)? onChanged; + final bool isFirstField; const OtpInputField({ super.key, this.controller, this.onChanged, + this.isFirstField = false, }); @override @@ -43,7 +45,7 @@ class OtpInputField extends StatelessWidget { validator: null, textAlign: TextAlign.center, keyboardType: TextInputType.number, - maxLength: 1, + maxLength: isFirstField ? 6 : 1, ), ), ),