首页 > 解决方案 > 在颜色属性中动态添加字符串值

问题描述

我想知道如何可以完成以下情况?

我有以下代码:

List<String> arrColorList = ['blue', 'red', 'yellow'];

    return Scaffold(
        backgroundColor: Colors.white,
        body: Container(
            child: Center(
                child: Container(
                    child: Center(
                      child: SingleChildScrollView(
                        child: Container(
                          child: Padding(
                            padding: const EdgeInsets.all(36.0),
                            child: ListView.builder(
                                scrollDirection: Axis.vertical,
                                shrinkWrap: true,
                                itemCount: arrColorList.length,
                                itemBuilder: (BuildContext context, int index) {
                                  return new GestureDetector(
                                    onTap: () {},
                                    child: SizedBox(
                                      width: 42.0,
                                      height: 42.0,
                                      child: const DecoratedBox(
                                        decoration: const BoxDecoration(
                                          color: const Color.fromRGBO(66, 165, 245, 1.0)
                                        ),
                                      ),
                                    ),
                                  );
                                }),
                          ),
                        ),
                      ),
                    ),
                ),
            ),
        )
        );
  }

我想要实现的是将 的值动态放入arrColorListcolor 属性中,如下所示:

Colors.arrColorList[index] // Should end up in Colors.blue or Colors.red

但这是无效的。知道我该如何解决这个问题吗?

标签: flutterdart

解决方案


没有为“颜色”类型定义吸气剂“arrColorList”。

将颜色列表类型字符串更改为颜色

List<Color> arrColorList = [Colors.blue, Colors.red, Colors.yellow];

完整的源代码

Scaffold(
        backgroundColor: Colors.white,
        body: Container(
          child: Center(
            child: Container(
              child: Center(
                child: SingleChildScrollView(
                  child: Container(
                    child: Padding(
                      padding: const EdgeInsets.all(36.0),
                      child: ListView.builder(
                          scrollDirection: Axis.vertical,
                          shrinkWrap: true,
                          itemCount: arrColorList.length,
                          itemBuilder: (BuildContext context, int index) {
                            return new GestureDetector(
                              onTap: () {},
                              child: SizedBox(
                                width: 42.0,
                                height: 42.0,
                                child:  DecoratedBox(
                                  decoration:  BoxDecoration(
                                      color: arrColorList[index]),
                                ),
                              ),
                            );
                          }),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ))

推荐阅读