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

flutter_speed_dial 7.0.0:SpeedDialChild图标点击后无法更新的问题求助

解决flutter_speed_dial 7.0.0中SpeedDialChild图标不更新的问题

问题根源在于flutter_speed_dial 7.x版本的内部逻辑:当SpeedDial菜单处于展开状态时,SpeedDialChild不会自动响应外部状态变化,因为组件未触发局部重建。结合你使用useState(HookWidget)的场景,通过ValueListenableBuilder即可实现不关闭菜单前提下的图标实时更新。

具体修改方案

  1. 确保你的组件是HookWidget(适配useState的使用场景)
  2. 用ValueListenableBuilder包裹Icon,监听showCriticalPoint的状态变化:
final showCriticalPoint = useState(false);

// ... 其他代码

SpeedDial(
  isOpenOnStart: true,
  closeManually: true,
  renderOverlay: false,
  closeDialOnPop: false,
  direction: SpeedDialDirection.down,
  backgroundColor: Colors.white,
  activeIcon: Icons.close,
  iconTheme: IconThemeData(color: themeData.colorScheme.secondary),
  icon: Icons.menu_outlined,
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(8),
  ),
  buttonSize: const Size(48, 48),
  childrenButtonSize: const Size(48, 48),
  spacing: 4,
  children: [
    SpeedDialChild(
      // 用ValueListenableBuilder包裹Icon实现局部更新
      child: ValueListenableBuilder<bool>(
        valueListenable: showCriticalPoint,
        builder: (context, isCritical, _) {
          return Icon(
            isCritical ? Icons.report_off_outlined : Icons.report_outlined,
            color: Colors.white,
          );
        },
      ),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
      ),
      backgroundColor: Colors.red,
      onTap: () {
        showCriticalPoint.value = !showCriticalPoint.value;
      },
    ),
  ],
)

原理说明

useState返回的是ValueNotifier类型对象,ValueListenableBuilder可以监听其状态变化。当showCriticalPoint.value切换时,只会重建内部的Icon组件,不会触发整个SpeedDial的重建,因此菜单会保持展开状态,图标也能实时更新。

内容的提问来源于stack exchange,提问作者Tanvir Ibn Mizan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 04:32:42