Sub topic 3
One-pane and two-pane list-detail layouts
This chapter assumes you know Dart 3 syntax, can build widget trees with StatelessWidget and StatefulWidget, and can use Row, Expanded, ListView and Navigator. You should also be able to run a Flutter app on a device, emulator or desktop target.
A list-detail layout shows a list of items and the details of the selected item. On a narrow window, the two parts take turns: the list fills the screen, and tapping an item opens its details on a new page. On a wide window, both parts sit side by side.
One pane or two panes
The choice depends on the available width, which you read with LayoutBuilder. The 600 logical pixel boundary comes from Material 3's medium window class.
import 'package:flutter/material.dart';
class Item {
final String title;
const Item(this.title);
}
class ListDetail extends StatefulWidget {
const ListDetail({super.key});
@override
State<ListDetail> createState() => _ListDetailState();
}
class _ListDetailState extends State<ListDetail> {
static const _items = [Item('Inbox'), Item('Sent'), Item('Drafts')];
int _selected = 0;
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final wide = constraints.maxWidth >= 600;
final list = ListView.builder(
itemCount: _items.length,
itemBuilder: (context, i) => ListTile(
title: Text(_items[i].title),
selected: wide && i == _selected,
onTap: () {
if (wide) {
setState(() => _selected = i);
} else {
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => Scaffold(
appBar: AppBar(),
body: Center(child: Text(_items[i].title)),
),
));
}
},
),
);
if (!wide) return list;
return Row(
children: [
SizedBox(width: 280, child: list),
const VerticalDivider(width: 1),
Expanded(child: Center(child: Text(_items[_selected].title))),
],
);
},
);
}
}Platform conventions
Use defaultTargetPlatform from package:flutter/foundation.dart to choose platform-specific behavior, such as back-navigation style or page transitions. Material widgets already follow many platform rules, so change only what the platform clearly expects.
Touch, mouse and keyboard
Keep touch targets at least 48 logical pixels, which ListTile already does. Mouse users expect hover feedback, which you can add with MouseRegion. Keyboard users need focus and arrow-key support, which ListView and Focus widgets provide when you keep the focus order logical.
Testing layouts at different sizes
Set the simulated window size on tester.view, then reset it in a teardown:
testWidgets('shows two panes when wide', (tester) async {
tester.view.physicalSize = const Size(1200, 800);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(const MaterialApp(home: ListDetail()));
expect(find.byType(VerticalDivider), findsOneWidget);
});Common pitfalls
- Reading
MediaQueryfor a layout choice inside a panel. The panel's width can differ from the window's width, soLayoutBuildergives the correct answer. - Forgetting to reset the test view size, which leaks into later tests.
- Hard-coding the 280-pixel list width. Use a fraction of the available width when the list must scale.
Foldables and multi-display considerations
A foldable device can expose a hinge or fold through MediaQuery.of(context).displayFeatures. Avoid placing controls over a fold. Treat a folded or split window as an ordinary width change, and let LayoutBuilder handle it.
Exercise
A user resizes the window from 1200 to 500 pixels while a detail pane is open in the two-pane layout. What should the app show, and why?
Show answer
It should show the one-pane list, because 500 is below the 600 pixel breakpoint. The selected item's details should remain reachable by tapping the item, which pushes the detail page. Using LayoutBuilder makes this switch happen automatically on each rebuild.