英文:
Using for loop to generate table row in flutter
问题
请检查下面的代码,我做错了什么,我试图使用for循环生成一个表行的列表。
但是,我得到的结果只返回列表中的最后一个项目并停止,但是当我使用打印时,它显示正在生成项目,就像它应该的那样。
class CustomTableRow extends StatelessWidget {
const CustomTableRow({super.key});
@override
Widget build(BuildContext context) {
return Table(
border: TableBorder(
top: containerBorderSide,
bottom: containerBorderSide,
horizontalInside: containerBorderSide),
children: [
returnRestoreWord(),
],
);
}
}
BorderSide containerBorderSide = BorderSide(
color: Colors.grey.shade300,
width: 2,
);
List<String> restoreWords = [
'Abandon',
'Ability',
'Divorce',
'Leopard',
'Scale',
'Lens',
'Music'
];
TableRow returnRestoreWord() {
String words = '';
for (String word in restoreWords) {
words = word; // 这里应该使用 '+=' 而不是 '='
}
return TableRow(children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
words,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.w500,
),
),
)
]);
}
请注意,在returnRestoreWord
函数中,你应该使用 +=
而不是 =
,以便将每个单词连接在一起,而不是覆盖变量 words
。这样,你将生成一个包含所有单词的表行,而不仅仅是最后一个单词。
英文:
Please review my code below what am i doing wrong am trying to generate a list of table row using for loop.
And the result am getting return just the last item in my list and stop, but i when i use a print it shows its generating the items as it surpose to.
class CustomTableRow extends StatelessWidget {
const CustomTableRow({super.key});
@override
Widget build(BuildContext context) {
return Table(
border: TableBorder(
top: containerBorderSide,
bottom: containerBorderSide,
horizontalInside: containerBorderSide),
children: [
returnRestoreWord(),
],
);
}
}
BorderSide containerBorderSide = BorderSide(
color: Colors.grey.shade300,
width: 2,
);
List<String> restoreWords = [
'Abandon',
'Ability',
'Divorce',
'Leopard',
'Scale',
'Lens',
'Music'
];
TableRow returnRestoreWord() {
String words = '';
for (String word in restoreWords) {
words = word;
}
return TableRow(children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
words,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.w500,
),
),
)
]);
}
Please review my code below what am i doing wrong am trying to generate a list of table row using for loop.
And the result am getting return just the last item in my list and stop, but i when i use a print it shows its generating the items as it surpose to.
答案1
得分: 1
我认为您可能正在寻找类似于以下内容的东西:
class CustomTableRow extends StatelessWidget {
const CustomTableRow({super.key});
@override
Widget build(BuildContext context) {
return Table(
border: TableBorder(
top: containerBorderSide,
bottom: containerBorderSide,
horizontalInside: containerBorderSide),
children: [
for ( final word in restoreWords )
TableRow(children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
word,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.w500,
),
),
)
])
],
);
}
}
BorderSide containerBorderSide = BorderSide(
color: Colors.grey.shade300,
width: 2,
);
List<String> restoreWords = [
'Abandon',
'Ability',
'Divorce',
'Leopard',
'Scale',
'Lens',
'Music'
];
英文:
I think you might be looking for something like this:
class CustomTableRow extends StatelessWidget {
const CustomTableRow({super.key});
@override
Widget build(BuildContext context) {
return Table(
border: TableBorder(
top: containerBorderSide,
bottom: containerBorderSide,
horizontalInside: containerBorderSide),
children: [
for ( final word in restoreWords )
TableRow(children: [
Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
word,
style: const TextStyle(
fontSize: 25,
fontWeight: FontWeight.w500,
),
),
)
])
],
);
}
}
BorderSide containerBorderSide = BorderSide(
color: Colors.grey.shade300,
width: 2,
);
List<String> restoreWords = [
'Abandon',
'Ability',
'Divorce',
'Leopard',
'Scale',
'Lens',
'Music'
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论