Flutter 应用运行在许多屏幕尺寸(手机、平板、web、桌面)上,因此构建响应式(适应尺寸)和自适应(适应平台)UI 很重要。Flutter 提供了 MediaQuery、LayoutBuilder 和灵活布局小部件等工具。
响应式:适应屏幕尺寸
// MediaQuery — get screen dimensions and adapt
final width = MediaQuery.of(context).size.width;
if (width > 600) {
// tablet/desktop layout (e.g. side-by-side)
} else {
// phone layout (e.g. stacked)
}
// LayoutBuilder — adapt based on the available space (parent constraints)
LayoutBuilder(builder: (context, constraints) {
if (constraints.maxWidth > 600) return WideLayout();
return NarrowLayout();
})
