Flutter键盘完成按钮导致文本字段内容消失

时间:2019-05-05 18:35:33

标签: flutter controller keyboard textfield

我有一个带有一行图标的文本字段,我想将值保存在文本字段中,告诉我按一下图标以确认该值不在键盘上的完成按钮上。

在这里,单击完成关闭键盘后,文本字段中的文本消失了。

点击该图标会产生空值。

**试图通过消除控制器的行为来解决此问题,但由于单击不再存在控制器,文本字段中的值仍保留在那里,因此无法正常工作。

class ForumCard extends StatelessWidget {
  final Forums choosentype;
  final ForumServices forumServices;
  final Firestore firestore = Firestore();
  final TextEditingController _textF = TextEditingController();

  ForumCard({Key key, @required this.choosentype, this.forumServices})
      : super(key: key);


  @override
  Widget build(BuildContext context) {
    final AuthService authService = Provider.of<AuthService>(context);
    final user$ = authService.user.where((user) => user != null);
    final commentslist = List.from(choosentype.comments);
    return Container(
      width: MediaQuery.of(context).size.width * 0.9,
      child: Card(
        elevation: 100,
        color: Colors.white,
        child: Center(
          child: StreamBuilder<FirebaseUser>(
              stream: user$,
              builder: (context, snapshot) {
                final currentUser = snapshot.data;
                if (!snapshot.hasData) {
                  return Text("nooooooooodata");
                }
                return Column(
                  children: <Widget>[

                    StreamBuilder<String>(
                        stream: forumServices.text,
                        builder: (context, snapshot) {
                          if (!snapshot.hasData) {
                            return Container();
                          }
                          final text = snapshot.data;
                          return choosentype.type == "question"
                              ? Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: 
                                        CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Expanded(
                                      child: TextField(
                                        decoration: InputDecoration(
                                          hintText: "Write a comment",
                                        ),
                                        onSubmitted: (String text) {
                                          forumServices.addtext(text);
// I am adding here in a stream was trying to eliminate controller but doesnot work as the value in the textfield remains there after click on the icon as there is no longer controller.
                                        },
                                        controller: _textF,
                                        onEditingComplete: null,
                                      ),
                                    ),
                                    IconButton(
                                      icon: Icon(Icons.comment),
                                      onPressed: () {
                                        commentslist.add({
                                          "0": currentUser.uid,
                                          "1": currentUser.displayName,
                                          "2": _textF.text,
                                        });
                                        print(commentslist);
                                        Firestore.instance
                                            .collection('forums')
                                            .document(choosentype.uid)
                                            .updateData(
                                          {"comments": commentslist},
                                        );
                                      },
                                    ),
                                  ],
                                )

1 个答案:

答案 0 :(得分:0)

将小部件转换为StatefulWidget,以便能够保留TextFieldController的状态。

相关问题