flutter no mediaquery ancestor found code example

Example: No MediaQuery widget ancestor found.

Its because, the showModalBottomSheet tries to access the ancestor of type 
MaterialApp from the given context.

Use Builder widget to get new context with MaterialApp ancestor or Separate 
your MaterialAapp and Scaffold widgets into separate widgets.

Using Builder :

floatingActionButton: Builder(
  builder: (context) => FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () { showModalBottomSheet(
          context: context,
          builder: (context) {
            return Text('Modal bottom sheet', style: TextStyle(fontSize: 30));
          });
      }
  ),
),
  
reference:
https://stackoverflow.com/questions/59864150/flutter-exception-caught-by-gesture-no-mediaquery-widget-found-inside-showmodal

Tags:

Misc Example