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

插件详情v1.0.5

flutter_sizer

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
942
喜欢数
24
Pub 分数
150

安装插件

flutter pub add flutter_sizer

插件介绍

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

Flutter Sizer 是一个响应式 UI 包,可在不同屏幕尺寸和方向上实现自适应布局。它提供辅助小部件和扩展功能,可轻松根据设备的屏幕尺寸缩放字体、大小和边距。通过利用屏幕的宽度和高度,它确保在各种设备上保持一致的设计外观,而无需手动计算。非常适合希望创建流畅、可扩展界面且代码量最少的开发者。

适合场景

  • 为多种屏幕尺寸创建响应式布局
  • 在设备间按比例缩放UI元素
  • 简化移动和网络应用中的响应式设计
  • 保持一致的间距和排版
  • 使用统一外观构建跨平台应用
  • 减少UI代码中的手动尺寸计算

主要特点

  • 根据屏幕尺寸自动调整小部件的大小
  • 用于轻松单位转换的辅助扩展
  • 支持竖屏和横屏模式
  • 仅需几行代码的最小设置
  • 跨平台兼容性
  • 对 Android、iOS、Web、macOS、Windows 和 Linux 的内置支持

适合对象

  • 开发响应式 Flutter 应用程序的开发者
  • 优先考虑跨设备的一致性用户界面
  • 需要快速实现响应式布局的项目
  • 针对具有不同屏幕尺寸的多个平台的应用程序
  • 设计师与开发人员紧密合作,实现像素级精确的布局
  • 初学者学习响应式 Flutter 模式

使用建议

  • 使用自定义布局约束时可能需要进行调整
  • 不适合高度复杂或非均匀布局
  • 过度嵌套小部件可能会影响性能
  • 需要在多种屏幕尺寸上进行仔细测试
  • 最好与一致的设计系统一起使用
  • 高级缩放逻辑的自定义功能有限

原始 README

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

Flutter Sizer

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

Responsive Image

Responsive Image

Content

Installation

Add flutter_sizer to pubspec.yaml

dependencies:
  flutter_sizer: ^1.0.4

Parameters

  • Adaptive.h() - Returns a calculated height based on the device
  • Adaptive.w() - Returns a calculated width based on the device
  • Adaptive.sp() - Returns a calculated sp based on the device (deprecated)
  • Adaptive.dp() - Returns a calculated dp based on the device
  • .h - Returns a calculated height based on the device
  • .w - Returns a calculated width based on the device
  • .sp - Returns a calculated sp based on the device (deprecated)
  • .dp - Returns a calculated dp based on the device
  • Device.boxConstraints - Returns the Device's BoxConstraints
  • Device.orientation - Returns the Screen Orientation (portrait or landscape)
  • Device.screenType - Returns the Screen Type (mobile or tablet)
  • Device.devicePixelRatio - Returns the devicePixel Ratio or (1.0)

Usage

Import the Package

import 'package:flutter_sizer/flutter_sizer.dart';

Wrap MaterialApp with FlutterSizer widget

 FlutterSizer(
      builder: (context, orientation, screenType) {
        return MaterialApp();
      }
 )

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(
  'Flutter Sizer', 
  style: TextStyle(fontSize: 15.dp),
)

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,
   )

DeviceType

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,
   )

Take Note

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

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:flutter_sizer/flutter_sizer.dart';

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

所属分类

源码仓库

使用此插件的项目

暂无相关数据。