flutter 适配屏幕宽高工具

使用的是flutter插件flutter_screenutil

flutter pub add  flutter_screenutil 

使用

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    //填入设计稿中设备的屏幕尺寸,单位dp
    return ScreenUtilInit(
      designSize: const Size(360, 690),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context , child) {
        return MaterialApp(
			///...
        );
      },
      /// child: const HomePage(title: 'First Method'),
    );
  }
}

工具

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

typedef BackRun = Future<bool> Function();

class UiUtil {
  ///返回顶部状态栏高度
  static double statusBarHeight(BuildContext context) {
    return ScreenUtil().statusBarHeight;
  }

  ///获取系统屏幕宽度
  static double sysW() {
    return w(null);
  }

  ///根据屏幕宽度适配
  static double wMax(double width) {
    return w(width, max: width);
  }

  /// 根据屏幕宽度适配
  /// 不传入参数,则返回原始屏幕宽度
  static double w(num? width, {double? min, double? max}) {
    if (width == null) {
      return ScreenUtil().screenWidth;
    }
    double w = ScreenUtil().setWidth(width);
    if (min != null) {
      return getMax(w, min);
    }
    if (max != null) {
      return getMin(w, max);
    }
    return w;
  }

  /// 获取系统屏幕高度
  static double sysH() {
    return h(null);
  }

  /// 根据屏幕高度适配
  static double hMax(double height) {
    return h(height, max: height);
  }

  /// 根据屏幕高度适配
  static double h(num? height, {double? min, double? max}) {
    if (height == null) {
      return ScreenUtil().screenHeight;
    }
    double h = ScreenUtil().setHeight(height);
    if (min != null) {
      return getMax(h, min);
    }
    if (max != null) {
      return getMin(h, max);
    }
    return h;
  }

  /// 获取系统宽像素
  static double sysX() {
    return sysW() / (ScreenUtil().pixelRatio ?? window.devicePixelRatio);
  }

  /// 获取系统高像素
  static double sysY() {
    return sysH() / (ScreenUtil().pixelRatio ?? window.devicePixelRatio);
  }

  /// 根据屏幕宽度适配宽像素
  static double x(double width, {double? min, double? max}) {
    double x = w(width) / (ScreenUtil().pixelRatio ?? window.devicePixelRatio);
    if (min != null) {
      return getMax(x, min);
    }
    if (max != null) {
      return getMin(x, max);
    }
    return x;
  }

  /// 根据屏幕高度适配宽像素
  static double y(double height, {double? min, double? max}) {
    double y = h(height) / (ScreenUtil().pixelRatio ?? window.devicePixelRatio);
    if (min != null) {
      return getMax(y, min);
    }
    if (max != null) {
      return getMin(y, max);
    }
    return y;
  }

  /// 获取字体大小
  static double sp(double fontSize, {double? min, double? max}) {
    double sp = ScreenUtil().setSp(fontSize);
    if (min != null) {
      return getMax(sp, min);
    }
    if (max != null) {
      return getMin(sp, max);
    }
    return sp;
  }

  /// 空白
  static Widget sizeDivider({double width = 0, double height = 0, Color color = Colors.transparent}) {
    if (height > 0) {
      return Divider(
        height: height,
        color: color,
      );
    } else if (width > 0) {
      return VerticalDivider(
        width: width,
        color: color,
      );
    } else {
      return const Divider(
        height: 0,
      );
    }
  }

  ///构建添加了返回处理的widget
  static Widget buildAddBackWidget(Widget addBackWidget, {BackRun backRun = defBackRun, bool canUseBack = true}) {
    return WillPopScope(onWillPop: backRun, child: addBackWidget is! WillPopScope ? addBackWidget : addBackWidget.child);
  }

  static Future<bool> defBackRun() {
    return Future(() => false);
  }

  static T getMax<T extends num>(T a, T b) {
    return (a.compareTo(b) == 1) ? a : b;
  }

  static T getMin<T extends num>(T a, T b) {
    return (a.compareTo(b) == -1) ? a : b;
  }
}

相关推荐

  1. flutter 屏幕工具

    2024-06-06 13:54:04       35 阅读
  2. Flutter 屏幕之相对尺寸

    2024-06-06 13:54:04       44 阅读
  3. uni-app使用canvas手机进行渲染

    2024-06-06 13:54:04       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 13:54:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 13:54:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 13:54:04       82 阅读
  4. Python语言-面向对象

    2024-06-06 13:54:04       91 阅读

热门阅读

  1. 通过nginx弄一个滑块加图片的人机验证

    2024-06-06 13:54:04       34 阅读
  2. 渗透测试之Web安全系列教程(二)

    2024-06-06 13:54:04       30 阅读
  3. 计算机网络——网络安全

    2024-06-06 13:54:04       33 阅读
  4. 【TB作品】msp430f5529单片机,dht22,烟雾传感器

    2024-06-06 13:54:04       27 阅读
  5. Selenium自动化测试入门:设置等待时间

    2024-06-06 13:54:04       28 阅读