英文:
Different colors on gridView items retrieved from Firebase?
问题
我对这个还比较新手,有点困惑。尝试在网格视图项目上设置不同的背景颜色,但我知道的所有方法似乎都不起作用,因为数据来自Firebase。
var colors = [
Colors.red,
Colors.blue,
Colors.cyan,
Colors.green,
Colors.yellow,
];
Widget gridPost(
String Name,
String JobTitle,
String Company,
String photo,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: colors[index]
)
);
}
这不起作用是因为`colors[index]`中的`index`部分。如果我将`int index`添加到`gridPost`小部件中,然后也将其添加到
return gridPost(
snapshot.data!.docs[index]['index'],
)
但这也不起作用,因为Firebase中没有这个。在Firebase中添加一个虚拟字段也没有起作用。请帮帮忙 :)
这大致是我想要的。
英文:
I'm pretty new to this and I'm a bit stumped. Trying to have various background colours on gridView items but all the ways I know of don't seem to work, because the data is coming from firebase.
var colors = [
Colors.red,
Colors.blue,
Colors.cyan,
Colors.green,
Colors.yellow,
];
Widget gridPost(
String Name,
String JobTitle,
String Company,
String photo,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: colors[index]
)),}
This doesn't work because of the 'index' part of colors[index]. if I add int index to the gridPost widget, it then wants it also as
return gridPost(
snapshot.data!.docs[index]['index'],)
but that doesn't work either because that doesn't exist in Firebase. adding a dummy field to firebase also didn't work. Please help
This is more or less what I'm after
答案1
得分: 1
以下是您要翻译的代码部分:
if you want to create random color and use it in the background you can do it like this
Widget gridPost(
String Name,
String JobTitle,
String Company,
String photo,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0)
)),}
**EDIT**
if you want to select from the list try this code:
Widget gridPost(
String Name,
String JobTitle,
String Company,
String photo,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius all(Radius.circular(20)),
color: colors[math.Random().nextInt(colors.length)]
)),}
如果您有其他需要,请随时告诉我。
英文:
if you want to create random color and use it in the background you can do it like this
Widget gridPost(
String Name,
String JobTitle,
String Company,
String photo,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0)
)),}
EDIT
if you want to select from the list try this code:
Widget gridPost(
String Name,
String JobTitle,
String Company,
String photo,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: colors[math.Random().nextInt(colors.length)]
)),}
答案2
得分: 0
尝试创建一个像这样的方法:
List<Color> colors = [
Colors.red,
Colors.blue,
Colors.cyan,
Colors.green,
Colors.yellow,
];
Widget gridPost(
//添加你需要的其他属性
int index,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: colors[index]
)),}
像这样调用方法:
return gridPost(snapshot.data!.docs[index]['index'])
英文:
Try to create a method like this :
List<Color> colors = [
Colors.red,
Colors.blue,
Colors.cyan,
Colors.green,
Colors.yellow,
];
Widget gridPost(
//add other properties that you need
int index,
) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: colors[index]
)),}
call method like this :
return gridPost(snapshot.data!.docs[index]['index'])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论