npx skills add https://github.com/flutter/skills --skill flutter-layout通过组合布局部件、管理约束条件并实现自适应设计模式,构建健壮、响应式的 Flutter 用户界面。假设目标环境已安装 Flutter SDK,并且用户熟悉 Dart 语法和状态管理基础知识。
确定布局策略(决策逻辑) 分析 UI 需求,并使用以下决策树选择合适的底层布局部件:
Row。Column。Stack 并搭配 Positioned 或 Align 子部件。ListView。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
GridView。CustomScrollView 搭配 Slivers。LayoutBuilder 或 MediaQuery。应用约束的黄金法则 强制执行 Flutter 的核心布局规则:约束向下传递,尺寸向上传递,父部件设置位置。 当一个部件需要特定尺寸时,确保其父部件允许该尺寸。使用 ConstrainedBox 来注入特定的约束,但请记住它只是在父部件的约束基础上添加限制。
// 示例:在灵活的父部件内强制指定尺寸 Center( child: ConstrainedBox( constraints: const BoxConstraints( minWidth: 70, minHeight: 70, maxWidth: 150, maxHeight: 150, ), child: Container( color: Colors.blue, width: 1000, // 将被约束为 150 (最大值) height: 10, // 将被约束为 70 (最小值) ), ), )
实现自适应布局 对于必须同时支持移动设备和平板/桌面形态的屏幕,实现一个 LayoutBuilder,根据可用宽度来分支 UI 逻辑。
class AdaptiveScreen extends StatelessWidget { const AdaptiveScreen({super.key});
@override 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()), // 侧边栏 Container(width: 1, color: Colors.grey), // 分隔线 const Expanded(child: Placeholder()), // 主内容区 ], ); }
Widget _buildNarrowLayout() { return const Column( children: [ Expanded(child: Placeholder()), // 主内容区 ], ); } }
组合弹性布局(行和列) 当使用 Row 或 Column 时,使用 Expanded(强制子部件填满可用空间)或 Flexible(允许子部件小于可用空间)来管理子部件的尺寸。
Row( 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), ), ], )
收集上下文 暂停并询问用户: "此布局的目标设备是什么(例如,移动设备、平板、网页)?需要哪些具体的断点宽度?"
验证与修复:处理无界约束 验证没有将 Expanded 或 Flexible 部件放置在提供无界约束的父部件内(例如 ListView 或 SingleChildScrollView)。如果发生 RenderFlex 溢出错误,请实施以下修复:
// 错误:在可滚动视图中使用 Expanded 会导致无界高度错误。 // SingleChildScrollView(child: Column(children: [Expanded(child: Container())]))
// 正确:使用有界高度或移除 Expanded。
// 或者,使用带有 SliverFillRemaining 的 CustomScrollView:
CustomScrollView(
slivers: [
SliverToBoxAdapter(child: HeaderWidget()),
SliverFillRemaining(
hasScrollBody: false,
child: ExpandedContentWidget(),
),
],
)
SafeArea 中,以防止与系统 UI 重叠。Expanded 或 Flexible 部件放置在提供无界约束的父部件内(例如,SingleChildScrollView、ListView,或水平滚动视图内的 Row)。Container;使用 Padding 部件以获得更好的性能。useMaterial3: true)。每周安装量
1.2K
代码仓库
GitHub 星标数
833
首次出现
2026年3月4日
安全审计
安装于
codex1.2K
cursor1.2K
opencode1.2K
github-copilot1.2K
gemini-cli1.2K
amp1.2K
Constructs robust, responsive Flutter user interfaces by composing layout widgets, managing constraints, and implementing adaptive design patterns. Assumes the target environment has the Flutter SDK installed and the user is familiar with Dart syntax and state management fundamentals.
Determine Layout Strategy (Decision Logic) Analyze the UI requirements and select the appropriate base layout widgets using the following decision tree:
Row.Column.Stack with Positioned or Align children.ListView.GridView.CustomScrollView with Slivers.LayoutBuilder or MediaQuery.Apply the Golden Rule of Constraints Enforce Flutter's core layout rule: Constraints go down. Sizes go up. Parent sets position. When a widget requires a specific size, ensure its parent allows it. Use ConstrainedBox to inject specific constraints, but remember it only adds to the parent's constraints.
// 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) ), ), )
Implement Adaptive Layouts For screens that must support both mobile and tablet/desktop form factors, implement a LayoutBuilder to branch the UI logic based on available width.
class AdaptiveScreen extends StatelessWidget { const AdaptiveScreen({super.key});
@override 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 ], ); } }
Compose Flex Layouts (Rows and Columns) When using Row or Column, manage child sizing using Expanded (forces child to fill available space) or Flexible (allows child to be smaller than available space).
Row( 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), ), ], )
Gather Context STOP AND ASK THE USER: "What are the target devices (e.g., mobile, tablet, web) and specific breakpoint widths required for this layout?"
Validate-and-Fix: Handle Unbounded Constraints Verify that no Expanded or Flexible widgets are placed inside unbounded parents (like ListView or SingleChildScrollView). If a RenderFlex overflow occurs, implement the following fix:
// 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(),
),
],
)
SafeArea to prevent overlap with system UI.Expanded or Flexible widget inside a parent that provides unbounded constraints (e.g., SingleChildScrollView, ListView, or Row inside a horizontally scrolling view).Container solely for padding; use the Padding widget for better performance.useMaterial3: true is default).Weekly Installs
1.2K
Repository
GitHub Stars
833
First Seen
Mar 4, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex1.2K
cursor1.2K
opencode1.2K
github-copilot1.2K
gemini-cli1.2K
amp1.2K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
103,800 周安装
YouTube视频分析师 - 逆向分析病毒内容公式,提取钩子、留存机制与情感触发点
647 周安装
SQLiteData 使用指南:SwiftData 轻量级替代方案,支持 CloudKit 同步
CTF密码学挑战速查指南 | 经典/现代密码攻击、RSA/ECC/流密码实战技巧
648 周安装
Bitrefill CLI:让AI智能体自主购买数字商品,支持加密货币支付
Bilibili 字幕提取工具 - 支持 AI 字幕检测与 ASR 转录,一键下载视频字幕
648 周安装
assistant-ui thread-list 线程列表:管理多聊天线程的 React AI SDK 组件
649 周安装