搜尋套件、儲存庫與 APP 分類

sizer 封面
套件詳情v3.1.3

sizer

適用於行動裝置、網頁與桌面的響應式UI解決方案——讓適應性變得輕鬆無比。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
7.2萬
喜歡數
1850
Pub 分數
150

安裝套件

flutter pub add sizer

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Sizer 是一個 Flutter 套件,可簡化跨行動裝置、網頁和桌面平台的響應式 UI 設計。它提供了一種簡單方法,透過基於設備螢幕尺寸的邏輯單位,創建能無縫適應不同螢幕尺寸和方向的版面。使用 Sizer,開發人員可以相對於螢幕寬度和高度定義大小,確保無論裝置類型或解析度為何,外觀都保持一致。此套件可輕鬆整合至現有專案中,設定簡單,非常適合建立可擴展且易於維護的使用者介面。

適用情境

  • 建構跨平台響應式應用
  • 為多種螢幕尺寸創建自適應版面
  • 確保移動設備和桌面端的UI縮放一致
  • 簡化響應式設計,無需複雜的媒體查詢
  • 設計適用於小型手機和大型桌面螢幕的版面

主要特色

  • 自動螢幕尺寸適應
  • 支援所有主要平台(Android、iOS、Web、macOS、Windows、Linux)
  • 易於使用的基於比例的尺寸調整
  • 內建方向處理
  • 所需的最小樣板程式碼

適合對象

  • 針對多個平台的開發者
  • 注重UI一致性的團隊
  • 需要快速響應版面實現的專案
  • 初學者學習響應式Flutter設計
  • 需要在各種設備上動態擴展的應用程式

使用建議

  • 可能需要對現有的硬編碼值進行初始調整
  • 最佳搭配一致的設計系統使用
  • 不太適合高度自訂或非均勻縮放的需求
  • 僅限基於尺寸的響應式,不包含版面結構變更
  • 需要理解邏輯像素縮放原理

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Sizer (Responsive UI solution for Mobile App,Web and Desktop)

Looking for Flutter development support? Let’s connect — available on WhatsApp and Email.


A Flutter package that simplifies responsive design by automatically adapting your app’s UI to any screen size — making responsiveness seamless and intuitive.

With Sizer

Without Sizer

Content

Installation ⬇️

Add sizer to pubspec.yaml

dependencies:
  sizer: ^3.1.3
How to use ⚙️
Import the Package
import 'package:sizer/sizer.dart';
Wrap MaterialApp with Sizer widget
Sizer( 
  builder: (context, orientation, screenType) {
    return MaterialApp(
      home: HomePage(),
    );
  },
);
Widget Size
Container(
  width: Adaptive.w(20),    // This will take 20% of the screen's width
  height: 30.5.h     // This will take 30.5% of the screen's height
)
Font size
Text(
  'Sizer', 
  style: TextStyle(fontSize: 15.dp), 
  // 15.sp can also be used instead of .dp
  // To know their differences, check #FAQ
)
Orientation

If your app needs to work seamlessly in both portrait and landscape orientations

Device.orientation == Orientation.portrait
  ? Container(   // Widget for Portrait
      width: 100.w,
      height: 20.5.h,
   )
  : Container(   // Widget for Landscape
      width: 100.w,
      height: 12.5.h,
   )
ScreenType

Same layout to look different in tablet and mobile, use the Device.screenType method:

Device.screenType == ScreenType.tablet
  ? Container(   // Widget for Tablet
      width: 100.w,
      height: 20.5.h,
   )
  : Container(   // Widget for Mobile
      width: 100.w,
      height: 12.5.h,
   )

*Device.ScreenType can not be Desktop unless maxTabletWidth is set

Guideline

Sizer
  • maxMobileWidth - Maximum width of a mobile device (If the device's width is larger than this, it will be categorized as a tablet) - Default value: 599
  • maxTabletWidth - Maximum width of a tablet device (If the device's width is larger than this, it will be categorized as a desktop) - Optional: enables Desktop ScreenType if enabled
Extensions
  • Adaptive.h() or .h - Calculated percentage of the device's height (40.h -> 40% of device's height)
  • Adaptive.w() or .w - Calculated percentage of the device's width (40.w -> 40% of device's width)
  • Adaptive.sp() or .sp - Calculated sp based on the device's pixel density and aspect ratio (See FAQ)
  • Adaptive.dp() or .dp - Calculated dp based on the device's pixel density (See FAQ)
*Note: Only use .sh and .sw if you want height and width to depend on the device's available height and width after applying SafeArea. Use .h and .w by default.
  • Adaptive.sh() or .sh - Calculated percentage of the remaining device's height after applying SafeArea
  • Adaptive.sw() or .sw - Calculated percentage of the remaining device's width after applying SafeArea

  • Device.boxConstraints - BoxConstraints of the device
  • Device.orientation - Screen Orientation of the device (portrait or landscape)
  • Device.screenType - Screen type of the device (mobile or tablet)
  • Device.aspectRatio - Aspect ratio of the device
  • Device.pixelRatio - Pixel density ratio of the device

  • Adaptive.cm() or .cm - The respective value in value in centimeters
  • Adaptive.mm() or .mm - The respective value in value in millimeters
  • Adaptive.Q() or .Q - The respective value in quarter-millimeters
  • Adaptive.inches() or .inches - The respective value in inches
  • Adaptive.pc() or .pc - The respective value in picas (1/6th of 1 inch)
  • Adaptive.pt() or .pt - The respective value in points (1/72th of 1 inch)
  • Adaptive.px() or .px - The respective value in pixels

Note

You need to import sizer package in order to access number.h, number.w, number.dp, and number.sp

Auto import in VSCode and Android Studio doesn't work for dart extension methods. Typing 10.h would not bring up auto import suggestion for this package

One workaround is to type Device so that the auto import suggestion would show up:

import 'package:sizer/sizer.dart';

FAQ

What is the difference between .sp and .dp?

.dp is supposedly calculated see below based on Material Design's dp calculation while.sp is calculated based on the device's pixel density and aspect ratio

Why is dp not calculated using the device's screen density?

Since there is no way to obtain a device's physical width in inches, we could not calculate for screen density. Material Design's screen density calculation involves a device's physical width in inches. As an alternative, dp is calculated using the device's pixel ratio.

Community Support

If you have any suggestions or issues, feel free to open an issue

If you would like to contribute, feel free to create a PR

所屬分類

原始碼儲存庫

使用此套件的專案