【问题标题】:How to fix cropped shadow in a Horizontal List View in Flutter如何在 Flutter 中修复水平列表视图中的裁剪阴影
【发布时间】:2019-08-14 11:51:40
【问题描述】:

当我在 ListView 中创建一个带有盒子阴影的容器(水平滚动)时,阴影看起来不错,但是,当我在 ListView 中添加多个容器时,它们的阴影(只是阴影,而不是容器)在顶部和底部被裁剪.

还请注意,整个 ListView 都包裹在一个父 Container 下。

我试图增加父容器的高度(整个 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,但在实际输出中,Containers 的 BoxShadow 会从顶部和底部进行裁剪。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

    解决问题的步骤

    1. 创建几个具有一定宽度和半径为 10.0 的 BoxShadow 的容器(作为卡片)。让我们称之为子容器。

    2. 创建一个带有水平滚动轴的 ListView。将上面制作的子容器放在这个 ListView 中。

    3. 现在将 ListView 包装在一个新的容器中(我们称之为父容器),为 ListView 提供一个高度。如果您希望子容器的高度为 140.0,则将父容器的高度设置为 160.0,其中包括上下各 10.0 的 BoxShadow 半径(10.0+ 140.0 + 10.0)。

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

    5. 问题解决了(傻我)。

    此处的示例代码

    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,
          ),
        ],
      ),
    ),
    

    【讨论】:

    • 这将导致 ListView 与同一列或行中的其他小部件之间的差距更大。
    • 您可以通过将 clip 设置为 none 来使用 listView 中的 clip 选项:clipBehavior: Clip.none
    【解决方案2】:

    正如@Zahra 所写,

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

    仅当您的小部件超出屏幕尺寸时才会进行裁剪

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-07
      • 2012-11-08
      • 1970-01-01
      • 2013-07-12
      • 2023-03-27
      • 2020-08-17
      • 2015-12-03
      • 2021-06-24
      相关资源
      最近更新 更多