Flutter:构建一个 Cupertino Picker,显示包含 2 个小部件的行,通过循环遍历 Map

Flutter: Building a Cupertino Picker that shows rows containing 2 widgets, from looping through a Map

我创建了一个选择器,它显示一个图标和一个字符串,彼此相邻。选择器项目取自我创建的包含键:字符串和值:图标的地图。

现在在我的代码中,我正在使用 Cupertino Picker 并添加子项:[ Row(Icon + String)] 但是如果我想更新它们就很糟糕了。

我试图获得一个循环来生成这些行 + 子项,但我不知道如何做。

有人可以告诉我获得此结果的方法或更有效的方法吗?我正在考虑提取行并创建一个构造函数来输入图标和字符串,但我确信有更好的方法...

代码如下:

Expanded(
                          child: CupertinoPicker(
                            itemExtent: 40,
                            children: <Widget>[
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[0],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[0],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[1],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[1],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[2],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[2],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[3],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[3],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[4],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[4],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[5],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[5],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[6],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[6],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  BuildingProblem.problemListIcons[7],
                                  Padding(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      BuildingProblem.problemListNames[7],
                                      style:
                                          TextStyle(color: Colors.white70),
                                    ),
                                  )
                                ],
                              )
                            ],
                            onSelectedItemChanged: (int index) {
                              print('good boi');
                            },
                            looping: true,
                            backgroundColor: Color(0xff2e3032),
                          ),
                        ),

看起来如何:

您可以复制粘贴运行下面的完整代码
您可以使用 control-flow-collections

代码片段

children: <Widget>[
                  for (var i = 0;
                      i < BuildingProblem.problemListIcons.length;
                      i++)
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        BuildingProblem.problemListIcons[i],
                        Padding(
                          padding: EdgeInsets.only(left: 10),
                          child: Text(
                            BuildingProblem.problemListNames[i],
                            style: TextStyle(color: Colors.white70),
                          ),
                        )
                      ],
                    ),
                ],

工作演示

您需要将文件 analysis_options.yaml 添加到项目的根目录和以下行

analyzer:
  enable-experiment:
    - control-flow-collections

完整代码

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class BuildingProblem {
  static List<Icon> problemListIcons = [];
  static List<String> problemListNames = [];
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    BuildingProblem.problemListIcons.add(Icon(Icons.add));
    BuildingProblem.problemListIcons.add(Icon(Icons.cast));
    BuildingProblem.problemListNames.add("add");
    BuildingProblem.problemListNames.add("cast");
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: CupertinoPicker(
                itemExtent: 40,
                children: <Widget>[
                  for (var i = 0;
                      i < BuildingProblem.problemListIcons.length;
                      i++)
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        BuildingProblem.problemListIcons[i],
                        Padding(
                          padding: EdgeInsets.only(left: 10),
                          child: Text(
                            BuildingProblem.problemListNames[i],
                            style: TextStyle(color: Colors.white70),
                          ),
                        )
                      ],
                    ),
                ],
                onSelectedItemChanged: (int index) {
                  print('good boi');
                },
                looping: true,
                backgroundColor: Color(0xff2e3032),
              ),
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}