[英]How to get color inside a function that returns a widget?
I am learning flutter, I am trying to do something like 'values/color.xml' (which is used in android studio) to keep my colors in a file so that i can use them throughout my app easily.我正在学习颤动,我正在尝试做一些类似'values/color.xml' (在android studio中使用)的事情来将我的颜色保存在一个文件中,以便我可以在我的应用程序中轻松使用它们。 i have stored them in a class but when i use it inside a function that returns widget the colors are not appearing.
我已将它们存储在一个类中,但是当我在返回小部件的函数中使用它时,颜色没有出现。
Color Values Class:颜色值类:
import 'package:flutter/material.dart';
class ColorValues {
static Color highlightColor = const Color(0xFF202d35);
}
my function:我的功能:
Container currentPage() {
return Container(
height: 20.0,
width: double.infinity,
child: Text("index"),
color: ColorValues.highlightColor, //Doesn't work.
color: Colors.Black, // But this works perfectly.
);
}
Inside this container widget i will be displaying the index of current page of PageView.在这个容器小部件中,我将显示 PageView 当前页面的索引。 I am using ColorValues.highlightColor inside my build function and it is working fine there.
我在我的构建函数中使用 ColorValues.highlightColor,它在那里工作正常。 help me please.
请帮帮我。
Thank you.谢谢你。
Try this once.试试这个。 Remove the static keyword and if you are working in flutter then rename your color file as color.dart not as color.xml
删除 static 关键字,如果您正在使用颤振,则将您的颜色文件重命名为 color.dart 而不是 color.xml
将您的颜色文件重命名为color_value.dart
,而不是color.xml
,就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.