美文网首页Flutterflutter
flutter-底部弹出框,半透明背景+圆角问题

flutter-底部弹出框,半透明背景+圆角问题

作者: 浮华_du | 来源:发表于2021-04-21 15:03 被阅读0次

    今天,要实现一个底部弹出的、背景半透明的、上面两个是圆角的框,大概是这样.


    image.png

    1.想到了使用showBottomSheet

    showBottomSheet(
                                  backgroundColor: Colors.black54,
                                  context: context,
                                  builder: (context) {
                                    return Container(
                                      height: 400,
                                      width: double.infinity,
                                      decoration: BoxDecoration(
                                          color: Colors.white,
                                          borderRadius: BorderRadius.only(
                                              topLeft: Radius.circular(30),
                                              topRight: Radius.circular(30))),
                                    );
                                  });
    
    image.png
    • 可是实验发现,这个widget的backgroundColor属性,并不是整个弹窗的半透明背景,而是弹出的这个widget内容的背景,而且点击背景,弹窗也无法消失.

    2.接着,我找到了这个showModalBottomSheet

    showModalBottomSheet(
                                barrierColor: Colors.black54,
                                context: context,
                                builder: (ctx) {
                                  return Container(
                                    height: 400,
                                    width: double.infinity,
                                    decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.only(
                                            topLeft: Radius.circular(30),
                                            topRight: Radius.circular(30))),
                                  );
                                },
                              );
    
    image.png
    • 可是,问题又出现了,圆角怎么没有出现?(背景是全透明时,可以看到child内容的圆角)

    3.几经周转,终于让我找到了答案:除了给内容设置圆角之外, showModalBottomSheet也要增加shape属性.

    https://blog.csdn.net/qq_35317752/article/details/103813196

     showModalBottomSheet(
                                barrierColor: Colors.black54,
                                context: context,
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(30),
                                        topRight: Radius.circular(30))),
                                builder: (ctx) {
                                  return Container(
                                    height: 400,
                                    width: double.infinity,
                                    decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.only(
                                            topLeft: Radius.circular(30),
                                            topRight: Radius.circular(30))),
                                  );
                                },
                              );
    

    相关文章

      网友评论

        本文标题:flutter-底部弹出框,半透明背景+圆角问题

        本文链接:https://www.haomeiwen.com/subject/poqzlltx.html