|
| 1 | +/// |
| 2 | +/// [Author] Alex (https://github.com/AlexV525) |
| 3 | +/// [Date] 2020/8/19 10:29 |
| 4 | +/// |
| 5 | +import 'dart:ui' as ui; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter/services.dart'; |
| 9 | + |
| 10 | +/// Screens utils with multiple properties access. |
| 11 | +/// 获取屏幕各项属性的工具类 |
| 12 | +class Screens { |
| 13 | + const Screens._(); |
| 14 | + |
| 15 | + /// Get [MediaQueryData] from [ui.window] |
| 16 | + /// 通过 [ui.window] 获取 [MediaQueryData] |
| 17 | + static MediaQueryData get mediaQuery => MediaQueryData.fromWindow(ui.window); |
| 18 | + |
| 19 | + /// The number of device pixels for each logical pixel. |
| 20 | + /// 设备每个逻辑像素对应的dp比例 |
| 21 | + static double get scale => mediaQuery.devicePixelRatio; |
| 22 | + |
| 23 | + /// The horizontal extent of this size. |
| 24 | + /// 水平范围的大小 |
| 25 | + static double get width => mediaQuery.size.width; |
| 26 | + |
| 27 | + /// The horizontal pixels of this size. |
| 28 | + /// 水平范围的像素值 |
| 29 | + static int get widthPixels => (width * scale).toInt(); |
| 30 | + |
| 31 | + /// The vertical extent of this size. |
| 32 | + /// 垂直范围的大小 |
| 33 | + static double get height => mediaQuery.size.height; |
| 34 | + |
| 35 | + /// The vertical pixels of this size. |
| 36 | + /// 垂直范围的像素值 |
| 37 | + static int get heightPixels => (height * scale).toInt(); |
| 38 | + |
| 39 | + /// Top offset in the [ui.window], usually is the notch size. |
| 40 | + /// 从 [ui.window] 获取的顶部偏移(间距),通常是刘海的大小。 |
| 41 | + static double get topSafeHeight => mediaQuery.padding.top; |
| 42 | + |
| 43 | + /// Bottom offset in the [ui.window], usually is the action bar/navigation bar size. |
| 44 | + /// 从 [ui.window] 获取的底部偏移(间距),通常是操作条/导航条的大小。 |
| 45 | + static double get bottomSafeHeight => mediaQuery.padding.bottom; |
| 46 | + |
| 47 | + /// Height exclude top and bottom safe height. |
| 48 | + /// 去除顶部和底部安全区域的高度 |
| 49 | + static double get safeHeight => height - topSafeHeight - bottomSafeHeight; |
| 50 | + |
| 51 | + /// Method to update status bar's style. |
| 52 | + /// 更新状态栏样式的方法 |
| 53 | + static void updateStatusBarStyle(SystemUiOverlayStyle style) { |
| 54 | + SystemChrome.setSystemUIOverlayStyle(style); |
| 55 | + } |
| 56 | + |
| 57 | + /// Scale factor for the text. |
| 58 | + /// 文字缩放的倍数 |
| 59 | + static double get textScaleFactor => mediaQuery.textScaleFactor; |
| 60 | + |
| 61 | + /// Return a fixed font size according to text scale factor. |
| 62 | + /// 根据文字缩放计算出的固定文字大小 |
| 63 | + static double fixedFontSize(double fontSize) => fontSize / textScaleFactor; |
| 64 | +} |
0 commit comments