gpt4 book ai didi

dart - flutter 步进器小部件 - 在各个步骤中验证字段

转载 作者:IT王子 更新时间:2023-10-29 06:55:05 25 4
gpt4 key购买 nike

我正在使用步进小部件来从用户那里收集信息并对其进行验证,我需要在每个步骤中调用一个 API,因此在每个继续按钮上验证一个步骤中的每个字段...我正在使用表单状态和表单小部件但问题是它会验证步进器中所有步骤中的整个字段...我如何才能仅验证步进器中的单个步骤?我浏览了 stepper.dart 中 Stepper 和 State 类的文档,但那里没有支持功能

代码如下

class SubmitPayment extends StatefulWidget {


SubmitPayment({Key key, this.identifier, this.amount, this.onResendPressed})
: super(key: key);

final String identifier;
final String amount;
final VoidCallback onResendPressed;

@override
State<StatefulWidget> createState() {
return _SubmitPaymentState();
}
}

class _SubmitPaymentState extends State<SubmitPayment> {
final GlobalKey<FormState> _formKeyOtp = GlobalKey<FormState>();
final FocusNode _otpFocusNode = FocusNode();
final TextEditingController _otpController = TextEditingController();
bool _isOTPRequired = false;

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Form(
key: _formKeyOtp,
child: Column(children: <Widget>[
Center(
child: Padding(
padding:
EdgeInsets.symmetric(horizontal: 16.0, vertical: 5.0),
child: Text(
Translations.of(context).helpLabelOTP,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontStyle: FontStyle.italic),
))),
CustomTextField(
icon: Icons.vpn_key,
focusNode: _otpFocusNode,
hintText: Translations.of(context).otp,
labelText: Translations.of(context).otp,
controller: _otpController,
keyboardType: TextInputType.number,
hasError: _isOTPRequired,
validator: (String t) => _validateOTP(t),
maxLength: AppConstants.otpLength,
obscureText: true,
),
Center(
child: ButtonBar(
mainAxisSize: MainAxisSize.max,
alignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text(Translations.of(context).resendOtpButton),
color: Colors.white,
textColor: Theme.of(context).primaryColor,
onPressed: widget.onResendPressed,
),
RaisedButton(
child: Text(
Translations.of(context).payButton,
),
onPressed: _doPullPayment,
),
],
)),
])),
);
}

String _validateOTP(String value) {
if (value.isEmpty || value.length < AppConstants.otpLength) {
setState(() => _isOTPRequired = true);
return Translations.of(context).invalidOtp;
}
return "";
}

bool _validateOtpForm() {
_formKeyOtp.currentState.save();
return this._formKeyOtp.currentState.validate();
}

Future<void> _doPullPayment() async {
setState(() {
_isOTPRequired = false;
});

if (!_validateOtpForm()) return false;

try {
setState(() {
_isOTPRequired = false;
});
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => AlertDialog(
content: ListTile(
leading: CircularProgressIndicator(),
title: Text(Translations.of(context).processingPaymentDialog),
),
),
);

TransactionApi api =
TransactionApi(httpDataSource, authenticator.sessionToken);
String responseMessage = await api.doPullPayment(
widget.identifier,
widget.amount,
_otpController.text,
TransactionConstants.transactionCurrency);

Navigator.of(context).pop();
await showAlertDialog(
context, Translations.of(context).pullPayment, '$responseMessage');
Navigator.pop(context);
} catch (exception) {
await showAlertDialog(context, Translations.of(context).pullPayment,
'${exception.message}');
Navigator.of(context).pop();
}
}

最佳答案

一种方法是使用单独的 Form对于每一步。要处理这个问题,请使用 GlobalKey<FormState> 的列表您可以根据 _currentStep 编制索引, 然后调用 validate()onStepContinue :

List<GlobalKey<FormState>> _formKeys = [GlobalKey<FormState>(), GlobalKey<FormState>(), …];


Stepper(
currentStep: _currentStep,
onStepContinue: () {
setState(() {
if (_formKeys[_currentStep].currentState?.validate()) {
_currentStep++;
}
});
},
steps:
Step(
child: Form(key: _formKeys[0], child: …),

这意味着以下内容:

  1. 由于您是在最后调用 API,因此您需要检查您是否在验证最后一步,并且 save而不仅仅是验证;
  2. 您可能想考虑我们的 Form s 到几个小部件。如果这样做,不要混淆 key每个 Widget 的参数有。通过 formKey作为未命名的参数以避免混淆。

关于dart - flutter 步进器小部件 - 在各个步骤中验证字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51231128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com