flutter-building-layouts by flutter/skills
npx skills add https://github.com/flutter/skills --skill flutter-building-layouts掌握 Flutter 布局的基本规则:约束向下传递,尺寸向上传递,父组件设置位置。
x 和 y 坐标完全在父 Widget 内部定义。子组件不知道自己在屏幕上的位置。Row 或 Column)的交叉轴方向或可滚动区域(ListView)内传递无界约束(例如 double.infinity)。这会导致渲染异常。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
根据所需的空间排列方式选择合适的结构型 Widget。
Row 和 Column: 使用 Row 实现水平线性布局,使用 Column 实现垂直线性布局。使用 mainAxisAlignment 和 crossAxisAlignment 控制子组件对齐。Expanded 和 Flexible: 将 Row 或 Column 的子组件包裹在 Expanded 中以强制其填充可用空间,或包裹在 Flexible 中允许其根据可用空间调整自身尺寸。Container: 当需要对 Widget 应用内边距、外边距、边框或背景颜色时,将其包裹在 Container 中。Stack: 当 Widget 必须在 Z 轴上重叠时,使用 Stack。使用 Positioned 将子组件锚定到 Stack 的特定边缘。SizedBox: 通过将子 Widget 包裹在具有明确 width 和 height 值的 SizedBox 中,对其施加严格、紧凑的约束。应用条件逻辑来处理不同的屏幕尺寸和设备形态。
LayoutBuilder、Expanded 和 Flexible,根据父组件的约束动态调整元素的大小和位置。遵循此顺序工作流来构建和实现健壮的 Flutter 布局。
Stack)。ListView 或 SingleChildScrollView)。Column 内部的 ListView)。Scaffold 和主要结构型 Widget 开始。Expanded 中(如果在弹性盒子内部),或将父组件包裹在可滚动的 Widget 中。反模式: 将 ListView 直接放在 Column 中会导致无界高度异常,因为 Column 向 ListView 提供了无限的垂直空间。
// 错误:抛出无界高度异常
Column(
children: [
Text('Header'),
ListView(
children: [/* items */],
),
],
)
实现: 将 ListView 包裹在 Expanded Widget 中,以将其高度限制在 Column 的剩余空间内。
// 正确:ListView 被约束在剩余空间内
Column(
children: [
Text('Header'),
Expanded(
child: ListView(
children: [/* items */],
),
),
],
)
实现 LayoutBuilder,根据可用宽度有条件地渲染不同的结构型 Widget。
Widget buildAdaptiveLayout(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
// 基于屏幕宽度的条件逻辑
if (constraints.maxWidth > 600) {
// 平板/桌面:并排布局
return Row(
children: [
SizedBox(width: 250, child: SidebarWidget()),
Expanded(child: MainContentWidget()),
],
);
} else {
// 移动端:带导航的堆叠布局
return Column(
children: [
Expanded(child: MainContentWidget()),
BottomNavigationBarWidget(),
],
);
}
},
);
}
每周安装量
2.3K
代码仓库
GitHub 星标
792
首次出现
12 天前
安全审计
安装于
codex2.3K
gemini-cli2.3K
opencode2.3K
github-copilot2.3K
cursor2.3K
kimi-cli2.3K
Master the fundamental Flutter layout rule: Constraints go down. Sizes go up. Parent sets position.
x and y coordinates of a child Widget exclusively within the parent Widget. Children do not know their own position on the screen.double.infinity) in the cross-axis of a flex box (Row or Column) or within scrollable regions (ListView). This causes render exceptions.Select the appropriate structural Widget based on the required spatial arrangement.
Row and Column: Implement Row for horizontal linear layouts and Column for vertical linear layouts. Control child alignment using mainAxisAlignment and crossAxisAlignment.Expanded and Flexible: Wrap children of Row or Column in Expanded to force them to fill available space, or to allow them to size themselves up to the available space.Apply conditional logic to handle varying screen sizes and form factors.
LayoutBuilder, Expanded, and Flexible to dynamically adjust the size and placement of elements based on the parent's constraints.Follow this sequential workflow to architect and implement robust Flutter layouts.
Stack).ListView or SingleChildScrollView).ListView inside a Column).Scaffold and primary structural Widgets.Anti-pattern: Placing a ListView directly inside a Column causes an unbounded height exception because the Column provides infinite vertical space to the ListView.
// BAD: Throws unbounded height exception
Column(
children: [
Text('Header'),
ListView(
children: [/* items */],
),
],
)
Implementation: Wrap the ListView in an Expanded Widget to bound its height to the remaining space in the Column.
// GOOD: ListView is constrained to remaining space
Column(
children: [
Text('Header'),
Expanded(
child: ListView(
children: [/* items */],
),
),
],
)
Implement LayoutBuilder to conditionally render different structural Widgets based on available width.
Widget buildAdaptiveLayout(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
// Conditional logic based on screen width
if (constraints.maxWidth > 600) {
// Tablet/Desktop: Side-by-side layout
return Row(
children: [
SizedBox(width: 250, child: SidebarWidget()),
Expanded(child: MainContentWidget()),
],
);
} else {
// Mobile: Stacked layout with navigation
return Column(
children: [
Expanded(child: MainContentWidget()),
BottomNavigationBarWidget(),
],
);
}
},
);
}
Weekly Installs
2.3K
Repository
GitHub Stars
792
First Seen
12 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex2.3K
gemini-cli2.3K
opencode2.3K
github-copilot2.3K
cursor2.3K
kimi-cli2.3K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
FlexibleContainer: Wrap Widgets in a Container when you need to apply padding, margins, borders, or background colors.Stack: Implement Stack when Widgets must overlap on the Z-axis. Use Positioned to anchor children to specific edges of the Stack.SizedBox: Enforce strict, tight constraints on a child Widget by wrapping it in a SizedBox with explicit width and height values.Expanded (if inside a flex box) or wrap the parent in a scrollable Widget.