首页 > 解决方案 > 防止 BoxShadow 被父级裁剪

问题描述

我正在用盒子阴影构建我自己的类似材质的卡片。我想将其中的几个组合在一起PageView,这样我就可以在卡片之间滑动——每张卡片都Card应该填满屏幕的整个宽度。卡片具有BoxShadowas 装饰,但是当将 插入CardPageView或任何其他包装小部件)时,它会在被的边界框BoxShadow夹住时消失。PageView

这可以通过将 包装Card成 a来解决Padding,但这不是我想要的,因为我更喜欢由最外面的小部件为整个视图的所有子小部件提供填充 - 这样以防我的填充发生变化我没有更改每个小部件。

这就是我的Card代码的样子:

class Card extends StatelessWidget {
  final Widget child;
  final EdgeInsetsGeometry padding;

  Card({@required this.child, this.padding});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Colors.white, boxShadow: <BoxShadow>[
        BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.1), blurRadius: 14.0),
        BoxShadow(
            color: Color.fromRGBO(0, 0, 0, 0.1),
            offset: Offset(0, 2),
            blurRadius: 2.0)
      ]),
      child: Padding(
        padding: padding ?? EdgeInsets.all(20),
        child: this.child,
      ),
    );
  }
}

包装其中的几个会导致所描述的剪辑行为。

有没有办法告诉 Flutter 不要剪掉任何从边界框“泄漏”出来的东西? 当将小部件放置在弹跳的堆栈上时,也会发生相同的行为。

标签: flutterflutter-layout

解决方案


您可以使用边距:

...
Container(
  margin: EdgeInsets.all(10), 
..

也许您会对 viewportFraction PageController 参数感兴趣:

final PageController controller = PageController(
  viewportFraction: 1,
);
...
PageView.builder(
  controller: controller,

它缩小了 PageView 中页面的大小,因此最近的页面变得可见。

在此处输入图像描述


推荐阅读