我正在使用 flutter speed dial 包来实现可扩展的 FAB。 它运行良好。我遇到的唯一问题是,当我在快速拨号打开的情况下按下后退按钮时,它会关闭(将其置于背景),而当我再次打开应用程序时,快速拨号仍然打开。 我希望在按下后退按钮而不关闭应用程序时关闭快速拨号。
我尝试使用 PopScope 但没有成功,而不是包示例页面中使用的 WillPopScope。
return SpeedDial(
activeIcon: Icons.close_rounded,
backgroundColor: theme.colorScheme.primary,
foregroundColor: theme.colorScheme.primary,
overlayColor: Colors.black,
overlayOpacity: 0.6,
spaceBetweenChildren: 16,
iconTheme: const IconThemeData(color: Colors.white),
icon: Icons.add_rounded,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(Sizes.sixteen))),
children: [
查看 SpeedDial 中的
openCloseDial
属性。
您可以创建一个值通知程序并将其分配给属性并在 WillPopScope 中处理它。
var isDialOpen = ValueNotifier<bool>(false);
SpeedDial(
...
openCloseDial: isDialOpen
...
)
在 PopScope 中,您可以执行以下操作:
onWillPop: () async {
if (isDialOpen.value) {
isDialOpen.value = false;
return false;
}
return true;
},
如果您使用 Go_Router 请记住 PopScope 不起作用,您可以使用 BackButtonListener
https://api.flutter.dev/flutter/widgets/BackButtonListener-class.html
使用示例:
BackButtonListener(
onBackButtonPressed: () async {
print('test');
return true;
},
child: SomeWidgets(),
);