TechAurelian
TechAurelian

Reputation: 5811

Automatically close a Flutter modal bottom sheet when the app is paused

I'm using showModalBottomSheet to show a modal material design bottom sheet.

showModalBottomSheet<void>(
  context: context,
  builder: (BuildContext context) {
  ...

I'd like this bottom sheet to automatically close (pop?) when Flutter Android app is paused (for example the user is tapping the home button).

How can I do this?

Upvotes: 0

Views: 982

Answers (2)

Nazarii Kahaniak
Nazarii Kahaniak

Reputation: 511

Check WidgetsBindingObserver: https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html. You can extend it in your modal widget and call pop() when the app state is paused.

Upvotes: 1

BLKKKBVSIK
BLKKKBVSIK

Reputation: 3548

Code to detect if user pressed home / tab on flutter? might be a viable solution?

If you detect a pause change in the app lifecycle you can probably then make a navigator.of(context).pop ?

Upvotes: 0

Related Questions