将地图列表转换为颤动中的步进器?

问题描述 投票:0回答:1

我正在使用flutter创建一个简单的待办事项列表应用程序。我想从地图列表中创建一个Stepper小部件,如下所示。

[
      { 
        'task': 'Cook for 5 min',
        'content': '30',
      },
      {
        'task': 'Stir gently',
        'content': '20',
      },
]

目前我在构建器函数中执行此操作:

List<Step> todoSteps = args['todos'].map<Step>((todo) {
      return Step(
        title: Text(todo['task']),
        content: Text(todo['content']),
        isActive: _currentStep >= i, // this is the issue
      );
    }).toList();

然后我继续使用todoSteps设置Stepper小部件的steps参数。

除了isActive参数之外,这大部分工作正常,我必须检查_currentStep是否小于index(i)。但问题是当我无法在功能中获得索引时。

我尝试在列表上使用asMap()函数然后读取索引,但它总是导致错误。

dart flutter material
1个回答
0
投票

.asMap().entries应该提供您所需要的:

List<Step> todoSteps = args['todos'].asMap().entries.map<Step>((e) {
    var i = e.key;
    var todo = e.value;
    return Step(
      title: Text(todo['task']),
      content: Text(todo['content']),
      isActive: _currentStep >= i, // this is the issue
    );
  }).toList();
© www.soinside.com 2019 - 2024. All rights reserved.