【问题标题】:Flutter: Transparent child with fill parent's background colorFlutter:透明的孩子,填充父母的背景颜色
【发布时间】:2020-12-15 10:07:21
【问题描述】:

我只是想显示相机的预览并调整用户以进行自拍。

我有以下 sn-p 代码,我想让这个人的图像透明(不是白色)并用颜色填充周围区域。

但是我面临的问题,如果我将SVG设置为透明色,它将显示父(容器)的颜色,

我需要使人的图像完全透明才能看到相机的预览,并用颜色填充周围区域。

Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Stack(
          children: [
            Container(
              height: MediaQuery.of(context).size.height,
              child: Center(
                child: _cameraPreviewWidget(),
              ),
            ),
            Container(
              height: MediaQuery.of(context).size.height,
              color: Colors.grey[700].withOpacity(0.8),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: SvgPicture.asset(
                  'assets/svg/selfie_person.svg',
                  alignment: Alignment.bottomCenter,
                  fit: BoxFit.cover,
                  color: Colors.white,
                ),
              ),
            ),
            Container(
              height: MediaQuery.of(context).size.height,
              child: Padding(
                padding: EdgeInsets.only(
                  top: MediaQuery.of(context).size.height * 0.05,
                  bottom: MediaQuery.of(context).size.height * 0.15,
                ),
                child: Column(
                  children: [
                    ListTile(
                      leading: InkWell(
                        onTap: () {
                          Navigator.pop(context, null);
                        },
                        child: FaIcon(
                          FontAwesomeIcons.arrowLeft,
                          color: Colors.white,
                        ),
                      ),
                      title: Center(
                        child: Text(
                          "Take a selfie",
                          style: Theme.of(context).textTheme.subtitle2,
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(
                        horizontal: MediaQuery.of(context).size.width * 0.05,
                      ),
                      child: Text(
                        "Take a quick selfie so we know it's you, this is never public",
                        style: Theme.of(context).textTheme.subtitle2,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 3,
                        textAlign: TextAlign.center,
                      ),
                    ),
                    Expanded(
                      child: Align(
                        alignment: Alignment.bottomCenter,
                        child: _captureButton(),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );

【问题讨论】:

  • 也许您可以尝试制作一个新图像,将正方形前面的人的轮廓剪掉,轮廓是空的。您的图像将是灰色部分,而不是您现在拥有的白色部分。
  • 这是个好主意,如果我没有找到 Flutter 的任何解决方案,我会这样做

标签: image flutter flutter-layout transparency


【解决方案1】:

我不太理解你的问题,但我认为你应该尝试 Opacity 类 .. 用 Opacity 包装你的 SVG

你可以检查一下:

https://api.flutter.dev/flutter/widgets/Opacity-class.html

【讨论】:

  • 如果我将 Opacity 与 SVG 一起使用,我会看到 Container 的背景颜色,但我想看看整个 Container 背后的东西(相机预览)
  • 如果你删除了一个容器和背景灰色。你仔细看摄像头了吗?
  • 是的,我看到了相机的预览,即使是通过容器的背景颜色,因为不透明度
  • 使用 Opacity Svg 后能否提供一张图片??
猜你喜欢
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多