Dart等效于Python zip和列表解析,用于从两个列表生成小部件列表

ru9i0ody  于 2022-12-06  发布在  Python
关注(0)|答案(4)|浏览(137)

I want to create a list of widgets MyWidget(categoryName, color) from the following two lists.

static const _categoryNames = <String>[
    'Length',
    'Area',
    'Volume',
  ];

  static const _baseColors = <Color>[
    Colors.teal,
    Colors.orange,
    Colors.pinkAccent,
  ];

In Python I would use a list comprehension with zip to get the result.

my_widget_list = [MyWidget(categoryName, baseColor) for categoryName, baseColor in zip(_categoryNames, _baseColors)]

Googling for a similar method for Dart did not provide any satisfactory solution.

dwbf0jvd

dwbf0jvd1#

如果你的目标是创建一个小部件列表(假设你的两个列表有相同数量的元素),你可以尝试

List<Widget> getWidgets() {

  List<Widget> my_widget_list = [];
  const _categoryNames = <String>[
    'Length',
    'Area',
    'Volume',
  ];

 const _baseColors = <Color>[
    Colors.teal,
    Colors.orange,
    Colors.pinkAccent,
  ];

  for (int i = 0; i <= _categoryNames.length -1 ; i++){
    my_widget_list.add(MyWidget(_categoryNames[i],_baseColors[i]));
  }

  return my_widget_list;
  }

Widget MyWidget(String categoryName, Color baseColor){
  return Container(
    color: baseColor,
    child: Text(categoryName,));
}
fjaof16o

fjaof16o2#

有一个zip function from package:quiver,结合 collection-for(Dart相当于Python的列表解析)可以让你大致了解它,Dart没有Python的自动解包功能,所以很不幸你不会得到很好的变量名:

my_widget_list = [
  for (var pair in zip([_categoryNames, _baseColors]))
    MyWidget(pair[0], pair[1]),
]
sshcrbum

sshcrbum3#

“Python” dart 语法

你可以用Control Flow Collections做一些看起来非常类似于Python的事情。

List<Widget> myWidgetList = [
      for (String name in categoryNames)
        for (Color color in baseColors)
          if (baseColors.indexOf(color) == categoryNames.indexOf(name))
            myWidget(name, color)
    ];

也许少一点“Python”,但更简洁。

List<Widget> myWidgetList = [
      for (String name in categoryNames)
        myWidget(name, baseColors[categoryNames.indexOf(name)])
    ];

具体来说,您应该通读控制流集合:撰写

完成单元测试

为了方便起见,这里有一个可以运行的快速单元测试。

import 'dart:core';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('build list of widgets with composing', () {
    List<Widget> myWidgetList = [
      for (String name in categoryNames)
        for (Color color in baseColors)
          if (baseColors.indexOf(color) == categoryNames.indexOf(name))
            myWidget(name, color)
    ];

    List<Widget> myOtherWidgetList = [
      for (String name in categoryNames)
        myWidget(name, baseColors[categoryNames.indexOf(name)])
    ];

    expect(myWidgetList.length, equals(myOtherWidgetList.length));
    expect(myWidgetList.toString(), equals(myOtherWidgetList.toString()));
    print(myWidgetList);
  });
}

const categoryNames = <String>[
  'Length',
  'Area',
  'Volume',
];
const baseColors = <Color>[
  Colors.teal,
  Colors.orange,
  Colors.pinkAccent,
];

Widget myWidget(String categoryName, Color baseColor) {
  return Container(
      color: baseColor,
      child: Text(
        categoryName,
      ));
}

相关问题