我有一个TextFormField窗口小部件,该窗口小部件嵌入在页面上,旨在接受用户的评论。用户键入评论后,我将对其进行验证并将其发布到Firebase。当我启用了硬件键盘,然后单击并键入时,一切正常。
但是,当我禁用硬件键盘并想要抬起手机的键盘时,它开始抬起,然后立即折叠起来,重新加载页面。
如何防止这种情况发生?
class CommentCollector extends StatefulWidget {
final String articleID;
final String articleDate;
final String articleTitle;
final String userName;
final String firstName;
final String lastName;
final String user_id;
const CommentCollector(
{Key key,
this.articleID,
this.articleTitle,
this.userName,
this.firstName,
this.lastName,
this.user_id,
this.articleDate})
: super(key: key);
@override
_CommentCollectorState createState() => new _CommentCollectorState();
}
class _PostData {
String comment = '';
}
class _CommentCollectorState extends State<CommentCollector> {
final _formKey = GlobalKey<FormState>();
bool _commentPosted = false;
_PostData _data = new _PostData();
void submit() {
// First validate form.
if (this._formKey.currentState.validate()) {
_formKey.currentState.save(); // Save our form now.
addPost({
'article': widget.articleID,
'article_title': widget.articleTitle,
'author': widget.userName,
'comment': _data.comment,
'firstName': widget.firstName,
'lastName': widget.lastName,
'user_id': widget.user_id,
'respect_count': 0,
});
setState(() {
_commentPosted = true;
});
}
}
@override
Widget build(BuildContext context) {
print(_formKey);
switch (_commentPosted) {
case (true):
return new UserPostGetter(
articleHeader: widget.articleTitle,
articleId: widget.articleID,
);
case (false):
return new Container(
child: new Center(
child: new Container(
padding: EdgeInsets.all(8.0),
child: new Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
elevation: 5.0,
child: new Form(
key: this._formKey,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Padding(
padding: EdgeInsets.all(10.0),
child: new Text(
"What's your take on the issue?",
style: new TextStyle(fontSize: 18.0),
),
),
new Padding(
padding: EdgeInsets.all(20.0),
child: new Card(
elevation: 5.0,
child: new TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
if (value.length > 250) {
return 'Please shorten comment';
}
},
decoration: new InputDecoration(
contentPadding: EdgeInsets.all(10.0),
border: InputBorder.none,
hintText: 'Your thoughts?',
),
keyboardType: TextInputType.multiline,
maxLines: 6,
maxLength: 250,
onSaved: (String value) {
this._data.comment = value;
}),
),
),
new RaisedButton(
onPressed: this.submit,
child: new Text(
"SUBMIT",
style: new TextStyle(color: Colors.white),
),
color: purpleColor(),
)
],
),
),
),
),
),
);
}
}
}
//add post to Firebase
Future<void> addPost(postData) async {
Firestore.instance.collection('post').add(postData).catchError((e) {
print(e);
});
Firestore.instance.collection('respect_count').add({
'count': 0,
}).catchError((e) {
print(e);
});
}