使用for循环在Flutter中生成表格行。

huangapple go评论79阅读模式
英文:

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&lt;String&gt; restoreWords = [
  &#39;Abandon&#39;,
  &#39;Ability&#39;,
  &#39;Divorce&#39;,
  &#39;Leopard&#39;,
  &#39;Scale&#39;,
  &#39;Lens&#39;,
  &#39;Music&#39;
];

TableRow returnRestoreWord() {
  String words = &#39;&#39;;
  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&lt;String&gt; restoreWords = [
  &#39;Abandon&#39;,
  &#39;Ability&#39;,
  &#39;Divorce&#39;,
  &#39;Leopard&#39;,
  &#39;Scale&#39;,
  &#39;Lens&#39;,
  &#39;Music&#39;
];

huangapple
  • 本文由 发表于 2023年8月4日 01:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定