如何使每个网格具有不同的颜色

how to make each grid with different color

我正在尝试使每个网格具有不同的颜色值来自控制器(来自 api)

我无法到达每个网格,因为使用地图不会为每个网格提供索引。

是否有另一种方式来查看这些网格或另一种方式来为每个网格涂上不同的颜色?

这是代码:

                        GridView.count(
                            padding: EdgeInsets.only(top: 0),
                            physics: BouncingScrollPhysics(),
                            childAspectRatio: getProportionateScreenWidth(159) /
                                getProportionateScreenWidth(115),
                            crossAxisCount: 2,
                            crossAxisSpacing: getProportionateScreenWidth(28.5),
                            mainAxisSpacing: getProportionateScreenWidth(14.3),
                            children: controller.letters.map(
                              (e) {
                                counter++;
                                return Material(
                                  elevation: 3,
                                  borderRadius: BorderRadius.circular(
                                      getProportionateScreenWidth(33)),
                                  child: Container(
                                    height: getProportionateScreenWidth(115),
                                    width: getProportionateScreenWidth(159),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(
                                          getProportionateScreenWidth(33)),
                                    ),
                                    child: Center(
                                        child: Text(
                                      e,
                                      style: TextStyle(
                                          fontSize:
                                              getProportionateScreenWidth(54),
                                          fontWeight: FontWeight.bold,
                                          color: counter % 2 == 0
                                              ? Color(0xff6FC989):
                                             Color(0xff00A49A)
                                      )
                                    )),
                                  ),
                                );
                              },
                            ).toList(),
                          ),

我想看的是这样的

之前生成 ColorList 然后稍微修改你的代码(将你的 controller.letters 转换为 Map:

children: controller.letters.asMap().entries.map((entry) {
  return Material(
    child: Contaner(
      child: Center(
        child: Text(entry.value), // value contains you `letter`.
        style: TextStyle(
          color: _generatedColors[entry.key], // key containes index starting from 0.
        ),    
      ),
    ),
  );  
}).toList();