Search code examples
dartflutter

Flutter onClosing callback for showModalBottomSheet


I have a showModalBottomSheet like the below, which I understand to inherit from BottomSheet (right?)

      showModalBottomSheet<void>(
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 260.0,
            child: Text('I am text')
          );
        },
      );

What I want to do:

I want to know (listen) when the modal is being closed, and act on it.

I've seen this onClosing callback: https://docs.flutter.io/flutter/material/BottomSheet/onClosing.html

How can I have a listener attached to the showModalBottomSheet, and then act accordingly when it fires?


Solution

  • Perhaps it's not the best solution, but showModalBottomSheet return a "Future" so you can used it.

    For example:

    void _showModal() {
        Future<void> future = showModalBottomSheet<void>(
          context: context,
          builder: (BuildContext context) {
            return Container(height: 260.0, child: Text('I am text'));
          },
        );
        future.then((void value) => _closeModal(value));
    }
    void _closeModal(void value) {
        print('modal closed');
    }