Flutter_学习记录_各个屏幕的适配

用flutter的这个库,可以解决:https://pub.dev/packages/flutter_screenutil

使用方法:

  1. 在pubspec.yaml文件中,添加库,如下图:
  2. 在main.dart中导入头文件
dart 复制代码
import 'package:flutter_screenutil/flutter_screenutil.dart';
  1. 在 Widget build(BuildContext context)方法中,初始化ScreenUtilInit, 代码如下:
dart 复制代码
class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(750, 1334),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (_, child) {
        return MaterialApp(
        // home: Tabs()
          initialRoute: '/',
          onGenerateRoute: onGenerateRoute,
        );
      },
    );
  }
}
  1. 使用方法说明
dart 复制代码
ScreenUtil().setWidth(540)  (dart sdk>=2.6 : 540.w) //Adapted to screen width
    ScreenUtil().setHeight(200) (dart sdk>=2.6 : 200.h) //Adapted to screen height , under normal circumstances, the height still uses x.w
    ScreenUtil().radius(200)    (dart sdk>=2.6 : 200.r)    //Adapt according to the smaller of width or height
    ScreenUtil().setSp(24)      (dart sdk>=2.6 : 24.sp) //Adapter font
    12.sm   //return min(12,12.sp)

    ScreenUtil().pixelRatio       //Device pixel density
    ScreenUtil().screenWidth   (dart sdk>=2.6 : 1.sw)    //Device width
    ScreenUtil().screenHeight  (dart sdk>=2.6 : 1.sh)    //Device height
    ScreenUtil().bottomBarHeight  //Bottom safe zone distance, suitable for buttons with full screen
    ScreenUtil().statusBarHeight  //Status bar height , Notch will be higher
    ScreenUtil().textScaleFactor  //System font scaling factor

    ScreenUtil().scaleWidth //The ratio of actual width to UI design
    ScreenUtil().scaleHeight //The ratio of actual height to UI design

    ScreenUtil().orientation  //Screen orientation
    0.2.sw  //0.2 times the screen width
    0.5.sh  //50% of screen height
    20.setVerticalSpacing  // SizedBox(height: 20 * scaleHeight)
    20.horizontalSpace  // SizedBox(height: 20 * scaleWidth)
    const RPadding.all(8)   // Padding.all(8.r) - take advantage of const key word
    EdgeInsets.all(10).w    //EdgeInsets.all(10.w)
    REdgeInsets.all(8)       // EdgeInsets.all(8.r)
    EdgeInsets.only(left:8,right:8).r // EdgeInsets.only(left:8.r,right:8.r).
    BoxConstraints(maxWidth: 100, minHeight: 100).w    //BoxConstraints(maxWidth: 100.w, minHeight: 100.w)
    Radius.circular(16).w          //Radius.circular(16.w)
    BorderRadius.all(Radius.circular(16)).w
  1. 可以看看官方文档的说明,这里不再写了。
相关推荐
和侧5 小时前
离子注入后为什么必须配套退火工艺?
笔记·学习
凛铄linshuo6 小时前
自动化测试:pytest第一个实例学习
学习·pytest
我命由我123457 小时前
智能助手通用问答边界
人工智能·python·学习·ai·学习方法·ai编程·智能体
鬼手点金8 小时前
Agent 的学习机制:后训练、上下文学习与外部化学习
java·人工智能·git·学习·自动化·nanogpt
几个几个n8 小时前
LaTeX 入门学习
学习
想七想八不如114089 小时前
元学习简单入门
人工智能·pytorch·python·深度学习·学习·机器学习
Awh-10 小时前
ARM 裸机开发 DAY11 学习笔记
arm开发·笔记·学习
2603_9695792812 小时前
U盘的系统备份怎样还原系统?详细步骤与注意事项
学习
知识分享小能手13 小时前
C学习教程,从入门到精通,C语言指针 —— 详细知识点总结(10)
c语言·开发语言·学习
Meteors.13 小时前
Android Compose学习02:基础组件与 Modifier
android·学习