我的代碼包含固定的小部件//1和//4內部Column,我想在它之間生成小部件串列。最初,我手動對小部件進行編碼(請參閱//2)。但是,由于我將有一個我想要生成的小部件串列BulletList,SurahCard我想使用for回圈或List.generate(參考//3)。
但是,我收到此錯誤:
Element 'List<Column>' can't be assigned to the list type '<Widget>'
感謝是否有人可以闡明如何解決這個問題?
import ...
class ReciteSurah extends StatelessWidget {
static const String id = 'recite_surah_screen';
int pageNumber;
int surahNumber;
late Surah surah;
late String surahName;
List<int> surahNumberList;
ReciteSurah({required this.surahNumberList, required this.surahNumber, required this.pageNumber}){
surah = SurahHelper().getSurah(surahNumber: surahNumber);
surahName = surah.nameMS;
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 96.0),
child: Column(
children: [
//1 - This widget is fixed
H2(text: '$pageNumber. BACA SURAH AL-FATIHAH & SURAH LAIN ATAU BEBERAPA AYAT LAIN'),
//2 - Manually calling the BulletList & SurahCard Widget
BulletList(lines:const [
'Baca surah al-Fatihah:',
] ),
SurahCard(surahNumber: 1),
BulletList(lines: [
'Baca surah $surahName:',
] ),
SurahCard(surahNumber: surahNumber),
//3 - Programatically generate List of BulletList & SurahCard Widget
List.generate(surahList.length, (index){
int curSurahNumber = surahNumberList[index];
Surah curSurah = SurahHelper().getSurah(surahNumber: curSurahNumber);
return Column(
children: [
BulletList(lines: [
'Baca surah ${curSurah.nameMS}:',
] ),
SurahCard(surahNumber: curSurahNumber),
],
);
},
),
//4 - This widget is fixed
P(text: 'Anda juga boleh membaca surah pilihan sendiri.'),
],
),
),
);
}
}
這是現有代碼的樣子,//1 //2并且//4:


uj5u.com熱心網友回復:
您可以使用展開... 運算子將串列添加widgets到您的列中。
...List.generate(
surahList.length,
(index) {
int curSurahNumber = surahNumberList[index];
Surah curSurah =
SurahHelper().getSurah(surahNumber: curSurahNumber);
return Column(
children: [
BulletList(lines: [
'Baca surah ${curSurah.nameMS}:',
] ),
SurahCard(surahNumber: ${curSurahNumber}),
],
);
},
),
擴展運算子用于在集合中插入多個元素,在您的情況下,它是您想要在其中添加一個
Column的子項(即) 。List<Widget>List<Widget>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/445767.html
