flutter - 从 Flutter 图像中的 Png 中删除白色背景

标签 flutter dart

我有一段代码在 DecorationImage 中显示图像(或图像路径,如果保存),但问题是透明 png 透明度被视为白色背景(_logo 是一个文件),这里是代码:

decoration: BoxDecoration(
      image: DecorationImage(
           image: _logopath != null
              ? FileImage(File(_logopath))
                : _logo == null
                 ? MemoryImage(kTransparentImage)
                  : FileImage(_logo),
                   fit: BoxFit.contain),

我的问题是是否有某种方法可以使所选的 png 图像实际上是透明的,并且没有白色背景填充透明度。

最佳答案

我认为它已经是透明的了。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Stack(
          children: [
            Container(
              color: Colors.grey,
            ),
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fitWidth,
                  image: AssetImage('images/gyvegetable.png'),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

sample

关于flutter - 从 Flutter 图像中的 Png 中删除白色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65640646/

相关文章:

arrays - 在 flutter/Dart 中创建二维数组

flutter - 我迁移到空安全,我不能在没有抛出错误的情况下运行 "flutter pub run build_runner build"

flutter - 在具有附加权限的情况下在与剪贴板数据相关的调试控制台中获取警告错误

flutter - 如何在Flutter上收听isDrawerOpen?

flutter - 将字符串传递给 Flutter 中的类?

dart - 创建一个倒数计时器,每 5 秒为列表中的每个值打印一次剩余时间

ios - IOS 构建中的重复符号

flutter - 使用提供程序包 FLUTTER 在应用程序关闭后保存状态

flutter - Isolate 的入口点函数不能标记为异步

flutter - 从List <String> flutter中的Wrap内部生成按钮