英文:
How to clear first text controller flutter
问题
以下是翻译好的部分:
-
我想将此屏幕下方的文本更改为黑色。
-
其次,当我输入数字时,下方的文本显示“输入的PIN不匹配。”,而不是在我完成输入不匹配的PIN后显示。
-
用户设置他们的卡的PIN码,然后在同一屏幕上,我使用相同的控制器来确认他们的PIN码。问题在于设置PIN码的PIN控制器不清晰。我在同一屏幕上使用相同的屏幕,不将用户导航到新屏幕上。请看一下并解决这个问题。
-
这是我的代码
class _SetPinTestscreenState extends State<SetPinTestscreen> {
TextEditingController setPinController = TextEditingController();
PinValidationProvider pinValidationProvider;
@override
void initState() {
Provider.of<PinValidationProvider>(context, listen: false).isPinSet = false;
super.initState();
}
@override
Widget build(BuildContext context) {
return MainTemplate(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Align(
alignment: Alignment.centerLeft,
child: Consumer<PinValidationProvider>(
builder: (context, pinProvider, child) {
return Text(
pinProvider.isPinSet
? '重新输入您的PIN码以确认'
: '设置PIN码以激活您的Finance House卡,以XXXX结尾',
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 30),
);
},
),
),
SizedBox(height: 20),
Center(
child: PinFieldAutoFill(
decoration: UnderlineDecoration(
textStyle: Theme.of(context).textTheme.subtitle1.copyWith(
fontWeight: FontWeight.bold,
fontSize: 15,
),
gapSpace: 8,
lineHeight: 1.2,
colorBuilder:
PinListenColorBuilder(Colors.black, Colors.grey.shade400),
),
onCodeChanged: (value) {
Provider.of<PinValidationProvider>(context, listen: false)
.validatePin(value, setPinController);
},
codeLength: 4,
cursor: Cursor(color: Colors.black, enabled: true),
controller: setPinController,
keyboardType: TextInputType.number,
),
),
SizedBox(height: 20),
Consumer<PinValidationProvider>(
builder: (context, provider, child) {
final textColor =
provider.isInvalidPin || !provider.isPinMatched
? Colors.red
: Colors.black;
if (!provider.isPinMatched &&
provider.isPinSet &&
setPinController.text.isNotEmpty) {
return Text(
'输入的PIN码不匹配。',
style: TextStyle(
color: Colors.red,
),
);
} else {
return Text(
'PIN码必须至少为4位数字。不可为单个数字,如1111或数字顺序,如1234。',
style: TextStyle(
color: textColor,
),
);
}
},
),
],
),
),
);
}
}
- 还有我的提供者类
class PinValidationProvider extends ChangeNotifier {
bool _isInvalidPin = false;
bool get isInvalidPin => _isInvalidPin;
bool isPinSet = false;
String pin = '';
String confirmedPin = '';
void validatePin(String pin) {
if (pin.length != 4) {
_isInvalidPin = false;
pin = pin ?? '';
} else {
confirmedPin = pin ?? '';
if (_isSimpleSequence(pin) || _isRepeatedDigit(pin)) {
_isInvalidPin = true;
} else {
_isInvalidPin = false;
if (!isPinSet) {
isPinSet = true;
}
}
}
Future.microtask(() {
notifyListeners();
});
}
bool _isSimpleSequence(String pin) {
for (int i = 0; i < pin.length - 1; i++) {
int digit1 = int.parse(pin[i]);
int digit2 = int.parse(pin[i + 1]);
if (digit2 - digit1 == 1) {
return true;
}
}
return false;
}
bool _isRepeatedDigit(String pin) {
for (int i = 0; i < pin.length - 1; i++) {
if (pin[i] == pin[i + 1]) {
return true;
}
}
return false;
}
}
希望这些翻译对您有所帮助。
英文:
UPDATED Question
I want to change this screen's below text in black
and secondly when i type digit the below text shows that Entered PIN does not match.
instead it shows after when i complete typed mis-match PIN.
-------------------------------------------------------------------
The user sets their PIN for the card, and then on the same screen, I am using the same controller to confirm their PIN. The issue is that the PIN controller for setting the PIN is not clear. I am using same screen. I am not navigating user to new screen. Kindly take a look on it and resolve it.
Here is my code
class _SetPinTestscreenState extends State<SetPinTestscreen> {
TextEditingController setPinController = TextEditingController();
PinValidationProvider pinValidationProvider;
@override
void initState() {
Provider.of<PinValidationProvider>(context, listen: false).isPinSet = false;
super.initState();
}
@override
Widget build(BuildContext context) {
return MainTemplate(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Align(
alignment: Alignment.centerLeft,
child: Consumer<PinValidationProvider>(
builder: (context, pinProvider, child) {
return Text(
pinProvider.isPinSet
? 'Re-enter your PIN code to Confirm'
: 'Set PIN to Activate your Finance House Card Ending With XXXX',
style: TextStyle(fontWeight: FontWeight.w700, fontSize: 30),
);
},
),
),
SizedBox(height: 20),
Center(
child: PinFieldAutoFill(
decoration: UnderlineDecoration(
textStyle: Theme.of(context).textTheme.subtitle1.copyWith(
fontWeight: FontWeight.bold,
fontSize: 15,
),
gapSpace: 8,
lineHeight: 1.2,
colorBuilder:
PinListenColorBuilder(Colors.black, Colors.grey.shade400),
),
onCodeChanged: (value) {
Provider.of<PinValidationProvider>(context, listen: false)
.validatePin(value, setPinController);
},
codeLength: 4,
cursor: Cursor(color: Colors.black, enabled: true),
controller: setPinController,
keyboardType: TextInputType.number,
),
),
SizedBox(height: 20),
Consumer<PinValidationProvider>(
builder: (context, provider, child) {
final textColor =
provider.isInvalidPin || !provider.isPinMatched
? Colors.red
: Colors.black;
if (!provider.isPinMatched &&
provider.isPinSet &&
setPinController.text.isNotEmpty) {
return Text(
'Entered PIN does not match.',
style: TextStyle(
color: Colors.red,
),
);
} else {
return Text(
'Pin must be at least 4 digits. It must not be single digits like'
' 1111 or digits in numerical order such as 1234',
style: TextStyle(
color: textColor,
),
);
}
},
),
],
),
),
);
}
}
Also here is my provider class
class PinValidationProvider extends ChangeNotifier {
bool _isInvalidPin = false;
bool get isInvalidPin => _isInvalidPin;
bool isPinSet = false;
String pin = '';
String confirmedPin = '';
void validatePin(String pin) {
if (pin.length != 4) {
_isInvalidPin = false;
pin = pin ?? '';
} else {
confirmedPin = pin ?? '';
if (_isSimpleSequence(pin) || _isRepeatedDigit(pin)) {
_isInvalidPin = true;
} else {
_isInvalidPin = false;
if (!isPinSet) {
isPinSet = true;
}
}
}
Future.microtask(() {
notifyListeners();
});
}
bool _isSimpleSequence(String pin) {
for (int i = 0; i < pin.length - 1; i++) {
int digit1 = int.parse(pin[i]);
int digit2 = int.parse(pin[i + 1]);
if (digit2 - digit1 == 1) {
return true;
}
}
return false;
}
bool _isRepeatedDigit(String pin) {
for (int i = 0; i < pin.length - 1; i++) {
if (pin[i] == pin[i + 1]) {
return true;
}
}
return false;
}
}
答案1
得分: 1
你可以调用setPinController.clear()
来清除它的文本,在这种情况下,你可以将setPinController
作为validatePin
方法的参数添加,然后在方法内部调用clear()
。
英文:
you can call setPinController.clear()
to clear it's text, in this case, you can add setPinController
as a parameter in validatePin
method, then call clear()
inside of the method.
void validatePin(String pin, TextEditingController controller) {
if (pin.length != 4) {
_isInvalidPin = false;
} else {
if (_isSimpleSequence(pin) || _isRepeatedDigit(pin)) {
_isInvalidPin = true;
} else {
_isInvalidPin = false;
if (!isPinSet) {
isPinSet = true;
this.pin = pin;
} else {
confirmedPin = pin;
}
}
if (confirmedPin != this.pin) {
controller.clear();
}
}
Future.microtask(() {
notifyListeners();
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论