168

I'm writing a Flutter app, and I'd like to use/implement the "frosted glass" effect that's common on iOS. How do I do this?

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
  • 2
    I write an article to show how to make blur effect on Flutter by using BackdropFilter & ImageFilter - read it [on Medium](https://medium.com/fluttervn/how-to-make-blur-effect-in-flutter-using-backdropfilter-imagefilter-559ffd8ab73?source=friends_link&sk=044ab21dd506a6146624ec3758b0d1b4) – anticafe Sep 30 '19 at 08:39
  • You can use the [blurrycontainer](https://pub.dev/packages/blurrycontainer) package. – Jon Jul 28 '20 at 14:56

4 Answers4

330

You can use the BackdropFilter widget to achieve this effect.

screenshot

import 'dart:ui';
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(home: new FrostedDemo()));
}

class FrostedDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        children: <Widget>[
          new ConstrainedBox(
            constraints: const BoxConstraints.expand(),
            child: new FlutterLogo()
          ),
          new Center(
            child: new ClipRect(
              child: new BackdropFilter(
                filter: new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
                child: new Container(
                  width: 200.0,
                  height: 200.0,
                  decoration: new BoxDecoration(
                    color: Colors.grey.shade200.withOpacity(0.5)
                  ),
                  child: new Center(
                    child: new Text(
                      'Frosted',
                      style: Theme.of(context).textTheme.display3
                    ),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
BananaNeil
  • 10,322
  • 7
  • 46
  • 66
Collin Jackson
  • 110,240
  • 31
  • 221
  • 152
  • How would I make the frosted effect cover the entire width/height of the app? – Pieter May 09 '17 at 17:08
  • 10
    you could use a Stack https://gist.github.com/collinjackson/321ee23b25e409d8747b623c97afa1d5 http://pasteboard.co/4ln6HDHWb.png – Collin Jackson May 09 '17 at 21:40
  • 3
    or, if you're trying to use the frosted glass effect as a modal barrier for dialogs, you could modify your copy of ModalBarrier to include a BackdropFilter. https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/modal_barrier.dart – Collin Jackson May 09 '17 at 21:46
  • Unfortunately the blur effect doesn't work on iOS devices: https://github.com/flutter/flutter/issues/10284 – Mark Oct 11 '17 at 03:04
  • 6
    iirc, the above issue has been solved and the blur effect works on iOS devices now :) – loganrussell48 Apr 18 '18 at 15:37
  • Is it possible to add a Feather effect to the blurred box corners? So the transition between the blurred area and the rest of the screen is progressive and not abrupt? – Michel Feinstein Feb 04 '20 at 05:45
  • how about disabling the background widget (e.g. all buttons etc, under the backdrop)? – onesiumus Apr 11 '20 at 07:12
  • @MichelFeinstein ClipRRect might be helpful for making the corners softer – Collin Jackson Apr 12 '22 at 22:41
  • It is worth mentioning that you must wrap the BackdropFilter with a ClipRRect or else it will blur the entire app. – faroukcharkas May 14 '22 at 02:22
26

I think I don't know the exact meaning of 'Frosted'(If my example didnot work here),

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

void main() => runApp(
    MaterialApp(
        title: "Frosted glass",
        home: new HomePage()
    )
);

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        fit: StackFit.expand,
        children: <Widget>[
          generateBluredImage(),
          new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              rectShapeContainer(),
            ],
          ),
        ],
      ),
    );
  }

  Widget generateBluredImage() {
    return new Container(
      decoration: new BoxDecoration(
        image: new DecorationImage(
          image: new AssetImage('assets/images/huxley-lsd.png'),
          fit: BoxFit.cover,
        ),
      ),
      //I blured the parent container to blur background image, you can get rid of this part
      child: new BackdropFilter(
        filter: new ui.ImageFilter.blur(sigmaX: 3.0, sigmaY: 3.0),
        child: new Container(
          //you can change opacity with color here(I used black) for background.
          decoration: new BoxDecoration(color: Colors.black.withOpacity(0.2)),
        ),
      ),
    );
  }

  Widget rectShapeContainer() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 40.0, vertical: 10.0),
      padding: const EdgeInsets.all(15.0),
      decoration: new BoxDecoration(
        //you can get rid of below line also
        borderRadius: new BorderRadius.circular(10.0),
        //below line is for rectangular shape
        shape: BoxShape.rectangle,
        //you can change opacity with color here(I used black) for rect
        color: Colors.black.withOpacity(0.5),
        //I added some shadow, but you can remove boxShadow also.
        boxShadow: <BoxShadow>[
          new BoxShadow(
            color: Colors.black26,
            blurRadius: 5.0,
            offset: new Offset(5.0, 5.0),
          ),
        ],
      ),
      child: new Column(
        children: <Widget>[
          new Text(
            'There\'s only one corner of the universe you can be certain of improving and that\'s your own self.',
            style: new TextStyle(
              color: Colors.white,
              fontSize: 20.0,
            ),
          ),
        ],
      ),
    );
  }
}

Outcome:

enter image description here

I hope this will help someone.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
7
BackdropFilter(
  filter: ImageFilter.blur(sigmaX: _sigmaX, sigmaY: _sigmaY),
  child: Container(
    color: Colors.black.withOpacity(_opacity),
  ),
),
Andrey Ozornin
  • 1,129
  • 1
  • 9
  • 24
Mahmoud Hamdy
  • 71
  • 1
  • 4
0

BackdropFilter constructor:

BackdropFilter({
  Key? key, 
  required ImageFilter filter, 
  Widget? child, 
  BlendMode blendMode = BlendMode.srcOver
})

ImageFilter.blur constructor:

ImageFilter.blur({
  double sigmaX = 0.0, 
  double sigmaY = 0.0, 
  TileMode tileMode = TileMode.clamp
})

Example 1: Blurred Background Image Preview: enter image description here

// main.dart
import 'package:flutter/material.dart';
import 'dart:ui';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Kindacode.com',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: NetworkImage(
                    'https://www.kindacode.com/wp-content/uploads/2021/04/background.jpeg'),
                fit: BoxFit.cover)),
        child: BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
          child: Center(
            child: Card(
              elevation: 10,
              color: Colors.black.withOpacity(0.5),
              child: const SizedBox(
                width: 300,
                height: 200,
                child: Center(
                  child: Text(
                    'Some Text',
                    style: TextStyle(fontSize: 30, color: Colors.white),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Rohan Arora
  • 303
  • 2
  • 12