我想知道如何做到以下情況?
我有以下代碼:
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
但這是無效的。知道我如何解決這個問題嗎?
uj5u.com熱心網友回復:
getter 'arrColorList' 不是為型別 'Colors' 定義的。
將顏色串列型別字串更改為顏色
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]),
),
),
);
}),
),
),
),
),
),
),
))
uj5u.com熱心網友回復:
創建一個顏色串列“串列”并用你想要的顏色填充它是很好的。
在你的情況下,你可以這樣做:
List<Color> arrColorList = [
Colors.blue,
Colors.red,
Colors.yellow,
];
然后像這樣使用它:
arrColorList[index]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342567.html
