flutter - 在颜色属性中动态添加字符串值
问题描述
我想知道如何可以完成以下情况?
我有以下代码:
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)
),
),
),
);
}),
),
),
),
),
),
),
)
);
}
我想要实现的是将 的值动态放入arrColorList
color 属性中,如下所示:
Colors.arrColorList[index] // Should end up in Colors.blue or Colors.red
但这是无效的。知道我该如何解决这个问题吗?
解决方案
没有为“颜色”类型定义吸气剂“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]),
),
),
);
}),
),
),
),
),
),
),
))
推荐阅读
- python - 在 seaborn 散点图中分配不同的标记、大小、边界和不透明度
- python - 0-1 背包如何具有数学上的指数时间复杂度?
- reactjs - 将拦截器添加到 axios 的实例
- terraform - Terraform - 任何值的变量类型是什么
- or-tools - 如何通过重新加载节点的析取来限制 VehicleVar 的域?
- r - 如何解决默认情况下缺少参数 K 错误
- c# - 从坐标列表中查找左下角坐标
- javascript - 如何从 java 脚本函数中调用 spring 服务(RequestParam)(错误 400:必需的字符串参数 'flag' 不存在)
- javascript - 将对象数组减少为唯一键并添加该键共有的值数组
- javascript - 从对象计算状态并显示结果