我在考慮如何將串列添加到函式然后通過 ListView.builder 添加,我經常嘗試 7,但應用程式給我錯誤enter image description here我不知道如何但我搜索這個主題我已閱讀檔案輸入鏈接描述在這里,我在本檔案中嘗試這種方式并嘗試更改本檔案中的某些行
import 'package:flutter/material.dart';
class ColorsPage2 extends StatefulWidget {
const ColorsPage2({Key? key}) : super(key: key);
@override
State<ColorsPage2> createState() => _ColorsPage2State();
}
class _ColorsPage2State extends State<ColorsPage2> {
final List<String> entries = <String>[
'Red',
'Pink',
'Purple',
'DeepPurple',
'Indigo',
'Blue',
'LightBlue',
'Cyan',
'Teal',
'Green',
'LightGreen',
'Lime',
'Yellow',
'Amber',
'Orange',
'DeepOrange',
'Brown',
'Grey',
'BlueGrey',
];
List<MaterialColor> colorCodes = <MaterialColor>[
Colors.red,
Colors.pink,
Colors.purple,
Colors.deepPurple,
Colors.indigo,
Colors.blue,
Colors.lightBlue,
Colors.cyan,
Colors.teal,
Colors.green,
Colors.lightGreen,
Colors.lime,
Colors.yellow,
Colors.orange,
Colors.deepOrange,
Colors.brown,
Colors.grey,
Colors.blueGrey,
];
Widget cardColor({MaterialColor? color, String? title, String? hexColor}) {
return InkWell(
child: Card(
color: colorCodes[colorCodes.length],
child: ListTile(
textColor: Colors.white,
title: Text(entries[entries.length]),
subtitle: SelectableText(
'This Is ${entries.length} Color , Hex: #$hexColor'),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index) {
return cardColor(
color: colorCodes[colorCodes.length],
title: entries[entries.length],
hexColor: 'F44336FF',
);
},
),
);
}
}
uj5u.com熱心網友回復:
您應該使用索引,而是使用長度;
嘗試這個:
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index) {
return cardColor(
color: colorCodes[index],
title: entries[index],
hexColor: 'F44336FF',
);
},
),
);
}
uj5u.com熱心網友回復:
color: colorCodes[colorCodes.length],
title: entries[entries.length],
length 屬性為您提供從 1 開始的串列大小。您嘗試訪問 colorCodes[colorCodes.length] 并且將無法正常作業,因為串列從 0 開始,因此您需要執行 colorCodes[colorCodes.length - 1] 例如。
uj5u.com熱心網友回復:
首先,確保條目列??表中的資料長度等于顏色代碼。
到目前為止,您只有 18 個顏色代碼資料和 19 個條目資料。在確保兩者具有相同的長度后,試試這個:
Widget cardColor({MaterialColor? color, String? title, String? hexColor}) {
return InkWell(
child: Card(
color: color,
child: ListTile(
textColor: Colors.white,
title: Text(title),
subtitle: SelectableText(
'This Is $title Color , Hex: #$hexColor'),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index) {
return cardColor(
color: colorCodes[index],
title: entries[index],
hexColor: 'F44336FF',
);
},
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/495493.html
上一篇:Flutter中的多型別串列視圖
