Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent BoxShadow from being clipped by parent

I'm building my own material-like card with a box shadow. I want to have several of these combined in a PageView, so that I can swipe between cards - each Card should fill out the entire width of the screen. The cards are having a BoxShadow as decoration, however when inserting the Card into the PageView (or any other wrapping widget), the BoxShadow will disappear as it is clipped by the PageView's bounding box.

This can be fixed by wrapping the Card into a Padding, but that's not what I want since I prefer the padding to be given by the outermost widget to all child widgets of that entire view - so that in case my padding ever changes I don't have to change every single widget.

This is how my Card code looks like:

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,
      ),
    );
  }
}

Wrapping several of these results in the described clipping behavior.

Is there a way of telling Flutter not to clip whatever is "leaking" out of the bounding box? The same behavior happens when positioning widgets on a stack out of bounce.

like image 357
Philip Feldmann Avatar asked Feb 02 '19 09:02

Philip Feldmann


1 Answers

You can use margins:

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

Maybe you would be interested with viewportFraction PageController argument:

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

It shrinks a size of the page in PageView, so nearest pages become visible.

enter image description here

like image 197
Kherel Avatar answered Nov 12 '22 02:11

Kherel