Responsive Builder 是一個 Flutter 套件,專為簡化跨多個平台建立響應式使用者介面而設計。它提供實用函數與元件,能根據螢幕大小、裝置類型及方向自動調整版面與樣式。透過使用斷點與彈性約束,開發者可建立動態適應各種裝置的 UI,無需撰寫重複的條件邏輯。非常適合針對 Android、iOS、Web、macOS、Windows 與 Linux 的應用程式,可簡化響應式設計的工作流程。
適用情境
建構跨平台響應式佈局
適應不同螢幕尺寸的使用者介面
為網頁和移動設備創建自適應設計
簡化基於斷点的樣式
支援多裝置應用開發
主要特色
基於斷点的響應式邏輯
跨平台相容性
靈活的部件實用工具
自動調整版面
最小的樣板程式碼
適合對象
開發跨平台應用的開發者
注重響應式設計的團隊
需要動態UI適應的項目
Flutter 初學者學習響應式模式
針對多樣化設備生態系統的應用
使用建議
可能需要了解響應式設計原則
對複雜佈局的自訂有限
依賴於斷点的一致使用
最適合標準響應式需求
不太適合高度動態或非線性佈局
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
The Responsive Builder
This package provides a set of utilities to aid in building responsive Flutter applications that adapt to different screen sizes and orientations.
1. Setup
Firstly, add package to pubspec.yaml.
Then, wrap your main MaterialApp widget with the TheResponsiveBuilder
voidmain() => runApp(
TheResponsiveBuilder(
builder: (context, orientation, screenType) {
returnMaterialApp(
//... your other MaterialApp properties
);
},
),
);
2. Usage of extensions
With the utilities set up, you can easily use the extensions provided:
- Height & Width
// This will set the height to 20% of the screen heightContainer(height: 20.h, ...);
// This will set the width to 50% of the screen widthContainer(width: 50.w, ...);
- Density Pixels & Scaled Pixels
// This uses a logarithmic formula to scale UI elements based on screen size and pixel density.TextStyle(fontSize: 16.dp, ...);
// This scales the text size based on the user's font size settings.TextStyle(fontSize: 16.sp, ...);
- Screen Type & Orientation
if (context.screenType == ScreenType.mobile) {
// Build UI for mobile
} else {
// Build UI for tablet
}
if (context.orientation == Orientation.portrait) {
// Build UI for portrait mode
} else {
// Build UI for landscape mode
}
- Lock & Unlock Screen Orientation
// Lock screen orientation to Portrait mode
context.lockToPortrait();
// Lock screen orientation to Landscape mode
context.lockToLandscape();
// Unlock screen orientation to automatic mode
context.unlockOrientation();