从Firebase获取的GridView项上的不同颜色?

huangapple go评论59阅读模式
英文:

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 从Firebase获取的GridView项上的不同颜色?

This is more or less what I'm after

从Firebase获取的GridView项上的不同颜色?

答案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&lt;Color&gt; 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][&#39;index&#39;])

huangapple
  • 本文由 发表于 2023年2月14日 18:54:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定