| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import 'package:flutter/material.dart';
- import 'dart:ui' as ui show window;
- /**
- * @Author: thl
- * @GitHub: https://github.com/Sky24n
- * @Email: 863764940@qq.com
- * @Email: sky24no@gmail.com
- * @Description: Screen Util.
- * @Date: 2018/9/8
- */
- ///默认设计稿尺寸(单位 dp or pt)
- double _designW = 360.0;
- double _designH = 640.0;
- double _designD = 3.0;
- /**
- * 配置设计稿尺寸(单位 dp or pt)
- * w 宽
- * h 高
- * density 像素密度
- */
- /// 配置设计稿尺寸 屏幕 宽,高,密度。
- /// Configuration design draft size screen width, height, density.
- void setDesignWHD(double w, double h, {double density: 3.0}) {
- _designW = w;
- _designH = h;
- _designD = density;
- }
- /// Screen Util.
- class ScreenUtil {
- double _screenWidth = 0.0;
- double _screenHeight = 0.0;
- double _screenDensity = 0.0;
- double _statusBarHeight = 0.0;
- double _bottomBarHeight = 0.0;
- double _appBarHeight = 0.0;
- MediaQueryData _mediaQueryData;
- static final ScreenUtil _singleton = ScreenUtil();
- static ScreenUtil getInstance() {
- _singleton._init();
- return _singleton;
- }
- _init() {
- MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
- if (_mediaQueryData != mediaQuery) {
- _mediaQueryData = mediaQuery;
- _screenWidth = mediaQuery.size.width;
- _screenHeight = mediaQuery.size.height;
- _screenDensity = mediaQuery.devicePixelRatio;
- _statusBarHeight = mediaQuery.padding.top;
- _bottomBarHeight = mediaQuery.padding.bottom;
- _appBarHeight = kToolbarHeight;
- }
- }
- /// screen width
- /// 屏幕 宽
- double get screenWidth => _screenWidth;
- /// screen height
- /// 屏幕 高
- double get screenHeight => _screenHeight;
- /// appBar height
- /// appBar 高
- double get appBarHeight => _appBarHeight;
- /// screen density
- /// 屏幕 像素密度
- double get screenDensity => _screenDensity;
- /// status bar Height
- /// 状态栏高度
- double get statusBarHeight => _statusBarHeight;
- /// bottom bar Height
- double get bottomBarHeight => _bottomBarHeight;
- /// media Query Data
- MediaQueryData get mediaQueryData => _mediaQueryData;
- /// screen width
- /// 当前屏幕 宽
- static double getScreenW(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery.size.width;
- }
- /// screen height
- /// 当前屏幕 高
- static double getScreenH(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery.size.height;
- }
- /// screen density
- /// 当前屏幕 像素密度
- static double getScreenDensity(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery.devicePixelRatio;
- }
- /// status bar Height
- /// 当前状态栏高度
- static double getStatusBarH(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery.padding.top;
- }
- /// status bar Height
- /// 当前BottomBar高度
- static double getBottomBarH(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery.padding.bottom;
- }
- /// 当前MediaQueryData
- static MediaQueryData getMediaQueryData(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery;
- }
- /// returns the size after adaptation according to the screen width.(unit dp or pt)
- /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
- /// size 单位 dp or pt
- static double getScaleW(BuildContext context, double size) {
- if (context == null || getScreenW(context) == 0.0) return size;
- return size * getScreenW(context) / _designW;
- }
- /// returns the size after adaptation according to the screen height.(unit dp or pt)
- /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
- /// size unit dp or pt
- static double getScaleH(BuildContext context, double size) {
- if (context == null || getScreenH(context) == 0.0) return size;
- return size * getScreenH(context) / _designH;
- }
- /// returns the font size after adaptation according to the screen density.
- /// 返回根据屏幕宽适配后字体尺寸
- /// fontSize 字体尺寸
- static double getScaleSp(BuildContext context, double fontSize) {
- if (context == null || getScreenDensity(context) == 0.0) return fontSize;
- return fontSize * getScreenW(context) / _designW;
- }
- /// Orientation
- /// 设备方向(portrait, landscape)
- static Orientation getOrientation(BuildContext context) {
- MediaQueryData mediaQuery = MediaQuery.of(context);
- return mediaQuery.orientation;
- }
- /// returns the size after adaptation according to the screen width.(unit dp or pt)
- /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
- /// size 单位 dp or pt
- double getWidth(double size) {
- return _screenWidth == 0.0 ? size : (size * _screenWidth / _designW);
- }
- /// returns the size after adaptation according to the screen height.(unit dp or pt)
- /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
- /// size unit dp or pt
- double getHeight(double size) {
- return _screenHeight == 0.0 ? size : (size * _screenHeight / _designH);
- }
- /// returns the size after adaptation according to the screen width.(unit dp or pt)
- /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
- /// sizePx unit px
- double getWidthPx(double sizePx) {
- return _screenWidth == 0.0
- ? (sizePx / _designD)
- : (sizePx * _screenWidth / (_designW * _designD));
- }
- /// returns the size after adaptation according to the screen height.(unit dp or pt)
- /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
- /// sizePx unit px
- double getHeightPx(double sizePx) {
- return _screenHeight == 0.0
- ? (sizePx / _designD)
- : (sizePx * _screenHeight / (_designH * _designD));
- }
- /// returns the font size after adaptation according to the screen density.
- /// 返回根据屏幕宽适配后字体尺寸
- /// fontSize 字体尺寸
- double getSp(double fontSize) {
- if (_screenDensity == 0.0) return fontSize;
- return fontSize * _screenWidth / _designW;
- }
- }
|