You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在有色Container上方实现文本颜色透明效果

实现有色容器上方的文本透明效果

用Flutter的Stack配合BlendMask就能实现这个需求,核心是让文本区域挖空有色容器,透出下方内容,代码示例如下:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.deepPurple[900], // 底层背景色
        body: Center(
          child: SizedBox(
            width: 320,
            height: 160,
            child: Stack(
              fit: StackFit.expand,
              children: [
                // 有色容器层
                Container(color: Colors.white.withOpacity(0.8)),
                // 透明文本层
                BlendMask(
                  blendMode: BlendMode.dstOut,
                  child: const Center(
                    child: Text(
                      "WIDGET TEXT",
                      style: TextStyle(
                        fontSize: 36,
                        fontWeight: FontWeight.w900,
                        color: Colors.black, // 颜色不影响,仅作为BlendMode的载体
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

关键说明

  • BlendMode.dstOut是核心:它会让文本所在区域清除掉下方的有色容器,从而透出最底层的背景
  • 如果需要透出的是图片而非纯色背景,把Scaffold的backgroundColor换成Image组件即可

内容的提问来源于stack exchange,提问作者Khamidjon Khamidov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 06:21:58