Sub topic 10
One-pane and two-pane list-detail layouts
Assume you know Dart, StatefulWidget, Scaffold, Row, ListView, and LayoutBuilder. A list-detail layout shows a list of items and the details of the selected item. On a narrow window, it usually shows one pane at a time. On a wider window, it can show both panes together.
This is an adaptive layout because it changes the interaction pattern, not only the size of its widgets.
Choosing one pane or two panes
A phone often does not have enough room for a useful list and detail view side by side. When the user taps an item, replace the list with the detail view. Provide a back button so the user can return to the list.
A tablet or desktop window can show the list and detail view together. The selected item remains visible while the user chooses another item.
Use a local width threshold that reflects the minimum space needed for both panes. The value is a design decision, not a device label.
A working Flutter implementation
LayoutBuilder is useful here because the decision depends on the width available to this page. That width can be smaller than the whole window when the page is inside a shell or split view.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: MessagesPage()));
class Message {
const Message(this.title, this.body);
final String title;
final String body;
}
class MessagesPage extends StatefulWidget {
const MessagesPage({super.key});
@override
State<MessagesPage> createState() => _MessagesPageState();
}
class _MessagesPageState extends State<MessagesPage> {
static const messages = [
Message('Release plan', 'The next release is ready for review.'),
Message('Design notes', 'The compact layout uses one pane.'),
Message('Meeting reminder', 'The design meeting starts at 10:00.'),
];
int selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Messages')),
body: LayoutBuilder(
builder: (context, constraints) {
final isWide = constraints.maxWidth >= 700;
final list = _buildList();
final detail = _buildDetail();
if (isWide) {
return Row(
children: [
SizedBox(width: 280, child: list),
const VerticalDivider(width: 1),
Expanded(child: detail),
],
);
}
return selectedIndex == -1 ? list : detail;
},
),
);
}
Widget _buildList() {
return ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
final message = messages[index];
return ListTile(
title: Text(message.title),
selected: index == selectedIndex,
onTap: () => setState(() => selectedIndex = index),
);
},
);
}
Widget _buildDetail() {
final message = messages[selectedIndex];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
BackButton(onPressed: () => setState(() => selectedIndex = -1)),
Padding(
padding: const EdgeInsets.all(24),
child: Text(message.body),
),
],
);
}
}The example uses -1 to represent “no detail selected” on a narrow window. In production, a nullable selected item can express this state more clearly. On a wide window, the back button is usually unnecessary, so you may hide it based on the same isWide value.
Common errors
- Using the total window width instead of the page’s local constraints. A navigation rail or sidebar may reduce the space available to the page.
- Choosing a breakpoint because a device is called a tablet. Measure whether both panes have enough usable width.
- Forgetting the narrow-layout back action.
- Giving both panes unbounded width inside a
Row. Use a fixed or constrained width for the list andExpandedfor the detail pane. - Losing the selected item when switching between one-pane and two-pane layouts. Store the selection in the state that survives the layout change.
Exercise
Why does this layout use LayoutBuilder instead of checking the device type?
Show answer
LayoutBuilder reports the width available to this page. The same page can therefore adapt correctly inside phones, tablets, desktop windows, or another split view.