【问题标题】:How to get access scoped-model models如何访问作用域模型模型
【发布时间】:2019-09-11 23:49:19
【问题描述】:

我正在尝试通过 _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 我当然可以访问,但它会使函数工作非常缓慢,因为它只构建了两次,我确信有更好的方法来做到这一点

【问题讨论】:

    标签: dart flutter scoped-model


    【解决方案1】:

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

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

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

    别忘了itemBuilder里面的return
    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2017-06-25
      相关资源
      最近更新 更多