Skip to content

Support getting the maximum window size (or the size of the display) #123307

@dnfield

Description

@dnfield

Today, on some Android devices (particularly large screen/foldable), restricting your orientation may place you into a letterboxed state. In that state, View#onSizeChanged will no longer report the actual size of the whole screen availalbe, but a smaller portion of the screen size.

In a scenario where a Flutter app decides to lock its orientation because of a small screen size (e.g. when the phone is folded up), it may end up locking itself into letter boxing when the phone is opened. It will never get the full avialalbe screen size in the MediaQuery because it has locked its orientation.

One work around for this is to unlock orientation during the fold transition, e.g.

class UnlockingObserver extends WidgetsBindingObserver {
  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    if (window.displayFeatures.any((DisplayFeature feature) => feature.state == DisplayFeatureState.postureHalfOpened)) {
      SystemChrome.setPreferredOrientations([]);
    }
  }
}

However, we should either expose some way to get the maximum display size (which is probably generally useful on other platforms as well, and which isn't necessarily the same as what the MediaQuery/window expose as the size).

Android recommends this formula:

/** Determines whether the device has a compact screen. **/
public boolean compactScreen() {
    WindowMetrics screenMetrics = WindowMetricsCalculator
                                  .getOrCreate()
                                  .computeMaximumWindowMetrics(this);
    int shortSide = Math.min(screenMetrics.getBounds().width(),
                             screenMetrics.getBounds().height());
    return shortSide / getResources().getDisplayMetrics().density < 600;
}

/cc @reidbaker -we've been talking about an issue related to this recently.
/cc @TytaniumDev for the same reason

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listengineflutter/engine related. See also e: labels.platform-androidAndroid applications specifically

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions