You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Navigator.push页面跳转失败:Hero标签重复致应用冻结

修复「多个Hero标签重复」导致的页面跳转异常

方案1:给每个FloatingActionButton指定唯一Hero标签

直接为每个FAB的heroTag参数设置独一无二的标识,覆盖默认的重复标签,避免动画冲突。

示例代码:

// 主页中的多个FAB配置
FloatingActionButton(
  heroTag: 'fab_home_add', // 唯一标签
  onPressed: () {},
  child: Icon(Icons.add),
),
FloatingActionButton(
  heroTag: 'fab_home_share', // 另一唯一标签
  onPressed: () {},
  child: Icon(Icons.share),
),

方案2:禁用FAB的Hero动画

如果不需要Hero过渡动画,直接将heroTag设为null,Flutter就不会为该FAB创建Hero组件,从根源避免标签冲突。

示例代码:

FloatingActionButton(
  heroTag: null, // 禁用Hero动画
  onPressed: () {},
  child: Icon(Icons.add),
),

额外注意事项

  • 页面跳转无需包裹setState,直接调用Navigator.push即可,示例:
AppBar(
  actions: [
    IconButton(
      icon: Icon(Icons.settings),
      onPressed: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => SettingsPage()),
        );
      },
    ),
  ],
)
  • 检查所有页面中带Hero动画的组件,确保跨页面的Hero标签也保持唯一。

内容的提问来源于stack exchange,提问作者Mohamed Elsayed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 04:25:53