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

插件详情v1.0.0+4

sized_context

直接在上下文中访问 MediaQuery 的尺寸信息,还添加了一些用于尺寸和布局的辅助方法。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
60
喜欢数
195
Pub 分数
160

安装插件

flutter pub add sized_context

插件介绍

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

sized_context 包通过上下文提供对 MediaQuery 尺寸信息的直接访问,简化了响应式 UI 的开发。它扩展了 BuildContext,提供了有用的辅助方法,可根据屏幕度量计算尺寸、间距和布局维度。这消除了在组件树中反复调用 MediaQuery.of(context) 的需要。非常适合在不同设备尺寸和方向上构建可适应的布局。

适合场景

  • 响应式布局设计
  • 跨平台 UI 适配
  • 动态小部件大小调整
  • 简化 MediaQuery 的使用
  • 设备间的一致间距

主要特点

  • 基于上下文的直接尺寸
  • 内置的大小计算辅助工具
  • 减少样板代码
  • 支持所有 Flutter 平台
  • 与现有小部件的轻松集成

适合对象

  • 开发响应式应用的开发者
  • 注重UI一致性的团队
  • 针对多个平台的项目
  • 需要动态布局的应用程序

使用建议

  • 需要上下文访问
  • 如果过度使用,可能会增加构建复杂性
  • 对于简单的静态布局不需要
  • 仅限尺寸相关功能
  • 最好与其他布局包配合使用

原始 README

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

✥ Sized Context - Easy responsive sizing

With this extension package you can easily access the MediaQuery sizing info directly on the build context:

Size size = context.sizePx;

It also provides additional convenience methods like landscape state, diagonal screen size, inch-based measurements, screen type and percentage values:

bool isLandscape = context.isLandscape; //Instead of: MediaQuery.of(context).orientation == Orientation.landscape
bool isTablet = context.diagonalInches >= 7; //Get physical device size in inches 
bool useSingleColumn = context.widthPx < 400; //Access .width and .height directly, no need to go through .size
double sidePadding = context.widthPercent(0.1); //Use percentages of width or height for sizing

🔨 Installation

dependencies:
  sized_context: ^1.0.0+1
⚙ Import
import 'package:sized_context/sized_context.dart';

🕹️ Usage

bool isLandscape = context.isLandscape;
//PIXELS
Size size = context.sizePx;
double width = context.widthPx;
double height = context.heightPx;
double diagonalPx = context.diagonalPx;
//INCHES
Size sizeInInches = context.sizeInches;
double widthInInches = context.widthInches;
double heightInInches = context.heightInches;
double diagonalInInches = context.diagonalInches;
//PERCENTAGE
double widthPercent = context.widthPct(.1);
double heightPercent = context.heightPct(.25);

For convenience you can also access the MediaQueryData object directly, to get any other methods or properties:

EdgeInsets padding = context.mq.padding;
Size safeSize = context.mq.removePadding();

🐞 Bugs/Requests

If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.

👏 Thanks

  • Thanks to reddit user der_kobold for the idea to turn this into an extension!

📃 License

MIT License

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目