Skip to content

Commit a8dd384

Browse files
committed
Fix delete in textField
1 parent 8a1c267 commit a8dd384

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed
Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'dart:async';
23

34
class VerificationCodeInput extends StatefulWidget {
45
VerificationCodeInput({this.onCompleted, this.keyboardType, this.length = 4});
@@ -7,14 +8,14 @@ class VerificationCodeInput extends StatefulWidget {
78
final int length;
89

910
@override
10-
verificationCodeInputState createState() => new verificationCodeInputState(
11+
_verificationCodeInputState createState() => new _verificationCodeInputState(
1112
onCompleted: onCompleted,
1213
keyboardType: this.keyboardType,
1314
length: length);
1415
}
1516

16-
class verificationCodeInputState extends State<VerificationCodeInput> {
17-
verificationCodeInputState(
17+
class _verificationCodeInputState extends State<VerificationCodeInput> {
18+
_verificationCodeInputState(
1819
{this.onCompleted,
1920
this.keyboardType = TextInputType.number,
2021
this.length = 4});
@@ -27,52 +28,59 @@ class verificationCodeInputState extends State<VerificationCodeInput> {
2728
final List<TextEditingController> _listControllerText =
2829
<TextEditingController>[];
2930

30-
String getInputVerify() {
31+
String _getInputVerify() {
3132
String verifycode = '';
3233
for (var i = 0; i < length; i++) {
33-
verifycode += _listControllerText[i].text;
34+
for(var index = 0; index < _listControllerText[i].text.length; index ++){
35+
if(_listControllerText[i].text[index] != ' ') {
36+
verifycode += _listControllerText[i].text[index];
37+
}
38+
}
3439
}
35-
print(verifycode);
3640
return verifycode;
3741
}
3842

39-
Widget createItem(int index) {
43+
Widget _createItem(int index) {
4044
Flexible flexible = Flexible(
41-
child: new TextField(
42-
keyboardType: keyboardType,
43-
enabled: _listControllerText[index].text.length == 1,
44-
maxLines: 1,
45-
maxLength: 1,
46-
focusNode: _listFocusNode[index],
47-
decoration: InputDecoration(
48-
enabled: false,
45+
child: new TextField(
46+
keyboardType: keyboardType,
47+
enabled: false,
48+
maxLines: 1,
49+
maxLength: 2,
50+
focusNode: _listFocusNode[index],
51+
decoration: InputDecoration(
52+
enabled: true,
4953
counterText: "",
5054
contentPadding: new EdgeInsets.all(10.0),
5155
errorMaxLines: 1,
5256
fillColor: Colors.black),
53-
onChanged: (String value) {
54-
if (value.length > 0 && index < length) {
55-
if (index == length - 1) {
56-
widget.onCompleted(getInputVerify());
57+
onChanged: (String value) {
58+
if (value.length > 1 && index < length || index == 0 && value.isNotEmpty) {
59+
if (index == length - 1) {
60+
widget.onCompleted(_getInputVerify());
61+
return;
62+
}
63+
_listControllerText[index + 1].value = new TextEditingValue(text: " ");
64+
FocusScope.of(context).requestFocus(_listFocusNode[index + 1]);
5765
return;
5866
}
59-
FocusScope.of(context).requestFocus(_listFocusNode[index + 1]);
60-
}
61-
if (value.isEmpty && index >= 0) {
62-
FocusScope.of(context).requestFocus(_listFocusNode[index - 1]);
63-
}
64-
},
65-
controller: _listControllerText[index],
66-
maxLengthEnforced: true,
67-
autocorrect: false,
68-
textAlign: TextAlign.center,
69-
autofocus: true,
70-
style: new TextStyle(fontSize: 25.0, color: Colors.black),
71-
));
67+
if (value.isEmpty && index >= 0) {
68+
_listControllerText[index - 1].value = new TextEditingValue(text: " ");
69+
FocusScope.of(context).requestFocus(_listFocusNode[index - 1]);
70+
}
71+
},
72+
controller: _listControllerText[index],
73+
maxLengthEnforced: true,
74+
autocorrect: false,
75+
textAlign: TextAlign.center,
76+
autofocus: true,
77+
style: new TextStyle(fontSize: 25.0, color: Colors.black),
78+
),
79+
);
7280
return flexible;
7381
}
7482

75-
List<Widget> createListItem() {
83+
List<Widget> _createListItem() {
7684
List<Widget> listWidget = <Widget>[];
7785
int numberVerification = length;
7886
if (_listFocusNode.isEmpty) {
@@ -83,7 +91,7 @@ class verificationCodeInputState extends State<VerificationCodeInput> {
8391
}
8492
for (var i = 0, j = 0; i < numberVerification * 2; i++) {
8593
if (i % 2 == 0) {
86-
listWidget.add(createItem(j++));
94+
listWidget.add(_createItem(j++));
8795
} else {
8896
if (j < numberVerification) {
8997
listWidget.add(new Padding(
@@ -101,7 +109,7 @@ class verificationCodeInputState extends State<VerificationCodeInput> {
101109
padding: new EdgeInsets.only(left: 50.0, right: 50.0),
102110
child: new Row(
103111
mainAxisAlignment: MainAxisAlignment.spaceBetween,
104-
children: createListItem()),
112+
children: _createListItem()),
105113
);
106114
}
107115
}

0 commit comments

Comments
 (0)