Flutter 循环以生成包含两列颤振的列表

Flutter 循环以生成包含两列颤振的列表,flutter,dart,visual-studio-code,Flutter,Dart,Visual Studio Code,我是Flatter的初学者,我需要创建一个循环并生成一个小部件,其中包含只有两列的行。 我尝试了太多不同的方法,但都不管用:// 当我将循环放入我的代码时,例如(var I=0;I打印('hii2'), ), ),); } 退货清单; } @凌驾 小部件构建(构建上下文){ 返回脚手架( appBar:appBar( 背景颜色:颜色。透明, 标题:对, 标题:文本(“Votre SMARTY PARK”), ), 主体:容器( 装饰:盒子装饰( 图像:装饰图像( 图像:NetworkImage(

我是Flatter的初学者,我需要创建一个循环并生成一个小部件,其中包含只有两列的行。
我尝试了太多不同的方法,但都不管用://
当我将循环放入我的代码时,例如(var I=0;I。。屏幕上没有显示任何内容,屏幕上的所有内容都变成了白色

以下是我正在使用的代码:

class _MapPageState extends State<MapPage> {
 var p1= [
    "A0",
    "A1",
    "A2",
    "A3",
  ];
   List<Widget> getWidgets()
  {
    List<Widget> list = new List<Widget>();
    for(var i = 0; i < p1.length-1; i+2){
        list.add(new Container(
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius:BorderRadius.circular(20.0)
                          ),
                          child: FlatButton(
                            child: Text(p1[i].toString(),style: TextStyle(color: Colors.white),),
                               onPressed:()=>print('hii1'),
                        ),
                      ),);
        list.add(new Container(
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius:BorderRadius.circular(20.0)
                          ),
                          child: FlatButton(
                            child: Text(p1[i+1].toString(),style: TextStyle(color: Colors.white),),
                               onPressed:()=>print('hii2'),
                        ),
                      ),);
    }
    return list;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        centerTitle: true,
        title: Text("Votre SMARTY PARK"),
      ),
      body:Container(
        decoration: BoxDecoration(
                image: DecorationImage(
                image: NetworkImage('https://images.wallpaperscraft.com/image/parking_cars_underground_131454_240x320.jpg'),
                fit: BoxFit.cover,
              ),),
        child: ListView(
              scrollDirection: Axis.vertical,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 80),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: getWidgets()
                    ),
                ),
              ],
            ),
      )
      );
  }
}
class\u映射页面状态扩展状态{
变量p1=[
“A0”,
“A1”,
“A2”,
“A3”,
];
列出getWidgets()
{
列表=新列表();
对于(变量i=0;i打印('hii1'),
),
),);
列表。添加(新容器)(
装饰:盒子装饰(
颜色:颜色。绿色,
边界半径:边界半径。圆形(20.0)
),
孩子:扁平按钮(
子项:文本(p1[i+1].toString(),样式:TextStyle(颜色:Colors.white)),
按下时:()=>打印('hii2'),
),
),);
}
退货清单;
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:颜色。透明,
标题:对,
标题:文本(“Votre SMARTY PARK”),
),
主体:容器(
装饰:盒子装饰(
图像:装饰图像(
图像:NetworkImage('https://images.wallpaperscraft.com/image/parking_cars_underground_131454_240x320.jpg'),
适合:BoxFit.cover,
),),
子:ListView(
滚动方向:轴垂直,
儿童:[
填充物(
填充:仅限常量边集(顶部:80),
孩子:新的一排(
mainAxisAlignment:mainAxisAlignment.space,
子项:getWidgets()
),
),
],
),
)
);
}
}

希望任何人都能帮上忙,因为我已经试了三天了。


谢谢

不要将两个
容器
添加到
列表
,而是将包含两个
容器的
添加到列表中。
此外,您可能应该将
列表视图中的
更改为

请参阅以下代码:
(我只是从您的代码中复制粘贴了一些内容,有很多更改,请仔细检查。您必须进行更改,使其看起来更整洁,并具有正确的名称等。)

导入“包装:颤振/材料.省道”;
void main(){
runApp(MainApp());
}
类MainApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
背景颜色:颜色。透明,
标题:对,
标题:文本(“Votre SMARTY PARK”),
),
正文:MapPage(),
),
);
}
}
类MapPage扩展StatefulWidget{
@凌驾
状态createState(){
返回MapPageState();
}
}
类MapPageState扩展了状态{
变量p1=[“A0”、“A1”、“A2”、“A3”];
List-widgetlist=List();
MapPageState(){
getWidgets(widgetlist);
}
@凌驾
小部件构建(构建上下文){
double-scwidth=MediaQuery.of(context).size.width;
double scheight=MediaQuery.of(context).size.height;
返回列(
mainAxisSize:mainAxisSize.max,
儿童:[
容器(
高度:scheight-kToolbarHeight-24,
装饰:盒子装饰(
图像:装饰图像(
图片:NetworkImage(
'https://images.wallpaperscraft.com/image/parking_cars_underground_131454_240x320.jpg'),
适合:BoxFit.cover,
),
),
子:ListView(
子项:widgetlist.map((元素){
返回元素;
}).toList(),
),
)
],
);
}
void getWidgets(列表wlist){
对于(int i=0;i打印('hii1'),
),
),
容器(
装饰:盒子装饰(
颜色:颜色。绿色,边界半径:边界半径。圆形(20.0)),
孩子:扁平按钮(
子:文本(
p1[i+1].toString(),
样式:TextStyle(颜色:Colors.white),
),
按下时:()=>打印('hii2'),
),
)
],
));
}
}
}
输出:

不要在
列表中添加两个
容器
,而是在列表中添加一个包含两个
容器的

此外,您可能应该将
列表视图中的
更改为

请参阅以下代码:
(我只是从您的代码中复制粘贴了一些内容,有很多更改,所以请仔细检查。您必须进行更改才能进行更改。)
import 'package:flutter/material.dart';

void main() {
  runApp(MainApp());
}

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          centerTitle: true,
          title: Text("Votre SMARTY PARK"),
        ),
        body: MapPage(),
      ),
    );
  }
}

class MapPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MapPageState();
  }
}

class MapPageState extends State<MapPage> {
  var p1 = ["A0", "A1", "A2", "A3"];
  List<Widget> widgetlist = List();

  MapPageState() {
    getWidgets(widgetlist);
  }
  @override
  Widget build(BuildContext context) {
    double scwidth = MediaQuery.of(context).size.width;
    double scheight = MediaQuery.of(context).size.height;
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[
        Container(
          height: scheight - kToolbarHeight - 24,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: NetworkImage(
                  'https://images.wallpaperscraft.com/image/parking_cars_underground_131454_240x320.jpg'),
              fit: BoxFit.cover,
            ),
          ),
          child: ListView(
            children: widgetlist.map((element) {
              return element;
            }).toList(),
          ),
        )
      ],
    );
  }

  void getWidgets(List<Widget> wlist) {
    for (int i = 0; i < p1.length-1; i++) {
      wlist.add(Row(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.circular(20.0)),
            child: FlatButton(
              child: Text(
                p1[i].toString(),
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () => print('hii1'),
            ),
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.green, borderRadius: BorderRadius.circular(20.0)),
            child: FlatButton(
              child: Text(
                p1[i + 1].toString(),
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () => print('hii2'),
            ),
          )
        ],
      ));
    }
  }
}
List<Row> getWidgets()
  {
    List<Row> list = new List<Row>();
    for(var i = 0; i < p1.length-1; i+=2){
        list.add(Row(
            mainAxisAlignment:MainAxisAlignment.spaceAround,
            children:[
                Container(
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius:BorderRadius.circular(20.0)
                        ),
                        child: FlatButton(
                            child: Text(p1[i].toString(),
                                       style: TextStyle(
                                           color: Colors.white
                                       ),
                             ),
                             onPressed:()=>print('hii1'),
                        ),
                   ),
                   Container(
                        decoration: BoxDecoration(
                            color: Colors.green,
                            borderRadius:BorderRadius.circular(20.0)
                        ),
                        child: FlatButton(
                            child: Text(p1[i+1].toString(),
                                       style: TextStyle(
                                                  color: Colors.white
                                       ),
                             ),
                             onPressed:()=>print('hii2'),
                        ),
                   ),
            ]
        );
    );
}
    return list;
        ListView(
              scrollDirection: Axis.vertical,
              children:getWidgets()
            ),