搜索插件、代码仓与 APP 分类

responsive_sizer 封面
插件详情v3.3.1

responsive_sizer

Responsive Sizer 通过提供辅助小部件和扩展来帮助实现响应式布局。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.6万
喜欢数
596
Pub 分数
160

安装插件

flutter pub add responsive_sizer

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Responsive Sizer 是一个 Flutter 包,可简化在不同屏幕尺寸和方向上创建响应式布局的过程。它提供了辅助小部件和扩展方法,可轻松根据设备的屏幕尺寸缩放字体、大小和间距。通过使用逻辑单位如 `vs`(垂直尺寸)和 `hs`(水平尺寸),开发人员可以确保在各种设备上保持一致的 UI 外观。该包可无缝集成到现有的 Flutter 项目中,并支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 构建跨设备的响应式 UI
  • 动态缩放文本和布局元素
  • 为多种屏幕尺寸创建自适应设计
  • 确保跨设备的一致间距
  • 简化响应式设计,无需复杂计算

主要特点

  • 易于使用的扩展方法
  • 支持 vs 和 hs 单位
  • 内置的响应式小部件
  • 跨平台兼容性
  • 最小的样板代码
  • 自动屏幕尺寸检测

适合对象

  • 开发跨平台应用的开发者
  • 注重响应式设计的团队
  • 需要动态扩展的项目
  • 初学者学习响应式Flutter
  • 针对各种设备屏幕的应用程序

使用建议

  • 可能需要对现有布局值进行初始调整
  • 最好与一致的设计系统一起使用
  • 不太适合高度自定义或非均匀缩放的需求
  • 性能影响可以忽略不计,但应在低端设备上进行测试
  • 需要理解响应式设计原则

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Responsive Sizer

Responsive Sizer helps implement a responsive layout by providing helper widgets and extensions.

Responsive Image

Responsive Image

Content

Installation

Add responsive_sizer to pubspec.yaml

dependencies:
  responsive_sizer: ^3.3.1

Usage

Import the Package

import 'package:responsive_sizer/responsive_sizer.dart';

Wrap MaterialApp with ResponsiveSizer widget

ResponsiveSizer( 
  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(
  'Responsive Sizer', 
  style: TextStyle(fontSize: 15.dp), 
  // 15.sp can also be used instead of .dp
  // To know their differences, check #FAQ
)

Orientation

If you want to support 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

If you want the 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

Parameters

ResponsiveSizer

  • 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

Take Note

You need to import responsive_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:responsive_sizer/responsive_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

所属分类

源码仓库

使用此插件的项目