showModalBottomSheet触发Futurebuilder

时间:2019-10-08 13:01:07

标签: flutter dart flutter-layout flutter-test

我真的很沮丧。

我有3个函数,每个函数都调用showModalBottomSheet

2个有状态和1个无状态的模态。

我在使用其他有状态模式时遇到问题。

它们都使用InkWell's onTap函数来调用。并且两者都没有占据整个屏幕,因此显示了主页。

我不触发未来/不刷新页面的状态模态:

void showCommentsModal(int id) {
    showModalBottomSheet(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20.0),
      ),
      backgroundColor: Color(colorBackground),
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return Container(
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
              color: Color(colorBackground),
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(20), topRight: Radius.circular(20))),
          height: MediaQuery.of(context).size.height / 1.5,
          width: MediaQuery.of(context).size.width,
          child: ViewCommentModal(
            postID: id,
            userList: userList,
          ),
        );
      },
    );
  }

触发状态/刷新列表的其他状态模态:

 addCommentModal(int i) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return Container(
          padding:
              EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
          child: AddCommentModal(
            onPost: (String text) {
              APIServices.commentPost(context, i.toString(), text);
              Navigator.pop(context);
            },
          ),
        );
      },
    );
  }

什么使addCommentModal触发了未来?请帮忙。

编辑我抓到了犯罪嫌疑人! 这是我的AddCommentModal代码:

class AddCommentModal extends StatefulWidget {
  final ValueChanged<String> onPost;

  AddCommentModal({@required this.onPost});

  @override
  _AddCommentModalState createState() => _AddCommentModalState();
}

class _AddCommentModalState extends State<AddCommentModal> {
  final commentController = TextEditingController();
  String defaultProfilePhoto = "";
  @override
  void initState() {
    defaultProfilePhoto = Constants.userFirstName[0].toUpperCase();
    super.initState();
  }

  @override
  void dispose() {
    commentController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.all(10),
        child: Row(
          children: <Widget>[
            Container(
              width: 50,
              height: 50,
              child: ClipRRect(
                  borderRadius: new BorderRadius.circular(50),
                  child: Constants.userProfilePhoto == null
                      ? Container(
                          color: Color(colorPrimary),
                          alignment: Alignment.center,
                          child: Text(
                            defaultProfilePhoto,
                            style: TextStyle(
                                color: Color(colorText), fontSize: 20),
                          ),
                        )
                      : Image.network(
                          APIServices.httpDomain + Constants.userProfilePhoto,
                          fit: BoxFit.cover,
                        )),
            ),
            Expanded(
                child: Container(
              margin: EdgeInsets.only(
                left: 10,
              ),
              child: TextFormField(
                controller: commentController,
                decoration: new InputDecoration(
                    suffixIcon: IconButton(
                        onPressed: () => widget.onPost(commentController.text),
                        icon: Icon(
                          FontAwesomeIcons.paperPlane,
                          size: 15,
                          color: Theme.of(context).primaryColor,
                        )),
                    contentPadding: EdgeInsets.all(10),
                    hintText: "Add a comment ...",
                    fillColor: Colors.white,
                    border: new OutlineInputBorder(
                      borderRadius: new BorderRadius.circular(20.0),
                    ),
                    focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                        borderSide:
                            BorderSide(width: 1, color: Color(0xFFff8b50)))),
                keyboardType: TextInputType.text,
                style: new TextStyle(fontFamily: "Poppins", fontSize: 15),
              ),
            ))
          ],
        ));
  }
}

导致页面刷新的问题是由于 SoftKeyboard 会在TextFormField处于焦点时弹出。

当我显示模态时,它运行平稳。但是当我开始点击field时,页面刷新,就好像再次调用了Future。

0 个答案:

没有答案