• 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;
      }
    }
    
    
  • 相关阅读:
    虚拟化 vs. 裸金属:K8s 部署环境架构与特性对比
    SpringCloud微服务实战——搭建企业级开发框架(三十八):搭建ELK日志采集与分析系统
    Object构造函数的方法
    Camunda 7.x 系列【43】事务子流程
    springboot RestTemplate优化 http 池化
    抓包工具fiddler的基础知识
    JAVA异常机制
    OpenMMLab MMYOLO目标检测环境搭建(一)
    方法与递归(JAVA基础一)
    Android 图像显示系统 - 基础知识之 BitTube
  • 原文地址:https://blog.csdn.net/weixin_44728369/article/details/139447716