Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter onClosing callback for showModalBottomSheet

Tags:

flutter

dart

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?

like image 499
KhoPhi Avatar asked Jul 27 '18 21:07

KhoPhi


People also ask

How do you make a showModalBottomSheet scrollable?

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 .

How do you use setState in showModalBottomSheet in flutter?

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, ), ); }, ); }, );

How do you dismiss modal bottom sheet in flutter?

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.

How do you make a bottom dialog in flutter?

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.


2 Answers

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'); }); 
like image 107
iPatel Avatar answered Oct 06 '22 20:10

iPatel


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'); } 
like image 39
AndDev Avatar answered Oct 06 '22 19:10

AndDev