1

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

现在在我的代码中,我正在使用 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),
                          ),
                        ),

它的样子:

在此处输入图像描述

4

1 回答 1

1

您可以在下面复制粘贴运行完整代码
您可以使用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),
      ),
    );
  }
}
于 2019-12-30T08:03:39.963 回答