gpt4 book ai didi

dart - 如何获得访问作用域模型模型

转载 作者:IT王子 更新时间:2023-10-29 06:57:19 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

只是不要忘记 itemBuilder 中的 return
希望对您有所帮助。

关于dart - 如何获得访问作用域模型模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55800265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com