Skip to content

Commit

Permalink
Adding mechanism to fix copy paste behaviour for otp input
Browse files Browse the repository at this point in the history
  • Loading branch information
MDikkii committed Oct 21, 2023
1 parent 6448ff9 commit 575d2e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 19 additions & 0 deletions recipients_app/lib/view/widgets/welcome/otp_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ class _OtpInputState extends State<OtpInput> {
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
Expand Down Expand Up @@ -54,6 +72,7 @@ class _OtpInputState extends State<OtpInput> {
}
_checkCodeStatus();
},
isFirstField: true,
),
OtpInputField(
controller: digit2Controller,
Expand Down
4 changes: 3 additions & 1 deletion recipients_app/lib/view/widgets/welcome/otp_input_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +45,7 @@ class OtpInputField extends StatelessWidget {
validator: null,
textAlign: TextAlign.center,
keyboardType: TextInputType.number,
maxLength: 1,
maxLength: isFirstField ? 6 : 1,
),
),
),
Expand Down

0 comments on commit 575d2e7

Please sign in to comment.