Sub topic 15
Foldables and multi-display considerations
This chapter closes the tutorial. It covers what changes when the window is not one simple rectangle. You should already know MediaQuery, LayoutBuilder, window-size classes, and the list-detail pattern from earlier chapters. The chapter also assumes you can run a Flutter app on an Android emulator or device. Foldable hinge data comes from the platform, and Android is where it is available.
What a foldable changes
A foldable phone or tablet has one flexible screen, or two screens joined by a hinge. From Flutter's point of view, three things can change while the app runs:
- The window size changes when the device is folded or unfolded.
- A display feature can appear. This is a region of the screen that is cut, folded, or covered.
- The window can be resized by the system, for example in split-screen or freeform mode.
Most of this is already handled by what you built earlier. Unfolding a phone gives your app a larger window, and a layout driven by window-size classes moves from compact to medium or expanded without special code. Display features matter only when a hinge or fold would cut through your content.
Display features in MediaQuery
MediaQuery.displayFeaturesOf(context) returns a list of DisplayFeature objects. Each one describes a region of the window in logical pixels.
| Property | Type | Meaning |
|---|---|---|
| `bounds` | `Rect` | The feature's rectangle, in window coordinates |
| `type` | `DisplayFeatureType` | `fold`, `hinge`, `cutout`, or `unknown` |
| `state` | `DisplayFeatureState` | `postureFlat`, `postureHalfOpened`, or `unknown` |
The types differ in what they mean for layout:
- A
foldis a crease in a continuous screen. Content can cross it, but it may look bad there. - A
hingeis a physical gap or obstruction between two screens. Content under it is lost. - A
cutoutis a camera hole or notch. It is not a separator, andSafeAreaalready handles most cutout cases.
The state tells you the posture. postureHalfOpened means the device is partly folded, as in a laptop or tabletop posture. postureFlat means it is fully open. Not every device reports a state, so treat unknown as "no posture information".
Finding the feature that separates content
Only fold and hinge features can split your layout. This helper returns the first one, or null.
import 'dart:ui' show DisplayFeature, DisplayFeatureType;
import 'package:flutter/widgets.dart';
DisplayFeature? separatingFeature(BuildContext context) {
for (final feature in MediaQuery.displayFeaturesOf(context)) {
final separates = feature.type == DisplayFeatureType.fold ||
feature.type == DisplayFeatureType.hinge;
if (separates) return feature;
}
return null;
}Call it from build. MediaQuery.displayFeaturesOf subscribes the widget to display-feature changes only, so the widget does not rebuild for unrelated MediaQuery changes such as the keyboard opening.
A fold-aware two-pane layout
The next widget puts one pane on each side of a separating feature. The orientation of the feature decides the arrangement:
- A tall, narrow rectangle is a vertical hinge, so the panes sit side by side.
- A wide, short rectangle is a horizontal hinge, so the panes are stacked.
- With no separating feature, the panes share the space equally.
import 'package:flutter/widgets.dart';
class FoldAwareTwoPane extends StatelessWidget {
const FoldAwareTwoPane({super.key, required this.start, required this.end});
final Widget start;
final Widget end;
@override
Widget build(BuildContext context) {
final feature = separatingFeature(context);
if (feature == null) {
return Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [Expanded(child: start), Expanded(child: end)],
);
}
final bounds = feature.bounds;
final isVerticalHinge = bounds.width < bounds.height;
if (isVerticalHinge) {
return Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(width: bounds.left, child: start),
SizedBox(width: bounds.width),
Expanded(child: end),
],
);
}
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(height: bounds.top, child: start),
SizedBox(height: bounds.height),
Expanded(child: end),
],
);
}
}This sample assumes the widget fills the whole window. The bounds rectangle is in window coordinates. If the widget sits below an AppBar or inside a padded parent, the vertical offsets are wrong. In that case, convert the rectangle to local coordinates with RenderBox.globalToLocal. Or wrap the content in DisplayFeatureSubScreen, which Flutter provides to keep a child inside the one sub-screen that contains an anchorPoint.
Reserve this widget for places where the pane split matters, such as list-detail. For content that can scroll across a fold, such as an article, a hinge-aware split is usually unnecessary.
Postures and what to do in each
Which posture you can react to depends on the device. A book-style posture has a vertical hinge, and the two sides work well as list and detail. A tabletop posture has a horizontal hinge, so the top half can show content such as video and the bottom half can show controls. In the flat posture, the window-size class alone is usually enough.
State must survive size changes
Unfolding changes the window size without restarting the app. Flutter's default Android manifest declares the configuration changes it handles itself, such as screenSize and smallestScreenSize, so the activity is not recreated. Your widgets simply rebuild with new constraints.
This only works if your state does not depend on the layout:
- Keep the selected item in state that lives above the layout switch, not inside a one-pane or two-pane subtree. If the detail page owns the selection, collapsing to one pane can lose it.
- Do not lock orientation with
SystemChrome.setPreferredOrientationsunless you must. A portrait lock on a foldable gives a stretched, letterboxed result. Recent Android versions also ignore such restrictions on large screens. - Keep
android:resizeableActivityat its default oftrue, so the system can put your app in split-screen or freeform windows.
Multiple windows and displays
The ideas from earlier chapters extend to every situation where the window is not the full screen:
- In Android split-screen, your window may be half the screen. Use
LayoutBuilderorMediaQuery.sizeOfand window-size classes, never the physical screen size. - On a device with two separate displays, each app window lives on one display. The app sees its own window, plus any hinge reported inside it.
- On desktop, the user can resize freely and drag the window between monitors with different pixel densities. Never cache
devicePixelRatio. Read it fromMediaQuery.devicePixelRatioOfwhen you need it. - Flutter's support for several windows in one app on desktop has been experimental. Check the current documentation before depending on it.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Treating a hinge like a cutout | Text or buttons hidden under the gap | Handle `hinge` and `fold` types separately from `cutout` |
| Assuming the feature list is non-empty | Null or index errors on normal phones | Use a helper that returns `null`, as above |
| Using the device's screen size | Wrong layout in split-screen | Read the window or parent constraints |
| Keeping selection state inside a pane | Selection lost when panes merge | Hoist state above the layout switch |
| Testing only on a phone emulator | Fold bugs found by users | Use a foldable emulator and widget tests |
Testing fold layouts
Android Studio includes foldable emulator profiles, which let you fold and unfold the device while the app runs. For repeatable checks, a widget test can supply display features directly through MediaQueryData.
import 'dart:ui' show DisplayFeature, DisplayFeatureState, DisplayFeatureType;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('splits panes at a vertical hinge', (tester) async {
tester.view.physicalSize = const Size(1200, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
final data = MediaQueryData(
size: const Size(1200, 800),
displayFeatures: [
DisplayFeature(
bounds: const Rect.fromLTWH(590, 0, 20, 800),
type: DisplayFeatureType.hinge,
state: DisplayFeatureState.postureHalfOpened,
),
],
);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: data,
child: const FoldAwareTwoPane(
start: SizedBox.expand(key: Key('start')),
end: SizedBox.expand(key: Key('end')),
),
),
),
);
expect(tester.getSize(find.byKey(const Key('start'))).width, 590);
expect(tester.getSize(find.byKey(const Key('end'))).width, 590);
});
}The test needs FoldAwareTwoPane and separatingFeature imported from your app. The right pane is 590 pixels wide: the 1200-pixel window minus the 590-pixel left pane and the 20-pixel hinge. Add a second test with an empty displayFeatures list to cover the no-fold case, and a third with a wide horizontal hinge to cover the stacked case.
Exercise
A user unfolds a phone while viewing an item in a one-pane list-detail layout. Name two things in your code that decide whether the item is still selected in the two-pane layout.
Show answer
First, the selected item must be stored in state above the widget that switches between one pane and two panes, such as in the shell widget or a state-management object, not in the detail page. Second, the app must not be recreated on the size change, which holds with Flutter's default Android manifest configuration and no orientation lock. With both in place, the rebuilt two-pane layout reads the same selection.
Exercise
A device reports one display feature with type hinge and bounds Rect.fromLTWH(0, 380, 800, 40) in an 800 by 800 window. Which arrangement does FoldAwareTwoPane choose, and what are the heights of the two panes?
Show answer
The width (800) is not less than the height (40), so it is not a vertical hinge. The widget uses the Column. The first pane is bounds.top = 380 pixels tall, the gap is 40 pixels, and the second pane takes the rest: 800 - 380 - 40 = 380 pixels.