Flutter 颤振-英雄小部件-2英雄使用相同的键?
假设我有一个ProductCard()小部件和ProductDetails()小部件。 在我的主屏幕上,我想显示当天的交易和本周的交易。 当我用Hero()小部件包装ProductCard()时,我将product.id作为密钥。 现在,当同一个产品同时出现在两个类别中时,就会出现一个问题:当天的交易和每周的交易,因为存在密钥冲突。Flutter 颤振-英雄小部件-2英雄使用相同的键?,flutter,flutter-animation,Flutter,Flutter Animation,假设我有一个ProductCard()小部件和ProductDetails()小部件。 在我的主屏幕上,我想显示当天的交易和本周的交易。 当我用Hero()小部件包装ProductCard()时,我将product.id作为密钥。 现在,当同一个产品同时出现在两个类别中时,就会出现一个问题:当天的交易和每周的交易,因为存在密钥冲突。 这种情况下的解决方案是什么,或者在这种情况下不能使用Hero()?让我们假设,第一个屏幕是MainScreen,第二个屏幕是DetailScreen class M
这种情况下的解决方案是什么,或者在这种情况下不能使用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 ...
编辑:我假设您已经获得了product.id作为一个键分配您可以通过连接categoeryID来创建英雄id+productID@deepak它将和同一产品一样。同一产品属于同一类别吗?是的。。正如第一个答案中提到的,无论如何,我只需要创建唯一的标签。。