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?
If you wish to have a bottom sheet that has a scrollable child such as a ListView or a GridView and have the bottom sheet be draggable, you should set this parameter to true. The useRootNavigator parameter ensures that the root navigator is used to display the BottomSheet when set to true .
showModalBottomSheet( context: context, builder: (BuildContext context) { return BottomSheet( onClosing: () {}, builder: (BuildContext context) { bool b = false; return StatefulBuilder( builder: (BuildContext context, setState) => Switch( onChanged: (bool v) { setState(() => b = v); }, value: b, ), ); }, ); }, );
Modal bottom sheets appear when triggered by a user action, such as tapping a button or an overflow icon. They can be dismissed by: Tapping a menu item or action within the bottom sheet. Tapping the scrim. Swiping the sheet down.
Here's the code: class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return MaterialApp( title: 'Modal Bottom Sheet Demo', home: Scaffold( backgroundColor: Colors. blueGrey, appBar: AppBar( title: const Text('Creating a Modal Bottom Sheet'), backgroundColor: Colors.
You can also achieve it by use of whenComplete
function of showModalBottomSheet
.
Let's see below code
showModalBottomSheet<void>( context: context, builder: (BuildContext context) { return Container( height: 260.0, child: Text('I am text') ); }, ).whenComplete(() { print('Hey there, I\'m calling after hide bottomSheet'); });
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'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With