• 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;
      }
    }
    
    
  • 相关阅读:
    Autoware.universe部署06:使用DBC文件进行UDP的CAN通信代码编写
    Android LiveData Cannot add the same observer with different lifecycles
    JDK多版本切换
    如何在外网访问公司项目?快解析实现内网ip让公网连接
    设置Excel表格“只读模式”的两种方法
    spring cloud笔记--微服务基础
    基于JavaWEB和MySQL的精品课程网站设计与实现
    java家乡旅游文化推广网站计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    LeetCode经典面试150题-day4(删除有序数组的重复项II)
    vue---十分钟搞懂vue计算属性
  • 原文地址:https://blog.csdn.net/weixin_44728369/article/details/139447716