ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Flutter 第三方 flutter_screenutil(屏幕适配)

Flutter 第三方 flutter_screenutil(屏幕适配) 一直觉得自己写的不是技术而是情怀一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的希望我的这条路能让你们少走弯路希望我能帮你们抹去知识的蒙尘希望我能帮你们理清知识的脉络希望未来技术之巅上有你们也有我。推荐一篇写的非常不过的文章(别人写的文章):Flutter应用框架搭建(二)屏幕适配该SDK的使用,在屏幕上设置了一个宽高的参考值,例如(1242,2208),那么屏幕中约束的控件的设置的数值(例如:414.w),都是按照(1242,2208)进行比例计算的,看下面图片就明白了安装导入flutter_screenutil:^5.9.0import package:flutter_screenutil/flutter_screenutil.dart;举例iPhone 12 mini初始化初始化 -推荐设置方式1page页面单独设置ScreenUtil.init可以在每个页面设置class MyApp extends StatelessWidget{override Widgetbuild(BuildContext context){returnMaterialApp(debugShowCheckedModeBanner:false,title:Flutter_ScreenUtil,theme:ThemeData(primarySwatch:Colors.blue,),home:HomePage(title:FlutterScreenUtil Demo),);}}class HomePage extends StatefulWidget{constHomePage({Key key,this.title}):super(key:key);final String title;override _HomePageStatecreateState()_HomePageState();}class _HomePageState extends StateHomePage{override Widgetbuild(BuildContext context){//设置尺寸填写设计中设备的屏幕尺寸如果设计基于360dp * 690dp的屏幕ScreenUtil.init(context,designSize:constSize(360,690));...}}使用这种方式只需在使用 flutter_screenutil 前进行初始化即可一般放在根路由即第一个页面加载的时候进行初始化。注意ScreenUtil.init 不能在 MyApp 中进行初始化会报如下错误 No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets. 因为这个时候还没加载 MaterialApp 无法使用 MediaQuery.of(context ) 获取到屏幕宽高关于上面两种初始化方法flutter_screenutil 作者推荐使用第二种方式。初始化 - 设置方式2main设置ScreenUtilInitclass MyApp extends StatelessWidget{constMyApp({super.key});override Widgetbuild(BuildContext context){returnScreenUtilInit(designSize:constSize(750,1334),// iphone6 750 1334 iphone 12 mini 1080 2340minTextAdapt:true,splitScreenMode:true,builder:(_,child){returnMaterialApp(debugShowCheckedModeBanner:false,title:First Method,// You can use the library anywhere in the app even in themetheme:ThemeData(primarySwatch:Colors.blue,textTheme:Typography.englishLike2018.apply(fontSizeFactor:1.sp),),// home: child,initialRoute:Home.routeName,routes:{Home.routeName:(context)Home(),},);},// child: Home(),);}}使用doublescreenWidthMediaQuery.of(context).size.width;doublescreenHeightMediaQuery.of(context).size.height;print(系统宽: $screenWidth);//375.0print(系统高: $screenHeight);//812.0doubleutilWidthScreenUtil().screenWidth;doubleutilHeightScreenUtil().screenHeight;print(ScreenUtil宽: $utilWidth);//设备宽度 375.0print(ScreenUtil高: $utilHeight);//设备高度 812.0doublepixelRatioScreenUtil().pixelRatio??0.0;print(pixelRatio $pixelRatio);//设备的像素密度 3.0doublebottomBarHeightScreenUtil().bottomBarHeight;print(bottomBarHeight $bottomBarHeight);//底部安全区距离适用于全面屏下面有按键的 34.0doublestatusBarHeightScreenUtil().statusBarHeight;print(statusBarHeight $statusBarHeight);//状态栏高度 刘海屏会更高 50.0doubletextScaleFactorScreenUtil().textScaleFactor;print(textScaleFactor $textScaleFactor);//系统字体缩放比例 1.0doublescaleWidthScreenUtil().scaleWidth;// 实际宽度设计稿宽度的比例 0.5print(scaleWidth $scaleWidth);doublescaleHeightScreenUtil().scaleHeight;// 实际高度与设计稿高度度的比例 0.6print(scaleHeight $scaleHeight);Orientation orientationScreenUtil().orientation;// 屏幕方向 .portraitprint(orientation $orientation);设置屏幕的Container的宽度是屏幕宽度的一半/// 宽高是宽度的 0.5 倍 , 显示正方形Container(width:0.5.sw,height:0.5.sw,color:Colors.green,),根据宽度进行设置//方式一:ScreenUtil().setWidth(540)//根据屏幕宽度适配尺寸//方式二:540.w根据高度进行设置//方式一:ScreenUtil().setHeight(200)//根据屏幕高度适配尺寸(一般根据宽度适配即可)//方式二:200.h根据宽度或高度中的较小者进行调整//方式一:ScreenUtil().radius(200)//根据宽度或高度中的较小者进行调整//方式二:200.r字体大小适配ScreenUtil().setSp(24)//字体大小适配//方式一:Text(Hello,style:TextStyle(fontSize:ScreenUtil().setSp(24)),),//方式二:Text(Hello,style:TextStyle(fontSize:24.sp),),除了上面 4 种扩展属性以外还提供了sm以及sw、shsm取数值本身与sp的值最小的值如12.sm则取12与12.sp的值进行比较取最小的值。swscreen width 的缩写即屏幕宽度作用是按屏幕宽度比例返回值。如0.2.sw则返回屏幕宽度的 20%1.sw则是整个屏幕宽度shscreen height 的缩写及屏幕高度作用与sw类似返回指定比例的屏幕高度值。如1.sh为整个屏幕高度注意如果使用sp去设置字体,那么字体会根据屏幕的宽高缩放字体的,如果需要设置成,所有屏幕的字体大小都一样的可以像下面这么设计:全局设置MaterialApp(debugShowCheckedModeBanner:false,title:Flutter_ScreenUtil,theme:ThemeData(primarySwatch:Colors.blue,),builder:(context,widget){returnMediaQuery(///设置文字大小不随系统设置改变data:MediaQuery.of(context).copyWith(textScaleFactor:1.0),child:widget,);},home:HomePage(title:FlutterScreenUtil Demo),),Text 单独设置Text(text,textScaleFactor:1.0)其他 Api除了适配的 api 以外flutter_screenutil 还提供了很多实用的 api 如下 ScreenUtil().pixelRatio设备的像素密度ScreenUtil().screenWidth屏幕宽度等同于 1.swScreenUtil().screenHeight屏幕高度等同于 1.shScreenUtil().bottomBarHeight底部导航高度如全屏底部按键的高度ScreenUtil().statusBarHeight状态栏高度ScreenUtil().textScaleFactor系统字体缩放比例ScreenUtil().scaleWidth实际宽度与设计图宽度的比例ScreenUtil().scaleHeight实际高度与设计图高度的比例ScreenUtil().orientation屏幕方向提问安卓,iPhone的机型这么多,如果写死一个iPhone 12 min的屏幕宽高,这么机型约束出来的效果会变形吗?答案: 不会开发中我们会填写一个机型的宽高,这个数据从哪里来呢?是来自蓝湖上产品用的是那个机型:注意下面两句话:1.我们设计下面的这个代码只是计算出一个比例因子2.关键在于 flutter_screenutil 默认只按宽度缩放,高度最终跟随宽度计算相应的比例.至于内容的高度是可以滚动嘛ScreenUtil.init(context, designSize: const Size(402,875));所以我们开发的时候怎么写呢?1.你就去蓝湖看屏幕的宽高是多少,直接填写进去就像上面的图片2.然后开发的时候,蓝湖的空间宽高是多少, 代码空间一样写对应的大小的好了,看下面的图片
返回列表