长笛-英雄小工具-2个英雄用相同的钥匙



假设我有一个ProductCard((小部件和ProductDetails((小程序。在我的主屏幕上,我想显示当天的交易和本周的交易。当我用Hero((小部件包装ProductCard((时,我将product.id作为密钥。现在,当同一产品同时属于两个类别时,就会出现问题:当天的交易和本周的交易,因为存在密钥冲突。这种情况下的解决方案是什么,或者Hero((不能在这种情况下使用?

假设第一个屏幕是MainScreen,第二个屏幕是DetailScreen

class MainScreen extends StatelessWidget {
String type = 'weekly/daily chose one';
..rest code...
child: Hero(
tag: 'imageHero$type', //assign the key including type weekly or daily etc
child: .. content widget ...
),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return DetailScreen(type:type); //pass the type as parameter to constructor
}));
},
...rest code...

在详细信息屏幕上,使用按键内的类型作为

class DetailScreen extends StatelessWidget {
... rest code ...
child: Hero(
tag: 'imageHero${widget.type}', // user the passed type here as included in the key
child: ... your widgets ...
),
),
onTap: () {
Navigator.pop(context);
},
... rest code ...

编辑:我认为您已经实现了产品.id作为关键任务

最新更新