getDimensions function

VoltronMap getDimensions(
  1. int windowWidth,
  2. int windowHeight,
  3. bool shouldUseScreenDisplay,
  4. BuildContext? context,
)

Implementation

VoltronMap getDimensions(
  int windowWidth,
  int windowHeight,
  bool shouldUseScreenDisplay,
  BuildContext? context,
) {
  var dimensionMap = VoltronMap();
  var windowDisplayMetricsMap = VoltronMap();

  final screenDensity = ScreenUtil.getInstance().scale;
  final screenWidthPixels = (ScreenUtil.getInstance().screenWidth).floor();
  final screenHeightPixels = (ScreenUtil.getInstance().screenHeight).floor();
  // 状态栏高度这里需要传入像素值
  var statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
  var bottomBarHeight = ScreenUtil.getInstance().navigationBarHeight;

  var screenDisplayMetricsMap = VoltronMap();
  screenDisplayMetricsMap.push("width", screenWidthPixels);
  screenDisplayMetricsMap.push("height", screenHeightPixels);
  screenDisplayMetricsMap.push("scale", screenDensity);
  screenDisplayMetricsMap.push("fontScale", screenDensity);
  screenDisplayMetricsMap.push("statusBarHeight", statusBarHeight);
  screenDisplayMetricsMap.push("navigatorBarHeight", bottomBarHeight);

  dimensionMap.push("screenPhysicalPixels", screenDisplayMetricsMap);

  if (!shouldUseScreenDisplay && context != null) {
    final density = ScreenUtil.getScreenDensity(context);
    final widthPixels = ScreenUtil.getScreenW(context);
    final heightPixels = ScreenUtil.getScreenH(context);

    windowDisplayMetricsMap.push("width", windowWidth >= 0 ? windowWidth : widthPixels);
    windowDisplayMetricsMap.push("height", windowHeight >= 0 ? windowHeight : heightPixels);
    windowDisplayMetricsMap.push("scale", density);
    windowDisplayMetricsMap.push("fontScale", screenDensity);
    windowDisplayMetricsMap.push("statusBarHeight", statusBarHeight);
    windowDisplayMetricsMap.push("navigatorBarHeight", bottomBarHeight);
  } else {
    windowDisplayMetricsMap.push("width", windowWidth >= 0 ? windowWidth : screenWidthPixels);
    windowDisplayMetricsMap.push("height", windowHeight >= 0 ? windowHeight : screenHeightPixels);
    windowDisplayMetricsMap.push("scale", screenDensity);
    windowDisplayMetricsMap.push("fontScale", screenDensity);
    windowDisplayMetricsMap.push("statusBarHeight", statusBarHeight);
    windowDisplayMetricsMap.push("navigatorBarHeight", bottomBarHeight);
  }

  dimensionMap.push("windowPhysicalPixels", windowDisplayMetricsMap);

  return dimensionMap;
}