如何解决Flutter 中引用 Firebase 的 DropdownButton 不会在选择后覆盖初始值
我使用 TextFormFields 和 DropdownButtons 构建了一个 Flutter 表单来编辑用户配置文件。初始值是使用 FutureBuilder 从 Firebase 调用的。下拉列表中的列表也来自 Firebase。
问题1:在DropdownButtons中,选择后初始值不改变; Firebase 中的值保留在表单中。但是当您点击“保存”按钮时,它会使用选择正确更新 Firebase。如何覆盖初始值?我已经尝试了很多方法,但我认为这是国家问题。
问题 2: 如何向 DropdownButton 添加验证器以确保它不是空的?我无法让它工作。
非常感谢!
class EditProfilePage extends StatefulWidget {
const EditProfilePage({Key key,@required this.database,this.profile})
: super(key: key);
final FirestoreDatabase database;
final Profile profile;
static Future<void> show(
BuildContext context,{
FirestoreDatabase database,Profile profile,}) async {
await Navigator.of(context,rootNavigator: true).push(
MaterialPageRoute(
builder: (context) =>
EditProfilePage(database: database,profile: profile),fullscreenDialog: true,),);
}
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State<EditProfilePage> {
final _formKey = GlobalKey<FormState>();
String userId;
String name;
String gender;
String relationshipStatus;
var _selectedGender;
var _selectedRelationshipStatus;
bool _validateAndSaveForm() {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
return true;
}
return false;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 2.0,title: Text('Edit Profile'),body: _buildContents(),backgroundColor: Colors.grey[200],);
}
Widget _buildContents() {
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),child: _buildForm(),);
}
Widget _buildForm() {
return Form(
key: _formKey,child: _buildFormChildren(),);
}
Widget _buildFormChildren() {
final auth = Provider.of<AuthBase>(context,listen: false);
return FutureBuilder<DocumentSnapshot>(
future: FirebaseFirestore.instance
.collection('users')
.doc(auth.currentUser.uid)
.get(),builder:
(BuildContext context,AsyncSnapshot<DocumentSnapshot> snapshot) {
List<Widget> children;
if (snapshot.hasData) {
_selectedGender = snapshot.data['gender'];
_selectedRelationshipStatus = snapshot.data['relationshipStatus'];
children = <Widget>[
TextFormField(
decoration: Inputdecoration(labelText: 'Name'),initialValue: snapshot.data['name'],validator: (value) =>
value.isNotEmpty ? null : 'Name can\'t be empty',onSaved: (value) => name = value,FormField(
initialValue: _selectedGender,builder: (FormFieldState state) {
return InputDecorator(
decoration: Inputdecoration(
labelText: 'Gender',child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('references')
.doc('profile_references')
.collection('gender')
.snapshots(),builder: (context,snapshot) {
if (!snapshot.hasData)
return const Center(
child: const CircularProgressIndicator(),);
var length = snapshot.data.docs.length;
DocumentSnapshot ds = snapshot.data.docs[length - 1];
return DropdownButton(
isDense: true,value: _selectedGender,hint: Text('Change to'),items: snapshot.data.docs
.map((DocumentSnapshot document) {
return DropdownMenuItem(
value: document.data()['gender'],child: Text(document.data()['gender']),);
}).toList(),onChanged: (value) {
setState(() {
gender = value;
_selectedGender = value;
});
},);
}),);
}),FormField(
initialValue: _selectedRelationshipStatus,builder: (FormFieldState state) {
return InputDecorator(
decoration: Inputdecoration(
labelText: 'Relationship Status',child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('references')
.doc('profile_references')
.collection('relationshipStatus')
.snapshots(),value: _selectedRelationshipStatus,items: snapshot.data.docs
.map((DocumentSnapshot document) {
return DropdownMenuItem(
value: document.data()['relationshipStatus'],child:
Text(document.data()['relationshipStatus']),onChanged: (value) {
setState(() {
relationshipStatus = value;
_selectedRelationshipStatus = value;
});
},];
} else if (snapshot.hasError) {
children = <Widget>[
Icon(
Icons.error_outline,color: Colors.red,size: 60,Padding(
padding: const EdgeInsets.only(top: 16),child: Text('Error: ${snapshot.error}'),)
];
} else {
children = <Widget>[
SizedBox(
child: CircularProgressIndicator(),width: 60,height: 60,const Padding(
padding: EdgeInsets.only(top: 16),child: Text('Awaiting result...'),)
];
}
return Center(
child: SingleChildScrollView(
child: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: children,SizedBox(height: 20),RaisedButton(
color: Colors.green[100],child: Text(
'Save',style: TextStyle(fontSize: 18,color: Colors.black),onpressed: () async {
if (_validateAndSaveForm()) {
try {
{
final profile = Profile(
name: name,gender: gender,relationshipStatus: relationshipStatus);
await widget.database.editProfile(profile);
Navigator.of(context).pop();
}
} on FirebaseException catch (e) {
showExceptionAlertDialog(
context,title: 'Operation Failed',exception: e,);
}
}
},],);
},);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。