[英]Flutter color of an asset
Is there any way to set the color of an SVG asset as a static variable?有没有办法将 SVG 资产的颜色设置为静态变量? for example instead of having it like stroke="#0059ee8" stroke="@color/primary"例如,而不是像 stroke="#0059ee8" stroke="@color/primary"
You can have a enums.dart
file under lib/constants
where you can maintain all the colors like:您可以在lib/constants
下有一个enums.dart
文件,您可以在其中维护所有颜色,例如:
const C_THEME_COLOR = 0xFFF46523;
And you can access the color where ever you need like:您可以在任何需要的地方访问颜色,例如:
backgroundColor: Color(C_THEME_COLOR),
Add import like:添加导入,如:
import 'package:my_project/constants/enums.dart';
You can try adding color to the SvgPicture widget itself.您可以尝试为 SvgPicture 小部件本身添加颜色。 Example using flutter_svg: ^0.22.0 package.使用 flutter_svg 的示例:^0.22.0 包。 Below is a snippet of the same.下面是相同的片段。
import 'package:flutter_svg/flutter_svg.dart';
Container(
height: 30,
child: SvgPicture.asset(
'assets/svg/add_money.svg',
color:
Theme.of(context).primaryColor),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.