使用第三方依赖 flutter_screenutil
使用
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() {
runApp(
ScreenUtilInit( // 嵌套主组件
designSize: const Size(1194, 834), // 设计稿的宽度和高度
minTextAdapt: true,
splitScreenMode: true,
builder:(context,child){
return GetMaterialApp(
title: "Application",
defaultTransition:Transition.rightToLeft,
initialRoute: AppPages.INITIAL,
getPages: AppPages.routes,
);
}
)
);
}
在组件中使用
import 'package:flutter_screenutil/flutter_screenutil.dart';
class MyScreenUtil {
static width(num val){return val.w;}
static height(num val){return val.h;}
static fontSize(num val){return val.sp;}
static radius(num val){return val.r;}
static getScreenWidth(){return 1.sw;}// 获取屏幕高度
static getScreenHeight(){return 1.sh;} // 获取屏幕宽度
static getStatusBarHeight(){
return ScreenUtil().statusBarHeight;
}
}