【发布时间】:2021-08-05 08:27:37
【问题描述】:
我正在尝试创建一个 Row 的容器,这些容器都具有 BoxShadow。阴影非常大,因此跨越到父级之外的空间,所以我在父级上使用clipBehavior: Clip.none。
这样做的问题是,对于行内的每个 Container,它的阴影都会重叠到前一个容器中。我在下面设置了一些粗体颜色来强调这个问题。如您所见,前一个容器的右侧从下一个容器中获取阴影。
这是我的代码
return SingleChildScrollView(
clipBehavior: Clip.none,
scrollDirection: Axis.horizontal,
child: Row(
children: [
_ShadowedBox(),
_ShadowedBox(),
_ShadowedBox(),
],
),
);
class _ShadowedBox extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 144,
child: Column(
children: [
SizedBox(
width: 144,
height: 144,
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(cardRadius),
boxShadow: [
BoxShadow(
offset: const Offset(0.0, 6.0),
blurRadius: 38.0,
spreadRadius: 0.0,
color: Colors.red,
),
BoxShadow(
offset: const Offset(0.0, 6.0),
blurRadius: 46.0,
spreadRadius: 0.0,
color: Colors.green,
),
BoxShadow(
offset: const Offset(0.0, 6.0),
blurRadius: 10.0,
spreadRadius: 0.0,
color: Colors.blue,
),
]
),
child: ClipRRect(
borderRadius: BorderRadius.circular(cardRadius),
child: Image.network("https://via.placeholder.com/400x400.png?text=Coming%20soon", fit: BoxFit.contain)
),
),
],
),
);
}
}
谁能建议如何阻止阴影渗入其他容器,同时仍然能够显示未被父级剪切的阴影?
【问题讨论】:
-
重要的
clipBehavior: Clip.none实际上是_ShadowedBox的Container。正如您在实际中看到的那样,当它们同时绘制时,很难将阴影与它们的小部件分开,使其看起来像一个堆栈。你不能使用边距和剪辑行为的组合来在你的元素之间创建空间吗?也许你可以分享一个最终设计?