1

我正在尝试在 _buildProductCard 小部件上访问我的模型:

Widget build(BuildContext context) {
    return ScopedModelDescendant<ConnectedProductModel>(
      builder:
          (BuildContext context, Widget child, ConnectedProductModel model) {
        return Scaffold(
          drawer: Drawer(),
          appBar: AppBar(),
          body: Column(
            children: <Widget>[
              Container(
                child: RaisedButton(
                  onPressed: () => model.addProduct('NaNa',
                      'https://cdn.pixabay.com/photo/2019/01/27/22/31/girl-3959203__340.jpg'),
                  child: Text('Add Product'),
                ),
              ),
              Expanded(
                child: ListView.builder(
                  //build the cards
                  itemBuilder: _buildProductCard,
                  //lengta of the list,
                  itemCount: model.product.length,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildProductCard(BuildContext context, int index) {
      return Card(
        child: Column(
          children: <Widget>[
            Image.network(model.product[index].image),
            Text(model.product[index].address),
            ButtonBar(
              alignment: MainAxisAlignment.center,
              children: <Widget>[
                IconButton(
                  icon: Icon(Icons.location_on, color: Colors.blue, size: 40.0),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ProductNavigation()),
                    );
                  },
                ),
                IconButton(
                  icon: Icon(Icons.delete, color: Colors.blue, size: 40.0),
                  onPressed: () => model.deleteProduct(index),
                ),
                IconButton(
                  icon: Icon(Icons.info, color: Colors.blue, size: 40.0),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => ProductInfoPage(
                            model.product[index].address,
                            model.product[index].image,
                            model.product[index].id),
                      ),
                    );
                  },
                ),
                IconButton(
                  icon:
                      Icon(Icons.contact_mail, color: Colors.blue, size: 40.0),
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (context) => FormPage()),
                    );
                  },
                )
              ],

如您所见,我用 ScopedModelDescendant 包裹了我的脚手架,但我无法在 _buildProductCard 方法中访问我的模型。如果我用 ScopedModelDescendant 包装 my_buildProductCard 我当然可以访问,但它使函数工作非常缓慢,因为它只构建了两次,我确信有更好的方法来做到这一点

4

1 回答 1

1

在您的项目生成器上,您可以执行以下操作:

itemBuilder: (BuildContext context, int index) {
                return _buildProductCard(context, index, model);
              }) 

在你的_buildProductCard添加一个额外的参数ConnectedProductModel model。所以签名将是Widget _buildProductCard(BuildContext context, int index, ConnectedProductModel model)

只是不要忘记itemBuilder中的返回。
希望这有帮助。

于 2019-04-22T20:32:19.390 回答