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. 可以看看官方文档的说明,这里不再写了。
相关推荐
AsiaSun.几秒前
《苍穹外卖》学习笔记
笔记·学习
爸爸6192 分钟前
Flutter StatusBar Color NS 在鸿蒙平台的使用指南
flutter·华为·harmonyos
iconball4 分钟前
个人用云计算学习笔记 --34华为 OceanStor 仿真器部署与基础使用指南
运维·笔记·学习·云计算
xian_wwq4 分钟前
【学习笔记】AI的边界
人工智能·笔记·学习
程序员老刘·5 分钟前
Flutter版本选择指南:3.38.5 补丁发布,生产环境能上了吗? | 2025年12月
flutter
w139548564225 分钟前
Flutter跨平台照片搜索库desktop_photo_search鸿蒙化使用指南
flutter·华为·harmonyos
AC赳赳老秦1 小时前
DeepSeek教育科技应用:智能生成个性化学习规划与知识点拆解教程
前端·网络·数据库·人工智能·学习·matplotlib·deepseek
后端小张2 小时前
【AI学习】深入探秘AI之神经网络的奥秘
人工智能·深度学习·神经网络·opencv·学习·机器学习·自然语言处理
d111111111d3 小时前
STM32 GPIO输出模式配置详解:从寄存器层面理解引脚控制
笔记·stm32·单片机·嵌入式硬件·学习
曾浩轩3 小时前
跟着江协科技学STM32之4-1OLED调试工具
科技·stm32·单片机·学习