Split View
Loading preview
Starting the Flutter app
Master-detail layout, collapses to single pane on mobile
import 'package:flutter/material.dart';
const _items = ['Inbox', 'Sent', 'Drafts', 'Trash'];
const _emails = [
{'from': 'Alice', 'subj': 'Meeting tomorrow'},
{'from': 'Bob', 'subj': 'Project update'},
{'from': 'Charlie', 'subj': 'Lunch plans'},
];
class SplitView extends StatefulWidget {
const SplitView({super.key});
@override
State<SplitView> createState() => _SplitViewState();
}
class _SplitViewState extends State<SplitView> {
int _selectedIndex = 0;
int? _selectedEmail;
var _showDetail = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Split View')),
body: LayoutBuilder(
builder: (context, constraints) {
final isNarrow = constraints.maxWidth < 600;
if (isNarrow) {
return _buildNarrowLayout();
}
return Row(
children: [
SizedBox(
width: 250,
child: ListView.builder(
itemCount: _items.length,
itemBuilder: (context, i) => ListTile(
leading: Icon(_icons[i]),
title: Text(_items[i]),
selected: _selectedIndex == i,
onTap: () => setState(() => _selectedIndex = i),
),
),
),
Expanded(
child: Row(
children: [
Expanded(
flex: 1,
child: ListView.builder(
itemCount: _emails.length,
itemBuilder: (context, i) {
final e = _emails[i];
return ListTile(
title: Text(e['from'] as String),
subtitle: Text(e['subj'] as String),
selected: _selectedEmail == i,
onTap: () => setState(() {
_selectedEmail = i;
_showDetail = true;
}),
);
},
),
),
if (_showDetail && _selectedEmail != null)
Expanded(
flex: 1,
child: Card(
margin: const EdgeInsets.all(8),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
_emails[_selectedEmail!]['subj']
as String,
style: Theme.of(context)
.textTheme
.titleLarge,
),
),
IconButton(
tooltip: 'Close email',
icon: const Icon(Icons.close),
onPressed: () => setState(
() => _showDetail = false),
),
],
),
const SizedBox(height: 16),
const Text(
'Email content goes here. This is the master-detail layout pattern.'),
],
),
),
),
),
],
),
),
],
);
},
),
);
}
static const _icons = [
Icons.inbox,
Icons.send,
Icons.drafts,
Icons.delete,
];
Widget _buildNarrowLayout() {
if (_selectedEmail != null && _showDetail) {
return Column(
children: [
AppBar(
leading: IconButton(
tooltip: 'Back',
icon: const Icon(Icons.arrow_back),
onPressed: () => setState(() => _showDetail = false),
),
title: Text(_emails[_selectedEmail!]['subj'] as String),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(
'Email content. On mobile, the detail collapses to a single pane.',
style: Theme.of(context).textTheme.bodyLarge,
),
),
),
],
);
}
return Column(
children: [
Expanded(
child: ListView(
children: [
for (var i = 0; i < _emails.length; i++)
ListTile(
title: Text(_emails[i]['from'] as String),
subtitle: Text(_emails[i]['subj'] as String),
onTap: () => setState(() {
_selectedEmail = i;
_showDetail = true;
}),
),
],
),
),
],
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
LayoutBuilder switches at 600px. Wide screens get a folder list, email list and detail Card side by side; narrow screens run _buildNarrowLayout, which swaps between list and detail and drops the folders. Tapping an email sets _selectedEmail and _showDetail on both layouts, so a closed detail pane reopens. One gap remains: _selectedIndex highlights a folder but _emails never changes.
Adapting it
- Filter or fetch
_emailsby_selectedIndex. - Replace the nested
AppBarin_buildNarrowLayoutwith a pushed route so the system back gesture works. - Load the message body for
_selectedEmailin place of the placeholder text.