'Hide Overlay only works at random times

I'm working on this flutter app and I've created this icon at the bottom of the screen. It's an overlay so it can stay at the bottom of the screen. Furthermore, I have a "Go back" button which goes back to the home screen using Navigator.pop(context);. On the "Go back" button I also have a hideOverlay(); function:

void hideOverlay(){
    entry?.remove();
    entry = null;
}

The showOverlay(); gets called at the top of the Widget:

WidgetsBinding.instance!.addPostFrameCallback((_) => showOverlay());

Here's the showOverlay();

void showOverlay() {
    entry = OverlayEntry(
        builder: (context) => Container(
          alignment: Alignment.bottomCenter,
          margin: const EdgeInsets.fromLTRB(0, 0, 0, 25),
          child: ElevatedButton(
            onPressed: () {},
            child: Icon(Icons.camera_alt_outlined, color: Colors.white),
            style: ElevatedButton.styleFrom(
              shape: CircleBorder(),
              padding: EdgeInsets.all(16),
              primary: Color(colorUtils.getColor(SWColor.highlighted)), // <-- Button color
              onPrimary: Color(colorUtils.getColor(SWColor.unhighlighted)), // <-- Splash color
            ),
          )
        )
    );

    final overlay = Overlay.of(context);
    overlay?.insert(entry!);
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source