Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

116 righe
3.9 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:qadirneyriz/config/config.dart';
  4. class OTPTextField extends StatefulWidget {
  5. final int length;
  6. final ValueChanged<String> onSubmitted;
  7. const OTPTextField({
  8. super.key,
  9. required this.length,
  10. required this.onSubmitted,
  11. });
  12. @override
  13. _OTPTextFieldState createState() => _OTPTextFieldState();
  14. }
  15. class _OTPTextFieldState extends State<OTPTextField> {
  16. late List<FocusNode> focusNodes;
  17. late List<TextEditingController> controllers;
  18. @override
  19. void initState() {
  20. super.initState();
  21. focusNodes = List.generate(widget.length, (index) => FocusNode());
  22. controllers =
  23. List.generate(widget.length, (index) => TextEditingController());
  24. }
  25. @override
  26. void dispose() {
  27. for (var controller in controllers) {
  28. controller.dispose();
  29. }
  30. super.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Directionality(
  35. textDirection: TextDirection.ltr,
  36. child: Row(
  37. children: List.generate(
  38. widget.length,
  39. (index) => Expanded(
  40. child: Padding(
  41. padding: const EdgeInsets.symmetric(horizontal: 7),
  42. child: Center(
  43. child: TextFormField(
  44. cursorColor: config.ui.mainGreen,
  45. style: const TextStyle(
  46. color: Colors.black,
  47. fontWeight: FontWeight.bold,
  48. fontSize: 20),
  49. controller: controllers[index],
  50. focusNode: focusNodes[index],
  51. keyboardType: TextInputType.number,
  52. textInputAction: index < widget.length - 1
  53. ? TextInputAction.next
  54. : TextInputAction.done,
  55. inputFormatters: [
  56. FilteringTextInputFormatter.allow(RegExp('[0-9]'))
  57. ],
  58. textAlign:
  59. TextAlign.center, // Center the text within the box.
  60. onChanged: (value) {
  61. if (value.length == 1) {
  62. if (index < widget.length - 1) {
  63. focusNodes[index].unfocus();
  64. FocusScope.of(context)
  65. .requestFocus(focusNodes[index + 1]);
  66. } else {
  67. focusNodes[index].unfocus();
  68. }
  69. } else if (value.isEmpty) {
  70. if (index > 0) {
  71. focusNodes[index].unfocus();
  72. FocusScope.of(context)
  73. .requestFocus(focusNodes[index - 1]);
  74. }
  75. }
  76. if (isInputValid()) {
  77. String otp = controllers
  78. .map((controller) => controller.text)
  79. .join();
  80. widget.onSubmitted(otp);
  81. }
  82. },
  83. decoration: InputDecoration(
  84. fillColor: Colors.white.withOpacity(.5),
  85. filled: true,
  86. border: OutlineInputBorder(
  87. borderRadius:
  88. BorderRadius.circular(12), // Add border radius
  89. ),
  90. focusedBorder: OutlineInputBorder(
  91. borderRadius: BorderRadius.circular(12),
  92. borderSide: BorderSide(
  93. color: config
  94. .ui.mainGreen, // Change focus color to yellow
  95. ),
  96. ))),
  97. ),
  98. ),
  99. ),
  100. ),
  101. ),
  102. );
  103. }
  104. bool isInputValid() {
  105. return controllers.every((controller) => controller.text.length == 1);
  106. }
  107. }