Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SafeArea not working in persistent bottomsheet in Flutter

Tags:

flutter

dart

I am using MaterialApp and Scaffold + SafeArea for screens. All works well until I try to use persistent BottomSheet. BottomSheet content igores SafeArea and are shown below system controls, for example in iPhone X.

I tried to wrap BottomSheet contents in another SafeArea element, but it did not help.

Is there a way to get the same functionality as SafeArea to work in BottomSheet? If yes then how?

like image 980
Zigmārs Dzērve Avatar asked Apr 09 '18 16:04

Zigmārs Dzērve


People also ask

Where can I use SafeArea flutter?

SafeArea class Null safety. A widget that insets its child by sufficient padding to avoid intrusions by the operating system. For example, this will indent the child by enough to avoid the status bar at the top of the screen.

How do you ignore safe area in flutter?

It is true by default and setting it to false would disable SafeArea from adding padding to the bottom of the screen. top : This property is also of type bool and setting it to false would avoid padding at top of the screen.

Why do we use SafeArea in flutter?

If you're developing an application using Flutter, avoiding content being clipped by system intrusions can be done by using SafeArea widget. You can set on which sides the system intrusions should be avoided and also the minimum padding to be applied on each side.

How do you set the showModalBottomSheet height in flutter?

It's possible this way: showModalBottomSheet( context: context, isScrollControlled: true, backgroundColor: Colors. transparent, builder: (context) => Container( height: MediaQuery. of(context).


2 Answers

Just make the root Widget of the showModalBottomSheet be a SafeArea Widget

showModalBottomSheet<void>(
    context: context,
    builder: (BuildContext context) {
      return SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[...

enter image description here

like image 128
user976746 Avatar answered Sep 22 '22 07:09

user976746


I have faced this issue too. When I changed code from showModalBottomSheet to _scaffoldKey.currentState.showBottomSheet my SafeArea stopped working.

You can solve it with these steps:

  1. create key for your Scaffold GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
  2. assign created key to your Scaffold key: _scaffoldKey,
  3. set bottom padding to your bottom sheet padding: EdgeInsets.only(bottom: MediaQuery.of(_scaffoldKey.currentState.context).viewPadding.bottom)

Here is a result, I also added 15 padding to top, left and right.

enter image description here

like image 33
miso01 Avatar answered Sep 21 '22 07:09

miso01