简体   繁体   中英

How to set a BackdropFilter using dynamic-sized widgets in Flutter

I have a column: header with text, body with image, footer with text, all widgets have transparent backgrounds. I want to set the background using a blur of the main image but I keep reaching dead ends.

In some situations this would be straight forward but in my scenario the image could be of any size and aspect ratio, and I need the effect to be wrapped with the column.

Here are my two failed attempts:

Method 1

I have a Stack with the image as the first item, then a BackdropFilter with ImageFilter blur, then my Column. This works but the Image bleeds out from under the Column because of the image size (which can be any size). I want to constrain it to the height of my Column.

return Container(
      child: Stack(
        alignment: AlignmentDirectional.topCenter,
        children: <Widget>[
          Positioned.fill(child: Image.network(_imgUrl)),
          BackdropFilter(
              filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
              child: Container(
                decoration: BoxDecoration(color: Colors.white.withOpacity(0.0)),
              )),
          Container(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              // https://stackoverflow.com/a/41846093/3429021
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Header(),
                BodyPhoto(),
                Footer()
              ],
            ),
          ),
        ],
      ),

Method 2

Putting the column in a Container with a DecorationImage, which sizes the image perfectly, but I have no way of applying the blur effect.

(_streamItem is my Column wrapped in a container)

body: Container(child: _streamItem,
          decoration: new BoxDecoration(image: DecorationImage(
              image: NetworkImage(_streamItem.imgUrl),
              fit: BoxFit.cover)
          )
      )

Any ideas?

You can explicitly give a fix height to the container that contains the image, which will even look better.

In this example I have set three images of very different dimensions and has no problem rendering.

Refer the below code:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('So Help'),
      ),
      body: new ListView(
        children: <Widget>[
          MyContainer(
            imageUrl:
                'https://i.dailymail.co.uk/i/pix/2017/01/16/20/332EE38400000578-4125738-image-a-132_1484600112489.jpg',
          ),
          MyContainer(
            imageUrl:
                'http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/4/11/1397210130748/Spring-Lamb.-Image-shot-2-011.jpg',
          ),
          MyContainer(
            imageUrl:
                'http://wackymania.com/image/2011/6/vertical-panoramic-photography/vertical-panoramic-photography-15.jpg',
          ),
        ],
      ),
    );
  }
}

class MyContainer extends StatelessWidget {
  final String imageUrl;

  MyContainer({@required this.imageUrl});

  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 300.0,
      child: new Stack(
        children: <Widget>[
          new Container(
            child: Image.network(
              this.imageUrl,
              width: MediaQuery.of(context).size.width,
              fit: BoxFit.cover,
            ),
          ),
          BackdropFilter(
            filter: Dui.ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
            child: new Container(
              color: Colors.transparent,
            ),
          ),
          new Container(
            height: 300.0,
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: MediaQuery.of(context).size.width,
                ),
                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: new Text(
                    'Header',
                    style: new TextStyle(color: Colors.white),
                  ),
                ),
                new Expanded(
                  child: Image.network(imageUrl),
                ),
                Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: new Text(
                    'Footer',
                    style: new TextStyle(color: Colors.white),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM