如何在Flutter中设置showModalBottomSheet的宽度

3

我有一个设计(如照片所示),并且正在使用showModalBottomSheet,但是当我设置宽度时,它不会改变并保持屏幕宽度,因此我有几个问题:

1-如何为showModalBottomSheet设置宽度
2-是否有其他替代showModalBottomSheet的底部菜单
3-如何模糊背景,就像照片中显示的那样

showModalBottomSheet<void>(
          context: context,
          builder: (BuildContext context) {
            return Container(
              height: SizeConfig.screenHeight * 0.6,
              width: 30,
              color: Colors.red,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    const Text('Modal BottomSheet'),
                    ElevatedButton(
                      child: const Text('Close BottomSheet'),
                      onPressed: () => Navigator.pop(context),
                    )
                  ],
                ),
              ),
            );
          },
        );

enter image description here

3个回答

5

在showModalBottomSheet中使用constraints属性。

 showModalBottomSheet(
    context: context,
    constraints: BoxConstraints(
      maxWidth:  600,              
    ),
    builder: ...
  ),

3

我通过在容器中再放置一个容器来解决了这个问题。
父容器透明,而子容器则有实心颜色和内边距。
但是,我仍然没有想出如何模糊背景。
以下是代码:

showModalBottomSheet(
            context: context,
            backgroundColor: Colors.transparent,
            builder: (BuildContext bc) {
              return Container(
                height: SizeConfig.screenHeight * 0.6,
               
                child: Padding(
                  padding: EdgeInsets.only(left: SizeConfig.screenWidth * 0.4),
                  child: Container(
                  
                    child: SingleChildScrollView(
                      child:
                        
                          Padding(
                        padding: EdgeInsets.only(
                            top: SizeConfig.blockSizeVertical * 1.5),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            
                          ],
                        ),
                      ),
                     
                    ),
                  ),
                ),
              );
            });

0
如果您想要自定义高度和宽度,可以使用以下代码将其定位到屏幕右侧,宽度为50%,高度为75%。虽然有些答案也可以实现此功能,但我在这里添加了一个额外的功能:如果用户单击表格左侧,它将弹出,因此它将完美地与您的设计相匹配。
showModalBottomSheet(
            backgroundColor: Colors.transparent,
            isScrollControlled: true,
            context: context,
            builder: (context) => GestureDetector(
              onTapDown: (tapDetails) {
                //closing bottom sheet on tapping on the left
                if (tapDetails.globalPosition.dx <
                    MediaQuery.of(context).size.width * 0.5) {
                  Navigator.pop(context);
                }
              },
              child: Container(
                height: MediaQuery.of(context).size.height * 0.75,
                padding: EdgeInsets.only(
                    left: MediaQuery.of(context).size.width * 0.5),
                color: Colors.transparent,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(20.0),
                      topRight: Radius.circular(20.0),
                    ),
                  ),
                ),
              ),
            ),
          );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接