我有一段代码在 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'),
),
),
),
],
),
),
);
}
}
关于flutter - 从 Flutter 图像中的 Png 中删除白色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65640646/