Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add useSafeArea parameter to showModalBottomSheet #107140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

bleroux
Copy link
Contributor

@bleroux bleroux commented Jul 6, 2022

Description

This PR adds an overrideMediaQuery parameter to showModalBottomSheet.
This PR adds an useSafeArea parameter to showModalBottomSheet.

Motivation : #39205 (comment)

showModalBottomSheet adds its own MediaQuery to remove top padding. That makes using an inner SafeArea not possible.
With this PR, if useSafeArea parameter is true a SafeArea will be automatically added.

Related Issue

Fixes #39205
Fixes #70701
Fixes #48677

Tests

Adds 1 test.

@flutter-dashboard flutter-dashboard bot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Jul 6, 2022
@bleroux bleroux force-pushed the fix_modal_bottom_sheet_overrides_media_query branch 2 times, most recently from 1a18183 to 3985b68 Compare July 6, 2022 13:35
@bleroux bleroux requested a review from Piinks July 6, 2022 14:15
Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check #39205 (comment)? It mentions Scaffold.of(context).showBottomSheet(... and showModalBottomSheet(... as test cases, and another comment mentions persistent bottom sheet. I do not know that they are the same, but it would be good to have test cases since they were mentioned.

overrideMediaQuery: false,
builder: (BuildContext context) {
innerContext = context;
return Container();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not also fix this?

Suggested change
return Container();
return MediaQuery(
data: MediaQuery.of(context)
child: Container()
);

I think adding a flag to override the inner workings of a widget is not a scalable pattern across the framework, and not very different from the solution above, especially in terms of being discoverable to the user. I haven't been able to find another case of overrideMediaQuery, but this was likened to the Scaffold.extendBody in the linked issue. Maybe just using the same naming convention will make it easier for the user to associate its purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not also fix this?

Did you meant MediaQuery.of(outerContext)? If yes, this is one of the workaround for this issue: storing the parent context, as done in this unit test. The problem is that this workaround is not easily discoverable and relies on a good understanding of BuildContext 😉

I think adding a flag to override the inner workings of a widget is not a scalable pattern across the framework

I agree too. The problem here is that BottomSheet inner workings relies on something that few widget do : consuming padding using a call to MediaQuery.removePadding. Doing so, it breaks the inner SafeArea that a user might want to add (and it is very difficult for the user to find why composition with a SafeArea does not work inside a BottomSheet). I think the Flutter way would have been to let the user use composition to decide if he wants to consume the top padding. Just my 2 cents to explain why this fix is mainly a way to find an official workaround without breaking existing code. Otherwise, users will use workarounds such as this one: adding an inner MaterialApp!.

Adding a property is the good option for compatibility and I agree that the main point is to find a good name for it. I agree too that overridesMediaQuery is not very good and unusual. extentBehingNavigationBar will be on par with Scaffold similar property, but discoverability is not optimal and this name is not fully self explanatory in the BottomSheet case. Naming is hard 😄

I just remembered that showDialog has a useSafeArea property. We might do something similar here. Its default value would be false. If true, instead of adding a MediaQuery that consumes the topPadding, we can insert a SafeArea. It will also make showModalBottomSheet and showDialog similar. And I think it is great for discoverability because most of the users participating in those issues were looking for a way to add a SafeArea in their BottomSheet.

@Piinks I could try to implement this, do you think adding a useSafeArea property is a valuable approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well said! I do appreciate how thorough you always are. :)
I think useSafeArea makes sense so we have a consistent patterns for showModalBottomSheet and showDialog.

Can you try it with the test cases reported in the issue? I think this sounds like a great solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Piinks
I updated the PR according to our previous discussion: adding a useSafeArea parameter to showModalBottomSheet.

About persistent bottom sheets, shown using showBottomSheet or Scaffold.bottomSheet property, despite both implementations adding a BottomSheet instance, the way they insert this widget in the tree is very different. The unusual MediaQuery.removePadding call is only done when calling showModalBottomSheet :

final Widget bottomSheet = MediaQuery.removePadding(

There is one open issue, #69676, related to showBottomSheet and SafeArea. After investigating it, I think it is more actionable to not cover these two different cases in the same PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks for investigating!

@bleroux bleroux force-pushed the fix_modal_bottom_sheet_overrides_media_query branch from 3985b68 to 33dc57b Compare July 14, 2022 08:43
@bleroux bleroux changed the title Make ModalBottomSheet overrides MediaQuery optional Add useSafeArea parameter to showModalBottomSheet Jul 15, 2022
Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This closes a bunch of issues, well done! LGTM!

@Piinks Piinks added autosubmit Merge PR when tree becomes green via auto submit App a: quality A truly polished experience labels Jul 19, 2022
@auto-submit auto-submit bot merged commit 9bd058e into flutter:master Jul 19, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 20, 2022
@bleroux bleroux deleted the fix_modal_bottom_sheet_overrides_media_query branch July 21, 2022 11:24
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 21, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 22, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 22, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Jul 22, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 23, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 23, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 24, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 24, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 25, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 26, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 26, 2022
camsim99 pushed a commit to camsim99/flutter that referenced this pull request Aug 10, 2022
@aqrc
Copy link

aqrc commented Jan 4, 2023

Sorry, when this feature is going to be released? It is already in documentation for 3.3.10, but not in the source code, which is confusing.

@Sander0542
Copy link

It looks like this feature is going to be released in Flutter 3.4. The merge commit (9bd058e) is in the pre-releases of Flutter 3.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: quality A truly polished experience autosubmit Merge PR when tree becomes green via auto submit App f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
4 participants