You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter开发:从商品网格页返回时如何传递选中对象?

Flutter 从商品网格页返回时带回选中商品的解决方案
  • 销售页面跳转时接收返回值
    点击「添加商品」按钮时,用async/await配合Navigator.push获取网格页返回的商品数据,拿到后更新销售页的商品列表:

    // 销售页面添加商品按钮的点击逻辑
    onTap: () async {
      // 跳转到商品网格页,等待返回选中的商品
      final selectedItems = await Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => ProductGridPage()),
      );
    
      // 若有返回数据,更新当前销售页面的商品列表
      if (selectedItems != null) {
        setState(() {
          // 这里根据需求处理,比如追加到现有列表
          currentSaleItems.addAll(selectedItems);
        });
      }
    },
    
  • 商品网格页返回时传递选中商品
    修改你现有的onTap代码,在调用pop时把选中的商品数据传回去:

    • 单选场景:
      onTap: () => isSelecting ? Navigator.of(context).pop(currentSelectedProduct) : null,
      
    • 多选场景(比如有确认按钮):
      // 假设multiSelectedProducts是已选中的商品列表
      onTap: () {
        if (isSelecting) {
          Navigator.of(context).pop(multiSelectedProducts);
        }
      },
      
  • 注意事项

    • 确保传递的商品数据是可序列化的(比如自定义模型类要支持序列化,或者用基础数据类型)
    • 销售页面要判断返回值不为空再处理,避免空指针问题

内容的提问来源于stack exchange,提问作者Arsénio Muanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 00:56:09