繁体   English   中英

Flutter - Hero 小部件 - 2 个具有相同键的英雄?

[英]Flutter - Hero widget - 2 hero with same key?

假设我有一个 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 ...

编辑:我假设您已经将product.id 作为关键分配

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM