使用的是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;
}
}