title: Flutter: CupertinoPicker BottomSheet listener for onClose?
body: <p>I'm looking through the Flutter Gallery for the code related to the CupertinoPicker.</p>
  
  <p>Here is the relevant code extract:</p>
  
  <pre><code>        child: new GestureDetector(
            // Blocks taps from propagating to the modal sheet and popping.
  //          onTap: () { },
            child: new SafeArea(
              child: new CupertinoPicker(
                scrollController: scrollController,
                itemExtent: _kPickerItemHeight,
                backgroundColor: CupertinoColors.white,
                onSelectedItemChanged: (int index) {
                  setState(() {
                    print(_selectedItemIndex);
                    _selectedItemIndex = index;
                  });
                },
                children: new List&lt;Widget&gt;.generate(coolColorNames.length, (int index) {
                  return new Center(child:
                    new Text(coolColorNames[index]),
                  );
                }),
              ),
            ),
          ),
  </code></pre>
  
  <p>Now, I need a callback / listener for when the CupertinoPicker closes, in other words, when the user has made his selection and his selection is final, I need to know what his final selection is.</p>
  
  <p>The use case here is that I want to make an api callback based on his final selection when the bottomsheet closes.</p>
  
  <p>At the moment, I can only get the values as the picker is spun by the user as there is only a callback for onSelectedItemChanged. See gif below.</p>
  
  <p><a href="https://i.stack.imgur.com/nW2Rx.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/nW2Rx.gif" alt="enter image description here"></a></p>
  
  <p>I see the BottomSheet widget has an onClosing callback: <a href="https://docs.flutter.io/flutter/material/BottomSheet-class.html" rel="noreferrer">https://docs.flutter.io/flutter/material/BottomSheet-class.html</a></p>
  
  <p>But I'm confused as to how I can get an instance of it to use as the Flutter Gallery sample is calling the bottomsheet using the following code and there is no method to get the bottomsheet from the code:</p>
  
  <pre><code>      new GestureDetector(
          onTap: () async {
            await showModalBottomSheet&lt;void&gt;(
              context: context,
              builder: (BuildContext context) {
                return _buildBottomPicker();
              },
            );
          },
          child: _buildMenu(),
        ),
  </code></pre>
  
  <p>Does anyone know how I can get the callback listener for this?</p>
  
  <p>EDIT - Based on Remi's solution, I have added the Navigator.of(context).pop(value) code within the onTap call back. However, the CupertinoPicker is not persistent so if the user touches outside the picker, the picker dismisses itself and a null value will be returned:</p>
  
  <pre><code>  Widget _buildBottomPicker() {
      final FixedExtentScrollController scrollController =
          new FixedExtentScrollController(initialItem: _selectedItemIndex);
  
      return new Container(
        height: _kPickerSheetHeight,
        color: CupertinoColors.white,
        child: new DefaultTextStyle(
          style: const TextStyle(
            color: CupertinoColors.black,
            fontSize: 22.0,
          ),
          child: new GestureDetector(
            // Blocks taps from propagating to the modal sheet and popping.
            onTap: () { Navigator.of(context).pop(_selectedItemIndex);},
            child: new SafeArea(
              child: new CupertinoPicker(
                scrollController: scrollController,
                itemExtent: _kPickerItemHeight,
                backgroundColor: CupertinoColors.white,
                onSelectedItemChanged: (int index) {
                  setState(() {
  //                  print(_selectedItemIndex);
  //                  Navigator.of(context).pop(index);
                    _selectedItemIndex = index;
                  });
                },
                children: new List&lt;Widget&gt;.generate(coolColorNames.length, (int index) {
                  return new Center(child:
                    new Text(coolColorNames[index]),
                  );
                }),
              ),
            ),
          ),
        ),
      );
    }
  </code></pre>