【问题标题】:Flutter keyboard done button causes textfield content to dissapper颤动键盘完成按钮导致文本字段内容消失
【发布时间】:2019-05-05 18:35:33
【问题描述】:

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

点击完成关闭键盘后,文本字段中的文本消失了。

点击图标时的结果为空值。

**尝试使用消除控制器的行为来解决此问题,但由于不再存在控制器,因此单击图标后文本字段中的值仍然存在,因此不起作用。

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},
                                        );
                                      },
                                    ),
                                  ],
                                )

【问题讨论】:

    标签: flutter controller keyboard textfield


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2021-05-07
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多