Loading...
Loading...
How to build your app's layout using Flutter's layout widgets and constraint system
npx skill4agent add flutter/skills flutter-layoutRowColumnStackPositionedAlignListViewGridViewCustomScrollViewLayoutBuilderMediaQueryConstrainedBox// Example: Forcing a specific size within a flexible parent
Center(
child: ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 70,
minHeight: 70,
maxWidth: 150,
maxHeight: 150,
),
child: Container(
color: Colors.blue,
width: 1000, // Will be constrained to 150 (max)
height: 10, // Will be constrained to 70 (min)
),
),
)LayoutBuilderclass AdaptiveScreen extends StatelessWidget {
const AdaptiveScreen({super.key});
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 600) {
return _buildWideLayout();
} else {
return _buildNarrowLayout();
}
},
),
),
);
}
Widget _buildWideLayout() {
return Row(
children: [
const SizedBox(width: 250, child: Placeholder()), // Sidebar
Container(width: 1, color: Colors.grey), // Divider
const Expanded(child: Placeholder()), // Main Content
],
);
}
Widget _buildNarrowLayout() {
return const Column(
children: [
Expanded(child: Placeholder()), // Main Content
],
);
}
}RowColumnExpandedFlexibleRow(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(Icons.star),
Expanded(
flex: 2,
child: Container(color: Colors.red, height: 50),
),
Flexible(
flex: 1,
child: Container(color: Colors.blue, height: 50),
),
],
)ExpandedFlexibleListViewSingleChildScrollView// INCORRECT: Expanded inside a scrollable view causes unbounded height errors.
// SingleChildScrollView(child: Column(children: [Expanded(child: Container())]))
// CORRECT: Use a bounded height or remove Expanded.
// Alternatively, use CustomScrollView with SliverFillRemaining:
CustomScrollView(
slivers: [
SliverToBoxAdapter(child: HeaderWidget()),
SliverFillRemaining(
hasScrollBody: false,
child: ExpandedContentWidget(),
),
],
)SafeAreaExpandedFlexibleSingleChildScrollViewListViewRowContainerPaddinguseMaterial3: true