【发布时间】:2020-05-22 22:15:10
【问题描述】:
当我运行我的程序来提交此表单时,验证器会运行并返回正确的错误检查,但是我无法想出一种方法来让 TextFormFields 的 onSaved 方法工作。但是,在运行代码时我没有收到任何错误,并且下拉按钮可以正常工作并正确上传到 Firebase。我目前已将名称设置为打印到 logcat 以进行调试,但它永远不会被调用。
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:connectuproject/authentication.dart';
class player_reg extends StatefulWidget {
player_reg(this.auth);
final BaseAuth auth;
@override
State<StatefulWidget> createState() => new PlayerRegState();
}
class PlayerRegState extends State<player_reg> {
final _formKey = new GlobalKey<FormState>();
String _name = "";
String _age = "";
String _grad = "";
String _highschool = "";
String _city = "";
String _state = "";
String _heightFeet = "";
String _heightInch = "";
String _weight = "";
String _hand = "Right";
String _number = "";
String _email = "";
String _errorMessage = "";
String _position = "Attack";
String _club = "";
bool _isLoading = false;
bool validateAndSave() {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
return true;
}
return false;
}
void validateAndSubmit() async {
setState(() {
_errorMessage = "";
_isLoading = true;
});
print("Auth: " + widget.auth.getUserId());
if (validateAndSave()) {
try {
String _uid = widget.auth.getUserId();
print("_uid: " + _uid + " Name: " + _name);
final databaseReference = Firestore.instance;
await databaseReference.collection("users").document(_uid).setData({
'name': _name,
'age': _age,
'grade': _grad,
'highschool': _highschool,
'state': _state,
'city': _city,
'heightFeet': _heightFeet,
'heightInch': _heightInch,
'weight': _weight,
'hand': _hand,
'position': _position,
'club': _club,
});
}
catch(e){
print(e.message);
}
}
setState(() {
_isLoading = false;
});
}
@override
void initState() {
_errorMessage = "";
_isLoading = false;
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Flutter login demo'),
),
body: Stack(
children: <Widget>[
_showForm(),
_showCircularProgress(),
],
));
}
Widget _showCircularProgress() {
if (_isLoading) {
return Center(child: CircularProgressIndicator());
}
return Container(
height: 0.0,
width: 0.0,
);
}
Widget _showForm() {
return new Container(
padding: EdgeInsets.all(16.0),
child: new Form(
key: _formKey,
child: new ListView(
shrinkWrap: true,
children: <Widget>[
showInstruct(),
showPerson(),
showNameInput(),
showAgeInput(),
showGradInput(),
showHighschoolInput(),
showStateInput(),
showCityInput(),
showPhysical(),
showHeightFeetInput(),
showHeightInchesInput(),
showWeightInput(),
showHandiness(),
showHandinessInput(),
showContact(),
showNumberInput(),
showEmailInput(),
showLacrosse(),
showPosition(),
showPositionInput(),
showClubInput(),
showSubmitButton(),
],
),
),
);
}
Widget showInstruct() {
return new Text(
"Please complete the following ",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 30.0,
fontWeight: FontWeight.bold,
),
);
}
Widget showPerson() {
return new Padding(
padding: const EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0),
child: new Text(
"Personal info: ",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 30.0,
),
));
}
Widget showNameInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
decoration: new InputDecoration(
hintText: 'Name',
),
validator: (value) => value.isEmpty ? 'Name can\'t be empty' : null,
onSaved: (String value) => print("saving the name value of " + value),
),
);
}
Widget showAgeInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
maxLength: 2,
autofocus: false,
keyboardType: TextInputType.number,
decoration: new InputDecoration(
hintText: 'Age',
),
validator: (value) => value.isEmpty ? 'Age can\'t be empty' : null,
onSaved: (String value) => _age = value.trim(),
),
);
}
Widget showGradInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
maxLength: 4,
autofocus: false,
keyboardType: TextInputType.number,
decoration: new InputDecoration(
hintText: 'Graduation year',
),
validator: (value) =>
value.isEmpty ? 'Graduation year can\'t be empty' : null,
onSaved: (String value) => _grad = value.trim(),
),
);
}
Widget showHighschoolInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
decoration: new InputDecoration(
hintText: 'Highschool',
),
validator: (value) =>
value.isEmpty ? 'Highschool can\'t be empty' : null,
onSaved: (String value) => _highschool = value.trim(),
),
);
}
Widget showStateInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
decoration: new InputDecoration(
hintText: 'State',
),
validator: (value) => value.isEmpty ? 'State can\'t be empty' : null,
onSaved: (String value) => _state = value.trim(),
),
);
}
Widget showCityInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 50.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
decoration: new InputDecoration(
hintText: 'City',
),
validator: (value) => value.isEmpty ? 'State can\'t be empty' : null,
onSaved: (String value) => _city = value.trim(),
),
);
}
Widget showPhysical() {
return new Text(
"Physical info: ",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 30.0,
),
);
}
Widget showHeightFeetInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
maxLength: 1,
autofocus: false,
keyboardType: TextInputType.number,
decoration: new InputDecoration(
hintText: 'Height(feet)',
),
validator: (value) => value.isEmpty ? 'Height can\'t be empty' : null,
onSaved: (String value) => _heightFeet = value.trim(),
),
);
}
Widget showHeightInchesInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
maxLength: 2,
autofocus: false,
keyboardType: TextInputType.number,
decoration: new InputDecoration(
hintText: 'Height(inches)',
),
validator: (value) => value.isEmpty ? 'Height can\'t be empty' : null,
onSaved: (String value) => _heightInch = value.trim(),
),
);
}
Widget showWeightInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
maxLength: 3,
autofocus: false,
keyboardType: TextInputType.number,
decoration: new InputDecoration(
hintText: 'weight(lbs)',
),
validator: (value) => value.isEmpty ? 'Weight can\'t be empty' : null,
onSaved: (String value) => _weight = value.trim(),
),
);
}
Widget showHandiness() {
return new Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: Text(
"Handiness:",
style: TextStyle(fontSize: 20),
),
);
}
Widget showHandinessInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
child: Container(
width: double.infinity,
child: new DropdownButton<String>(
isExpanded: true,
value: _hand,
elevation: 16,
underline: Container(
height: 2,
),
onChanged: (String newValue) {
setState(() {
_hand = newValue;
});
},
items: <String>['Right','Left']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)),
);
}
Widget showContact() {
return new Text(
"Contact info: ",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 30.0,
),
);
}
Widget showNumberInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
keyboardType: TextInputType.phone,
decoration: new InputDecoration(
hintText: 'Phone Number',
),
validator: (value) => value.isEmpty ? 'Number can\'t be empty' : null,
onSaved: (String value) => _number = value.trim(),
),
);
}
Widget showEmailInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 50.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
keyboardType: TextInputType.emailAddress,
decoration: new InputDecoration(
hintText: 'Email',
),
validator: (value) => value.isEmpty ? 'Not a valid Email' : null,
onSaved: (String value) => _email = value.trim(),
),
);
}
Widget showLacrosse() {
return new Text(
"Lacrosse info: ",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontFamily: 'Raleway',
fontSize: 30.0,
),
);
}
Widget showPosition() {
return new Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 0.0),
child: Text(
"Position:",
style: TextStyle(fontSize: 20),
));
}
Widget showPositionInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
child: Container(
width: double.infinity,
child: new DropdownButton<String>(
isExpanded: true,
value: _position,
elevation: 16,
underline: Container(
height: 2,
),
onChanged: (String newValue) {
setState(() {
_position = newValue;
});
},
items: <String>[
'Attack',
'Defense',
'Face-off',
'Goalie',
'Long Stick Midfield',
'Midfield'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)),
);
}
Widget showClubInput() {
return Padding(
padding: const EdgeInsets.fromLTRB(0.0, 25.0, 0.0, 50.0),
child: new TextFormField(
maxLines: 1,
autofocus: false,
decoration: new InputDecoration(
hintText: 'Club Team',
),
onSaved: (String value) => _club = value.trim(),
),
);
}
Widget showSubmitButton() {
return new Padding(
padding: EdgeInsets.fromLTRB(0.0, 45.0, 0.0, 25.0),
child: SizedBox(
height: 40.0,
child: new RaisedButton(
elevation: 5.0,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: Colors.blue,
child: new Text("Submit"),
onPressed: validateAndSubmit,
),
));
}
}
在此处输入代码
【问题讨论】:
标签: android forms firebase flutter dart