25

How can I create a semi-transparent background with the transparent hole inside? I tried to use decoration and foreground decorations with different blend modes, stack, ClipRect, colorfilters, but nothing works. I will appreciate any ideas. Thanks!

Semi-transparent background with transparent hole inside

1

1 Answer 1

58

The "easiest" way I've found to do it is using a ColorFiltered Widget with a Stack.

The following code will create exactly what you need:

@override
Widget build(BuildContext context) {
return Material(
  child: Stack(
    fit: StackFit.expand,
    children: [
      Image.network(
        'https://wallpaperplay.com/walls/full/e/5/3/13586.jpg',
        fit: BoxFit.cover,
      ),
      ColorFiltered(
        colorFilter: ColorFilter.mode(
            Colors.black.withOpacity(0.8), BlendMode.srcOut), // This one will create the magic
        child: Stack(
          fit: StackFit.expand,
          children: [
            Container(
              decoration: BoxDecoration(
                  color: Colors.black,
                  backgroundBlendMode: BlendMode.dstOut), // This one will handle background + difference out
            ),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                margin: const EdgeInsets.only(top: 80),
                height: 200,
                width: 200,
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(100),
                ),
              ),
            ),
            Center(
              child: Text(
                'Hello World',
                style: TextStyle(fontSize: 70, fontWeight: FontWeight.w600),
                ),
              )
            ],
          ),
        ),
      ],
    ),
  );
}

This one you not only create "holes" over views, it works with anything! including texts, etc.

Final result:

Mask Out

4
  • 3
    You are a miracle worker. Thanks! This was a lifesaver for keeping an element visible beneath an overlay. Nov 30, 2020 at 23:06
  • 3
    Is it possible to make black background with blur? Mar 29, 2021 at 21:47
  • @ArturDumchev Did you find a solution for combining opacity and blur?
    – tigrenok00
    Jun 29, 2022 at 11:58
  • Just wrap the Image.network with ImageFiltered widget and set it's imageFilter property. imageFilter: ImageFilter.blur(sigmaX: 3, sigmaY: 3),
    – Pars
    Jan 26 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.