不更新SpeedDialChild的状态(flutter_speed_dial:^7.0.0)

mutmk8jj  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(90)

我正在尝试更新SpeedDialChild图标值的基础上一个布尔值。

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(
                              child: Icon(
                                showCriticalPoint.value ? Icons.report_off_outlined : Icons.report_outlined,
                                color: Colors.white,
                              ),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(8),
                              ),
                              backgroundColor: Colors.red,
                              onTap: () {
                                showCriticalPoint.value = !showCriticalPoint.value;
                              },
                            ),
                            
                          ],
                        ),

在儿童我想改变图标时,其轻敲

SpeedDialChild(
                              child: Icon(
                                showCriticalPoint.value ? Icons.report_off_outlined : Icons.report_outlined,
                                color: Colors.white,
                              ),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(8),
                              ),
                              backgroundColor: Colors.red,
                              onTap: () {
                                showCriticalPoint.value = !showCriticalPoint.value;
                              },
                            ),

但未更新图标状态。如何在不关闭快速拨号菜单情况下更新图标状态?

hjqgdpho

hjqgdpho1#

看起来快速拨号保留了上下文。
请执行以下操作,

final showCriticalPoint = useState(false);
var myGlobalKey = GlobalKey();
....
SpeedDial(
key: myGlobalKey,
....
// Inside onTap
onTap: () {
  setState(() {
    myGlobalKey = GlobalKey();
    showCriticalPoint.value = !showCriticalPoint.value;
  });
},

这将强制快速拨号释放以前的上下文并在UI中呈现更新的值。

相关问题