Flutter:DraggableScrollableSheet 出现时覆盖整个屏幕

Isa*_*aak 5 modal-dialog draggable flutter bottom-sheet

点击后,我执行以下功能,该功能应该使底部工作表以通常的方式出现(从底部向上滚动):

showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(30.0),
      ),
      isScrollControlled: true,
      isDismissible: true,
      backgroundColor: Colors.white,
      builder: (context) => ChooseTimeDialog(),
    );
Run Code Online (Sandbox Code Playgroud)

应该出现的底部工作表应该是可滚动的。它的代码如下:

class ChooseTimeDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DraggableScrollableSheet(
      initialChildSize: 0.4,
      minChildSize: 0.2,
      maxChildSize: 0.6,
      builder: (context, scrollController) {
        return SingleChildScrollView(
          controller: scrollController,
          child: Container(
            color: Colors.blue,
            height: 300,
            width: 200,
          ),
        );
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是出现在水龙头上的结果:

结果

为什么它覆盖整个屏幕?

Sat*_*was 19

设置isScrollControlledtrueofshowModalBottomSheet()和 设置expandfalseof DraggableScrollableSheet()

showModalBottomSheet(
   context: context,
   isScrollControlled: true,
   ....
   builder: (context) => ChooseTimeDialog(),
);

class ChooseTimeDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DraggableScrollableSheet(
      expand: false,
      ....
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Wil*_*ill 6

当 isScrollControlled 设置为“True”时,bottomModal 被允许占据视图的高度。将其设置为“False”会改变这一点。

我使用您的代码创建了这个 dartpad,但删除了构建方法的小部件类 https://dartpad.dev/5850ec2b79564bb28f361eeed2b383ec

如果您想将模态表的代码与调用函数分开,您应该使用一个变量,而不是一个新类。

这是上面 dartpad 文件中包含的代码:

class MyFloatingActionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: () {
        showModalBottomSheet(
          shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30.0),
                ),
          isScrollControlled: false,
          isDismissible: true,
          backgroundColor: Colors.white,
          context: context,

          builder: (context) => DraggableScrollableSheet(
            initialChildSize: 0.4,
            minChildSize: 0.2,
            maxChildSize: 0.6,
            builder: (context, scrollController) {
              return SingleChildScrollView(
                controller: scrollController,
                child: Container(
                  color: Colors.blue,
                  height: 300,
                  width: 200,
                ),
              );
            },
          ),
        );
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我更新了我的飞镖板,因为我意识到我发布的飞镖板很糟糕。我将用它来进一步讨论。(这里是:https://dartpad.dev/5850ec2b79564bb28f361eeed2b383ec) (2认同)