4

我在 Flutter 应用程序中遇到了问题。即使 Scaffold 已将resizeToAvoidBottomInset设置为false ,键盘也会将模态底部表向上推。我希望模态底页保持在其初始位置。我将向您展示用于显示模态底部工作表的代码,并附上一段视频向您展示该错误。

Scaffold(
    resizeToAvoidBottomInset: false,
    key: _scaffoldKey,
    body: ...
)

showModalBottomSheet(
  context: context,
  isScrollControlled: true,
  backgroundColor: Colors.transparent,
  builder: (context) => Container(
      height: MediaQuery.of(context).size.height * 0.8,
      decoration: new BoxDecoration(
        color: Colors.white,
        borderRadius: new BorderRadius.only(
          topLeft: const Radius.circular(25.0),
          topRight: const Radius.circular(25.0),
        ),
      ),
      child: SearchPlace((place, alreadyExists) {
        Navigator.pop(context);
        didSelectPlace(place, alreadyExists);
      })),
);

希望你能帮助我,谢谢!

4

2 回答 2

5

好的,所以我自己找到了解决此问题的方法。

我希望模态底页占据屏幕的 80%,但它总是被键盘推动。为了解决这个问题,我将主Container包裹在Column小部件中,并添加了一个额外的透明 Container,它带有一个高度为屏幕 20% 的GestureDetector(以关闭底部工作表)。之后,我将 Column 包装在SingleChildScrollView中。现在一切都按预期工作!我在下面添加了一个视频。

showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    backgroundColor: Colors.transparent,
  builder: (context) => SingleChildScrollView(
    child: Column(children: [
      GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        child: Container(
          color: Colors.transparent,
          height: MediaQuery.of(context).size.height * 0.2,
        ),
      ),
      Container(
          height: MediaQuery.of(context).size.height * 0.8,
          decoration: new BoxDecoration(
            color: Colors.white,
            borderRadius: new BorderRadius.only(
              topLeft: const Radius.circular(25.0),
              topRight: const Radius.circular(25.0),
            ),
          ),
          child: SearchPlace((place, alreadyExists) => {
                Navigator.pop(context),
                didSelectPlace(place, alreadyExists),
              })),
    ]),
  ),
);
于 2021-02-15T14:17:41.357 回答
0

在这里解决了类似的问题请看一下-> https://stackoverflow.com/a/68660719/7760245

或使用

padding: MediaQuery.of(context).viewInsets // viewInsets will decorate your screen

完整代码->

showModalBottomSheet(
        context: context,
        barrierColor: popupBackground,
        isScrollControlled: true, // only work on showModalBottomSheet function
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(borderRadiusMedium),
                topRight: Radius.circular(borderRadiusMedium))),
        builder: (context) =>  Padding(
            padding: MediaQuery.of(context).viewInsets,
            child: Container(
                   height: 400, //height or you can use Get.width-100 to set height
                   child: <Your Widget here>
             ),)),)

图 1 图 2

于 2022-02-05T07:14:59.967 回答