假设我有一个ProductCard()小部件和ProductDetails()小部件。在我的主屏幕上,我想显示当天的交易和本周的交易。当我用Hero()小部件包装ProductCard()时,我给出了product.id作为键。现在,当同一产品同时出现在这两类产品中时,就会出现一个问题:一天的交易和一周的交易,因为钥匙之间有冲突。在这种情况下,什么是解决方案,或者Hero()不能在这种情况下使用?
发布于 2020-11-24 07:08:17
假设,第一个屏幕是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 ...编辑:--我假设您已经实现了product.id作为的关键任务
https://stackoverflow.com/questions/64981524
复制相似问题