如何在Flutter中的水平列表视图中修复裁剪阴影

时间:2019-03-23 19:17:09

标签: dart flutter

当我在ListView内创建带有框阴影的容器(水平滚动)时,阴影看起来很好,但是,当我在ListView内添加多个Container时,其阴影(只是阴影,不是Container)在顶部和底部被裁剪

请注意,整个ListView都包装在父容器下。

我试图增加父容器的高度(包裹整个ListView),但是它增加了子容器的高度,并且阴影仍然被裁剪。

我也尝试给父容器添加填充,但是仍然阴影仍然被裁剪。

也许我需要将ListView包裹在任何其他Widget中,这样才能顺利完成工作。

Container(
  // padding: EdgeInsets.only(left: 30.0, right: 0.0),
  height: 140.0,
  child: ListView(
    scrollDirection: Axis.horizontal,
    children: <Widget>[
      SizedBox(
        width: 30.0,
      ),
      Container(
        //This is actual custom Card
        width: 340.0,
        height: 140.0,
        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.black12,
              offset: Offset.zero,
              blurRadius: 10.0,
              spreadRadius: 0.0,
            )
          ],
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: Row(
          children: <Widget>[
            Container(
              padding: EdgeInsets.fromLTRB(
                  30.0, 20.0, 25.0, 20.0),
              child: Image.asset(
                  'assets/images/leather_boot.png'),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 30.0, bottom: 30.0),
              child: Column(
                crossAxisAlignment:
                    CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'BadAss Genuine',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w500,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 3.0,
                  ),
                  Text(
                    'Leather Boots',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w500,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Container(
                    height: 1.5,
                    width: 150.0,
                    color: Color(0xff643523),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: 30.0,
      ),
      Container(
        //This is actual custom Card
        width: 340.0,
        height: 140.0,
        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.black12,
              offset: Offset.zero,
              blurRadius: 10.0,
              spreadRadius: 0.0,
            )
          ],
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.0),
        ),
        child: Row(
          children: <Widget>[
            Container(
              padding: EdgeInsets.fromLTRB(
                  30.0, 20.0, 25.0, 20.0),
              child: Image.asset(
                  'assets/images/leather_boot.png'),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 30.0, bottom: 30.0),
              child: Column(
                crossAxisAlignment:
                    CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'BadAss Genuine',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w500,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 3.0,
                  ),
                  Text(
                    'Leather Boots',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w500,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Container(
                    height: 1.5,
                    width: 150.0,
                    color: Color(0xff643523),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: 30.0,
      ),
    ],
  ),
),

我希望ListView的Containers(作为自定义卡)具有适当的BoxShadow,但在实际输出中,Container的BoxShadow将从顶部和底部开始裁剪。

2 个答案:

答案 0 :(得分:7)

正如@Zahra 所写,

clipBehavior: Clip.none, 添加到 ListView,解决了问题。

仅当您的小部件超过屏幕尺寸时才会发生裁剪

答案 1 :(得分:0)

好的,我自己找到了解决方法。

解决问题的步骤

  1. 创建一些具有一定宽度并具有Radius 10.0的BoxShadow的容器(作为卡)。我们称之为子容器。

  2. 使用水平滚动轴创建ListView。将上面制作的子容器放入此ListView。

  3. 现在将ListView包裹在一个新的Container中(称为父容器),以赋予ListView一个高度。如果您希望子容器的高度为140.0,则使父容器的高度为160.0,其中上下分别包含BoxShadow的半径为10.0(10.0+ 140.0 + 10.0)。

  4. 现在为顶部和底部的10.0的ListView提供填充。

  5. 问题已解决(Silly Me)。

此处提供示例代码

Container(
  height: 160.0,
  child: ListView(
    padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
    shrinkWrap: true,
    scrollDirection: Axis.horizontal,
    children: <Widget>[
      SizedBox(
        width: 30.0,
      ),
      Container(
        width: 340.0,
        // height: 140.0,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.0),
          boxShadow: [
            BoxShadow(
              blurRadius: 10.0,
              color: Colors.black12,
            )
          ],
        ),
        child: Row(
          children: <Widget>[
            Container(
              padding: EdgeInsets.fromLTRB(
                  30.0, 20.0, 25.0, 20.0),
              child: Image.asset(
                  'assets/images/leather_boot.png'),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 20.0, bottom: 20.0),
              child: Column(
                crossAxisAlignment:
                    CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'BadAss Genuine',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 3.0,
                  ),
                  Text(
                    'Leather Boots',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Container(
                    height: 1.0,
                    width: 150.0,
                    color: Color(0xff643523),
                  ),
                                      SizedBox(
                    height: 12.0,
                  ),
                  Row(
                    children: <Widget>[
                      SizedBox(
                        width: 110.0,
                      ),
                      Container(
                        height: 30.0,
                        width: 30.0,
                        child: Image.asset(
                            'assets/images/boot.png'),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: 30.0,
      ),
      Container(
        //This is actual custom Card
        width: 340.0,
        // height: 140.0,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.0),
          boxShadow: [
            BoxShadow(
              blurRadius: 10.0,
              color: Colors.black12,
            )
          ],
        ),
        child: Row(
          children: <Widget>[
            Container(
              padding: EdgeInsets.fromLTRB(
                  30.0, 20.0, 25.0, 20.0),
              child:
                  Image.asset('assets/images/gloves.png'),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 20.0, bottom: 20.0),
              child: Column(
                crossAxisAlignment:
                    CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'Highly Durable',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 3.0,
                  ),
                  Text(
                    'Riding Gloves',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Container(
                    height: 1.0,
                    width: 150.0,
                    color: Color(0xff643523),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Row(
                    children: <Widget>[
                      SizedBox(
                        width: 110.0,
                      ),
                      Container(
                        height: 30.0,
                        width: 30.0,
                        child: Image.asset(
                            'assets/images/glove.png'),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: 30.0,
      ),
      Container(
        //This is actual custom Card
        width: 340.0,
        // height: 140.0,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10.0),
          boxShadow: [
            BoxShadow(
              blurRadius: 10.0,
              color: Colors.black12,
            )
          ],
        ),
        child: Row(
          children: <Widget>[
            Container(
              padding: EdgeInsets.fromLTRB(
                  30.0, 20.0, 25.0, 20.0),
              child:
                  Image.asset('assets/images/helmet.png'),
            ),
            Container(
              padding:
                  EdgeInsets.only(top: 20.0, bottom: 20.0),
              child: Column(
                crossAxisAlignment:
                    CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'German Hat',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 3.0,
                  ),
                  Text(
                    'Riding Helmet',
                    style: TextStyle(
                      color: Colors.black,
                      fontWeight: FontWeight.w400,
                      fontSize: 18.0,
                    ),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Container(
                    height: 1.0,
                    width: 150.0,
                    color: Color(0xff643523),
                  ),
                  SizedBox(
                    height: 12.0,
                  ),
                  Row(
                    children: <Widget>[
                      SizedBox(
                        width: 110.0,
                      ),
                      Container(
                        height: 30.0,
                        width: 30.0,
                        child: Image.asset(
                            'assets/images/helmeticon.png'),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: 30.0,
      ),
    ],
  ),
),