skip to Main Content

I am using the flutter speed dial package to implement expandable FAB.
It is working fine. The only problem I have is that when I press on the back button with the speed dial open, it closes (puts it the background) and when I open the app again the speed dial is still open.
I would like the speed dial to close when I press on the back button without closing the app.

I have tried using PopScope without success as oppose to WillPopScope which is used in the example page of the package.

 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: [

2

Answers


  1. Checkout the openCloseDial property in SpeedDial.

    You can create a value notifier and assign it to the property and handle it in the WillPopScope.

    var isDialOpen = ValueNotifier<bool>(false);
    
    SpeedDial(
      ...
      openCloseDial: isDialOpen
      ...
    )
    

    In your PopScope, you can do this:

    onWillPop: () async {
        if (isDialOpen.value) {
          isDialOpen.value = false;
          return false;
        }
        return true;
      },
    
    Login or Signup to reply.
  2. If you are using Go_Router keep in mind that PopScope wont’t work instead you can use BackButtonListener

    https://api.flutter.dev/flutter/widgets/BackButtonListener-class.html

    Use Example:

     BackButtonListener(
      onBackButtonPressed: () async {
        print('test');
        return true;
      },
      child: SomeWidgets(),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search