【问题标题】:Define Number of lists according with Number into Widget flutter根据 Number 定义列表的数量到 Widget Flutter
【发布时间】:2020-06-30 11:30:33
【问题描述】:

我所拥有的是已收到的订单数量,这些订单已根据此代码显示在一个选项卡中的小部件中:

class ShipmentTab extends StatefulWidget {
  @override
  _ShipmentTabState createState() => _ShipmentTabState();
}

class _ShipmentTabState extends State<ShipmentTab> {
  **final shipmentNumber = "16";**

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Card(
            color: Color(0xfffeaf0d),
            child: Container(
              width: 40,
              height: 40,
              child: Icon(
                Icons.local_shipping,
                color: Colors.white,
              ),
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(13),
          child: Text(
            shipmentNumber,
            style: TextStyle(
                color: Colors.white, fontSize: 35, fontWeight: FontWeight.bold),
          ),
        ),
      ],
    );
  }
}

如您所见,变量是“shipmentNumber”,显示“16” 下面我有listview.builder,我需要添加itemCount,这个计数必须参考“16”上方的选项卡 这是列表的代码:

Container(
          height: 400,
          child: ListView.builder(
            shrinkWrap: true,
            itemBuilder: (ctx, int) {
              return Card(
                color: Color(0xFF1f2032),
                elevation: 15,
                child: Container(
                  width: 60,
                  height: 60,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Card(
                        color: Color(0xfffeaf0d),
                        child: Container(
                            height: 40,
                            width: 40,
                            child: Icon(
                              Icons.local_shipping,
                              color: Colors.white,
                              size: 25,
                            )),
                      ),
                      Text(
                        'Ref № $int',
                        style: TextStyle(
                            color: Colors.white, fontWeight: FontWeight.bold),
                      ),
                      Text(
                        'Mario Rossi',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Color(0xfffeaf0d),
                        ),
                      ),
                      Text(
                        'Consegnato',
                        style: TextStyle(color: Colors.lightGreenAccent),
                      ),
                      Icon(
                        Icons.share,
                        color: Colors.white,
                      )
                    ],
                  ),
                ),
              );
            },
          ),
        ),
      ],
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    ListView.builderitemCount 参数。我注意到shippingNumber 在字符串中,因此您可以将其解析为int。您还需要删除 Container height 包裹您的 ListView

    ListView.builder(
            shrinkWrap: true,
            itemCount: int.parse(shipmentNumber),
            itemBuilder: (BuildContext context, int index) {
              /** build your Item here **/
             return BuiltItem();
            },
        )
    

    【讨论】:

    • 好的,非常感谢您的回答。假设我需要检索这个要在列表中显示的项目数量,但是这个数字必须检索到另一个小部件中,shipmentNumber 是否也在另一个小部件中,我如何从另一个小部件中检索这个 shippingNumber 并显示到 listbuilder 中?
    • 您可以尝试使用 ChangeNotifier 和 Provider Package flutter.dev/docs/development/data-and-backend/state-mgmt/simple 实现状态管理来监听特定值
    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多