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

flutter_screen_scaler 封面
套件詳情v3.0.0

flutter_screen_scaler

一個根據螢幕百分比縮放元件和字體大小的套件,並附有方便的擴展方法(例如 `50.h`、`30.w`、`16.sp`)。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
124
喜歡數
27
Pub 分數
160

安裝套件

flutter pub add flutter_screen_scaler

套件介紹

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

flutter_screen_scaler 是一個響應式 UI 套件,透過百分比擴展(例如 `50.h`、`30.w` 和 `16.sp`)根據螢幕尺寸實現動態縮放,自動調整元件與字型大小。它簡化了在不同裝置上建立適應性佈局的過程,根據螢幕高度、寬度和字型比例自動調整尺寸。此套件可無縫整合至 Flutter 的元件樹中,並支援所有主要平台,包含 Android、iOS、Web、macOS、Windows 與 Linux。非常適合希望在各種螢幕尺寸上維持一致設計而無需手動計算的開發者。

適用情境

  • 為多種螢幕尺寸建立響應式版面
  • 根據設備密度動態縮放字體
  • 使用統一的UI尺寸構建跨平台應用
  • 簡化複雜UI中的佈局計算
  • 確保在各種設備上保持一致的間距和尺寸

主要特色

  • 基於百分比的高度和寬度縮放(例如 50.h)
  • 透過 sp 單位(例如 16.sp)縮放字體大小
  • 擴展方法,用於直觀的語法
  • 跨平台支援
  • 自動螢幕尺寸檢測

適合對象

  • 開發響應式 Flutter 應用的開發者
  • 針對多種設備類型的團隊
  • 需要最少手動佈局調整的項目
  • 專注於一致性的UI設計師
  • 需要可擴展排版的應用程式

使用建議

  • 可能需要初始設定以實現最佳縮放
  • 行為取決於準確的螢幕度量
  • 不適合固定大小的設計
  • 過度使用可能導致效能影響
  • 最佳搭配響應式設計原則使用

原始 README

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

Flutter Screen Scaler

A Flutter package to effortlessly scale widget dimensions and font sizes based on screen percentages, now with convenient extension methods like 50.h, 30.w, and 16.sp.

This package helps you create responsive UIs that adapt to various screen sizes by simplifying the process of using screen-relative dimensions.

Features

  • Singleton Access: Easy-to-use static instance ScreenScaler.I.
  • Static Initialization: Simple one-time setup with ScreenScaler.init(context).
  • Extension Methods: Intuitive syntax for scaling:
    • value.h: Percentage of screen height.
    • value.w: Percentage of screen width.
    • value.sp: Responsive font size, scales linearly with screen width.
  • Direct Percentage Scaling: getWidth() and getHeight() now provide straightforward percentage-based values.
  • Parent-Relative Scaling: Scale dimensions based on a percentage of an immediate parent widget's size using scaleWidthFrom(parentSize, percentage) and scaleHeightFrom(parentSize, percentage).
  • Utility Methods: Includes helpers for EdgeInsets (padding, margins) and BorderRadius.

Installation

  1. Add this to your package's pubspec.yaml file:

    dependencies:
      flutter_screen_scaler: ^3.0.0
    
  2. Install packages from the command line:

    flutter pub get
    
  3. Import it in your Dart code:

    import 'package:flutter_screen_scaler/flutter_screen_scaler.dart';
    

Initialization (Important!)

Before using any screen-relative scaling methods or extension getters (.h, .w, .sp), you must initialize the ScreenScaler with a BuildContext. This is typically done once in your app, for example, in the build method of your main app widget or a root page.

@override
Widget build(BuildContext context) {
  // Initialize the ScreenScaler
  ScreenScaler.init(context);

  return MaterialApp(
    // ... your app code
  );
}

If ScreenScaler.init(context) is not called, a debug warning will be printed, and the scaler will operate with default (potentially incorrect) screen dimensions.

Usage Examples

Once initialized, you can use the extension methods or call methods on ScreenScaler.I.

1. Screen Percentage Scaling (Height and Width)

Use the .h and .w extension getters on any num (int or double) for concise scaling.

// In your widget's build method (after ScreenScaler.init has been called elsewhere)
Container(
  height: 50.h,   // 50% of screen height
  width: 80.w,    // 80% of screen width
  color: Colors.blue,
  child: Center(child: Text('Scaled Container')),
);
2. Responsive Font Scaling

Use the .sp extension getter for font sizes that scale linearly with screen width.

Text(
  'Responsive Text',
  style: TextStyle(
    fontSize: 16.sp, // Font size 16, scaled for screen width
  ),
);
3. Direct Singleton Usage (Alternative)

You can also call methods directly on the ScreenScaler.I instance:

Container(
  height: ScreenScaler.I.getHeight(50),   // 50% of screen height
  width: ScreenScaler.I.getWidth(80),     // 80% of screen width
  child: Text(
    'More Text',
    style: TextStyle(
      fontSize: ScreenScaler.I.getResponsiveFontSize(16), // Scaled font size 16
    ),
  ),
);
4. Parent-Relative Scaling

To scale a widget based on its parent's dimensions, use scaleWidthFrom or scaleHeightFrom within a LayoutBuilder.

LayoutBuilder(
  builder: (BuildContext context, BoxConstraints constraints) {
    Size parentSize = constraints.biggest; // Get parent's size

    return Container(
      width: ScreenScaler.I.scaleWidthFrom(parentSize, 50), // 50% of parent's width
      height: ScreenScaler.I.scaleHeightFrom(parentSize, 30), // 30% of parent's height
      color: Colors.green,
      child: Center(child: Text('Scales with parent')),
    );
  },
)
5. Padding, Margins, and Border Radius

The package continues to provide utility methods for creating scaled EdgeInsets and BorderRadius values. These methods internally use the core screen scaling logic.

// Example using screen-relative scaling
Container(
  padding: ScreenScaler.I.getPaddingAll(5.w), // Padding of 5% of screen width on all sides
  margin: ScreenScaler.I.getMarginLTRB(2.w, 1.h, 2.w, 1.h),
  decoration: BoxDecoration(
    color: Colors.lightBlueAccent,
    borderRadius: ScreenScaler.I.getBorderRadiusCircular(10.sp), // Radius scaled using .sp logic
  ),
  child: Text('Styled Container'),
);
Note on getTextSize and getFullScreen

The methods getTextSize(percentage) (alias for getFullScreen(percentage)) are still available. However, please note that getFullScreen calculates its value using the formula (percentage / 100)^2 * (screenWidth + screenHeight). This results in a quadratic scaling for the percentage, which might not be intuitive for typical font size scaling. For standard linear font scaling, prefer using the .sp extension or ScreenScaler.I.getResponsiveFontSize().

Why Use This Package?

When developing Flutter applications, you often define widget sizes explicitly. While this works on your test device, these fixed sizes can lead to UI overflows or awkward layouts on devices with different screen dimensions.

flutter_screen_scaler solves this by allowing you to define sizes and font scales as percentages of the screen dimensions (or parent dimensions). This approach, similar to using percentages in web development, helps create UIs that are more adaptive and look consistent across a wider range of devices.

Example

Refer to the updated example/lib/main.dart for a comprehensive demonstration of the new API and features.

screenshot

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。