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;
  }
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值