如何验证Flutter中的下拉菜单 [英] How validate dropdown in flutter
本文介绍了如何验证Flutter中的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是扑扑开发的新手.我正在尝试通过下拉菜单验证表单,但不能.我点击了此链接以进行下拉验证. https://github.com/flutter/flutter/flutter/issues/6422#issuecomment-262337023
I'm new to flutter development. I'm trying to validate form with drop down but could not. I followed this link for drop down validation. https://github.com/flutter/flutter/issues/6422#issuecomment-262337023
该下拉列表正在自动验证.
The drop down is being auto validated.
推荐答案
尝试在 Form
例如:
import 'package:flutter/material.dart';
class FormValidationWithDropdown extends StatefulWidget {
@override
_FormValidationWithDropdownState createState() =>
_FormValidationWithDropdownState();
}
class _FormValidationWithDropdownState
extends State<FormValidationWithDropdown> {
final _formKey = GlobalKey<FormState>();
bool _autovalidate = false;
String selectedSalutation;
String name;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
autovalidate: _autovalidate,
child: Column(
children: <Widget>[
DropdownButtonFormField<String>(
value: selectedSalutation,
hint: Text(
'Salutation',
),
onChanged: (salutation) =>
setState(() => selectedSalutation = salutation),
validator: (value) => value == null ? 'field required' : null,
items:
['MR.', 'MS.'].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
TextFormField(
decoration: InputDecoration(hintText: 'Name'),
validator: (value) => value.isEmpty ? 'Name is required' : null,
onSaved: (value) => name = value,
),
FlatButton(
child: Text('PROCEED'),
color: Colors.green,
onPressed: () {
if (_formKey.currentState.validate()) {
//form is valid, proceed further
_formKey.currentState.save();//save once fields are valid, onSaved method invoked for every form fields
} else {
setState(() {
_autovalidate = true; //enable realtime validation
});
}
},
)
],
),
),
);
}
}
这篇关于如何验证Flutter中的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文