【问题标题】:Grid(Row*Column) using nested for loop使用嵌套for循环的网格(行*列)
【发布时间】:2022-01-18 14:20:58
【问题描述】:

我是一个初学者,实际上是整个软件开发领域,

我想创建一个 4x2 网格,特别是带有 for 循环,

下面的代码旨在在 4 行和 2 列的网格中创建 8 个框,在 j==2 处更改为不同的行,但它没有发生,它继续在第一个构建容器(并且仅) 行,即使在达到j < 2 的条件后,

另外,我可以知道如何为for 创建代码块,就像在其他语言中所做的那样,如for(.) {.},我已经使用for(.) ...[.] 做了类似的事情,但是它只是一个愚蠢的复制粘贴off,再次from

要试用代码,visit:

import 'package:flutter/material.dart';
void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: Row(
          children: <Widget>[
            for (int i = 0; i < 4; i++) ...[
              Row(
                children: [
                  for (int j = 0; j < 2; j++) ...[
                    Column(
                      children: [
                        Container(
                          child: Row(
                            children: [
                              Column(
                                children: [
                                  Text(i.toString()),
                                  Text(j.toString()),
                                ],
                              ),
                              const Icon(Icons.keyboard_arrow_right_sharp),
                            ],
                          ),
                          margin: const EdgeInsets.all(5.0),
                          padding: const EdgeInsets.all(5.0),
                          decoration: BoxDecoration(
                            border: Border.all(
                              color: Colors.black,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ]
                ],
              ),
            ]
          ],
        ),
      ),
    ),
  );
}

谢谢你...

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我认为这是问题所在,您将创建的 Rows 放在另一个 Row 中。我想你想要的是Column:

    home: Scaffold(
        body: Column(
          children: <Widget>[
            for (int i = 0; i < 4; i++) ...[
              Row(
    ....
    

    【讨论】:

      猜你喜欢
      • 2021-01-12
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2014-09-01
      • 2013-06-05
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      相关资源
      最近更新 更多