2

I have a Flutter app that displays an animation. I want the animation to pause if there is an open Drawer or a modal dialog (like showModalBottomSheet) displayed anywhere in the App.

I could handle the events of a specific Drawer directly and manage the state on my own. However, I have multiple drawers in my app that I have to track and it feels like a lot of work. Is there a global place were I can check whether a Drawer (or modal dialog) is open anywhere in the app?

Fox32
  • 13,126
  • 9
  • 50
  • 71

2 Answers2

2

Both showModalBottomSheet and modal dialogs are pusing a route to the Navigator. This way one can check via ModalRoute.of(context)?.isCurrent != true whether another route (or dialog) is in front of the current route.

Sadly, the open Drawer of a Scaffold is not a route. However it isn't required to have a key on the scaffold, but one can check via Scaffold.of(context).isDrawerOpen if the next parent Drawer is open.

By combining the two state I can check whether the animation should be paused.

Fox32
  • 13,126
  • 9
  • 50
  • 71
0

Yes, there is way to know whether navigation drawer is opened or not

1. Declare GlobalKey

  GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

2. Assign a key to Scaffold Widget

 return Scaffold(
      key: _scaffoldKey,

3. Used wherever you want to know drawer status

_scaffoldKey.currentState.isDrawerOpen   // This return bool value True-Open, False-Close
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • This is what I have right now, but I have multiple Scaffolds, modal dialogs and I'm also using `showModalBottomSheet`. I'm s struggling a bit with handling the state of all of them on my own. – Fox32 May 22 '20 at 15:50
  • 1
    Then you must create common Scaffold Base class and implement this functionality there – Jitesh Mohite May 22 '20 at 15:52
  • 1
    This doesn't answer the question properly, neither you show you he would implement that in case of many drawers nor you added the dialog part. – iDecode May 22 '20 at 16:20