Create widget with transparent hole inside

后端 未结 1 831
一生所求
一生所求 2021-02-06 12:41

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

相关标签:
1条回答
  • 2021-02-06 12: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:

    0 讨论(0)
提交回复
热议问题