Sub topic 14
Adapting Layout to Screen Size and Input, Not Only Platform
This chapter assumes basic Dart, Flutter widgets, MaterialApp, Scaffold, Navigator, and setState. It also assumes you have used both Material and Cupertino widgets.
Platform is only one part of adaptation. An iPad may have a hardware keyboard. An Android phone may be used with a mouse. A desktop window may be narrow. A web app may run on a touch laptop. Use the space and input available at runtime instead of guessing from the operating system.
Use constraints for layout decisions
LayoutBuilder gives a widget the constraints from its parent. This is usually better than checking the device model or platform.
A common pattern is:
- Use a bottom
NavigationBarwhen the available width is narrow. - Use a side
NavigationRailwhen the width is wide. - Let content expand between the navigation and the edge of the window.
- Use scrolling instead of assuming a fixed height.
The breakpoint is a product decision. It should reflect whether the layout remains comfortable, not whether the device is technically a tablet.
import 'package:flutter/material.dart';
void main() {
runApp(const AdaptiveApp());
}
class AdaptiveApp extends StatelessWidget {
const AdaptiveApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorSchemeSeed: Colors.indigo),
home: const AdaptiveHome(),
);
}
}
class AdaptiveHome extends StatefulWidget {
const AdaptiveHome({super.key});
@override
State<AdaptiveHome> createState() => _AdaptiveHomeState();
}
class _AdaptiveHomeState extends State<AdaptiveHome> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final isWide = constraints.maxWidth >= 700;
final keyboardInset = MediaQuery.of(context).viewInsets.bottom;
return Scaffold(
appBar: AppBar(title: const Text('Adaptive settings')),
body: SafeArea(
child: Padding(
padding: EdgeInsets.only(bottom: keyboardInset),
child: Row(
children: [
if (isWide)
NavigationRail(
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() => _selectedIndex = index);
},
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.settings_outlined),
selectedIcon: Icon(Icons.settings),
label: Text('Settings'),
),
],
),
Expanded(
child: ListView(
padding: const EdgeInsets.all(24),
children: [
Text(
isWide ? 'Wide layout' : 'Compact layout',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 16),
const TextField(
decoration: InputDecoration(
labelText: 'Display name',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
FilledButton(
onPressed: () {},
child: const Text('Save'),
),
],
),
),
],
),
),
),
bottomNavigationBar: isWide
? null
: NavigationBar(
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() => _selectedIndex = index);
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.settings_outlined),
selectedIcon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
},
);
}
}The keyboard changes the visible area through viewInsets. Padding content by that inset helps keep focused fields and actions visible. SafeArea protects content from notches, system bars, and rounded screen edges.
Design for different input methods
Touch users need controls with comfortable hit areas. Flutter's standard buttons and navigation widgets generally provide suitable minimum sizes. Do not replace them with tiny icons simply because a wide screen has extra space.
For mouse and keyboard users, add meaningful focus order, visible focus states, keyboard shortcuts where useful, and hover feedback when it clarifies interaction. A touch-first screen should still be usable without a mouse, and a desktop screen should not require precise tapping.
Check adaptation without a device
Widget tests can pump the same widget under different widths. Test behavior at the breakpoint and just above or below it. Also test that a text field remains reachable when the keyboard reduces the visible space.
Exercise
Why is a width check with LayoutBuilder usually better than checking whether the platform is iOS or Android?
Show answer
Width describes the space the widget actually receives. The same platform can run on phones, tablets, desktops, or resized windows, so platform alone cannot choose a reliable layout.