【问题标题】:Flutter iterate through list of buttons with map颤振遍历带有地图的按钮列表
【发布时间】:2019-12-12 03:26:35
【问题描述】:

我想遍历一个非常相似的按钮列表,但我不确定最好的方法是什么,因为每个按钮都需要一个传递单个参数的 onPressed 函数。

      Column(
        children: buttons
            .map(
              (item) => Row(
                children: <Widget>[
                  myButton(item[0], item[2]),
                  myButton(item[1], item[2]),
                ],
              ),
            )
            .toList(),
      ),

class MyButton extends StatelessWidget {
  MyButton(this.abool, this.onPressed);

  final bool abool;
  final Function onPressed;

  @override
  Widget build(BuildContext context) {
      return RawMaterialButton(
        child:
            Image.asset('images/button.png'),
        onPressed: onPressed,
      );

当然,我在将不同的onPressed 函数存储在List 数组中时会遇到问题。在列中遍历类似按钮列表的最佳方法是什么?

【问题讨论】:

  • 您目前在buttons 变量中存储了什么?
  • @Marcel 我想存储 onPressed 函数。
  • 啊,我明白了。 myButton 的论据是什么?标签和onPressed 回调?按钮上的标签应该是什么?
  • @Marcel 我已经用按钮功能更新了问题。 onPressed 应该是类似(val) =&gt; aFunction(val) 的函数。第一项就像文本、布尔值或其他。
  • @Marcel 也许有更好的方法来做到这一点?我的意思是为 onPressed 生成一个具有不同功能的类似按钮列表?

标签: flutter dart


【解决方案1】:

从 Dart 2.3 开始,您可以使用列表解析(也称为 UI-as-code)在列表内动态生成按钮。

List<String> labels = ['apple', 'banana', 'pineapple', 'kiwi'];
List<VoidCallback> actions = [_buyApple, _doSomething, _downloadData, () => print('Hi')];

...

Column(
  children: [
    for (int i = 0; i < labels.length; i++)
      RaisedButton(
        label: Text(labels[i]),
        onPressed: actions[i],
      ),
  ],
)

【讨论】:

  • 看不到每个按钮的_buyFruit的函数是怎么生成的?
  • 由于问题中的解释,我需要为每个按钮提供不同的 onPressed 函数。如果函数都相同,我就不会有将它们存储在 List 数组中的问题。
  • 我编辑了答案以反映这一点。这种方法对你有用吗?如果按钮看起来都一样,你甚至不需要labels 列表
【解决方案2】:

如果按钮不具有相似的行为,您不应该创建 Mybutton 类。 你不能为注册按钮和登录按钮创建一个类,每个类都有不同的责任和行为。

当你有数据模型(shopItem)时应该使用这个类(从你的代码我认为是这种情况),并且你需要(添加到购物车)功能,这是你有的情况制作 addToCartButton 并成为这样的东西:

class AddToCartButton extends StatefulWidget {
  final ShoptItem item;
  AddToCartButton({Key key, this.item}) : super(key: key);
  @override
  _AddToCartButtonState createState() => _AddToCartButtonState();
}

class _AddToCartButtonState extends State<AddToCartButton> {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text('Add to Cart'),
      onPressed: () {
        setState(() {
            cartList.add(item);
        });
      },
    );
  }
}

请注意,如果按钮本身存在任何类型的状态,例如

Text(inCart? 'already in cart':'add to cart'),'

除非你使用像 provider , bloc 等状态管理。

这里的第二个通知(最重要的)我使用了 cartList,它基本上是 ShopItem 的列表。

现在所有按钮都有类似的功能(将商品添加到购物车),但每个按钮都应该添加不同的商品,这可以在您正在使用的列或我更喜欢的 ListView 构建器中完成

列方法:

Column(
  children: <Widget>[
    for( item in itemsList)
      AddToCartButton(item : item);

  ],

ListViewBuilder 方法:

ListView.builder(
  itemCount: itemsList.length,
  itemBuilder: (context,index){
    return AddToCartButton(item : itemsList[index]);
  },
)

希望能帮到你

【讨论】:

  • 这与其他答案相同。函数需要不同,正如问题中所述,它们不是具有不同参数的同一个函数,这很容易做到。
  • 正如我所说,如果按钮不具有相似的行为,您应该创建 myButton 类,如果我缺少任何东西,请解释
猜你喜欢
  • 1970-01-01
  • 2020-06-21
  • 2022-01-23
  • 2021-06-06
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
相关资源
最近更新 更多