【Flutter 必备插件】屏幕适配方案 flutter_screenutil

flutter 屏幕适配方案,用于调整屏幕和字体大小的 flutter 插件,让你的 UI 在不同尺寸的屏幕上都能显示合理的布局!

初始化设计稿尺寸

main.dart

less 复制代码
	@override
  Widget build(BuildContext context) {

    return ScreenUtilInit(
      designSize: const Size(375, 812), // 设计稿的尺寸
      minTextAdapt: true, // 文字适配,防止字体过小
      builder: (context, child) {
        return MaterialApp.router(
          routerConfig: AppRouter.router,
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFFFF9900)),
            useMaterial3: true,
          ),
        );
      }
    );
  }

在代码中使用适配单位

1. 基本单位

  • .w 根据屏幕宽度适配
  • .h 根据屏幕高度适配
  • .r 根据宽/高中较小者适配(常用于圆角)
less 复制代码
Container(
  width: 200.w,    // 相当于设计稿中 200 的宽度
  height: 100.h,   // 相当于设计稿中 100 的高度
  decoration: BoxDecoration(
    borderRadius: BorderRadius.circular(10.r), // 自适应圆角
  ),
);

2. 字体适配

  • .sp 根据屏幕宽度或高度自适应文本(推荐优先使用宽度)
less 复制代码
Text(
  'Hello ScreenUtil',
  style: TextStyle(fontSize: 24.sp),
);
  • 设置字体不随系统字体大小进行改变

    APP 全局:

less 复制代码
	@override
  Widget build(BuildContext context) {

    return ScreenUtilInit(
      designSize: const Size(375, 812), // 设计稿的尺寸
      minTextAdapt: true, // 文字适配,防止字体过小
      builder: (context, child) {
        return MaterialApp.router(
          routerConfig: AppRouter.router,
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFFFF9900)),
            useMaterial3: true,
          ),
          builder: (context, widget) {
            return MediaQuery(
            ///设置文字大小不随系统设置改变
            data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(1.0)),
            child: widget!,
            );
          }
        );
      }
    );
  }

3. API

scss 复制代码
ScreenUtil().screenWidth;  // 屏幕宽度
ScreenUtil().screenHeight; // 屏幕高度
ScreenUtil().pixelRatio;   // 设备像素密度
ScreenUtil.bottomBarHeight  //底部安全区距离,适用于全面屏下面有按键的
ScreenUtil.statusBarHeight  //状态栏高度 刘海屏会更高
ScreenUtil.textScaleFactor //系统字体缩放比例

ScreenUtil().scaleWidth  // 实际宽度设计稿宽度的比例
ScreenUtil().scaleHeight // 实际高度与设计稿高度度的比例

ScreenUtil().orientation  //屏幕方向

0.2.sw  //屏幕宽度的0.2倍
0.5.sh  //屏幕高度的50%
20.setVerticalSpacing  // SizedBox(height: 20 * scaleHeight)
20.horizontalSpace  // SizedBox(height: 20 * scaleWidth)
const RPadding.all(8)   // Padding.all(8.r) - 获取到const的优点
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  
相关推荐
穆梓兰煊14 小时前
Flutter端云协同推理架构解析
flutter·github
FungLeo17 小时前
成为全栈·Flutter App 篇·内置 WebView:网页历史、App 返回与外链安全
flutter·网络安全·移动开发·webview·路由管理·成为全栈
m0_7381858218 小时前
Flutter 鸿蒙化实战:volume_controller 适配 OpenHarmony,系统音量控制
flutter·华为·harmonyos·鸿蒙
FungLeo19 小时前
成为全栈·Flutter App 篇·Flutter Markdown 阅读器:支持范围与渲染边界
flutter·markdown·dart·代码高亮·成为全栈·移动阅读
m0_7381858219 小时前
Flutter 鸿蒙化实战:webview_cookie_manager 适配 OpenHarmony,WebView Cookie 管理
flutter·华为·harmonyos·鸿蒙
FungLeo19 小时前
成为全栈·Flutter App 篇·服务端目录与文章辅助阅读:锚点、上下篇、浏览量和阅读历史
flutter·锚点·阅读体验·成为全栈·markdown目录·api契约
恋猫de小郭21 小时前
Flutter Android 改动:APK 不再允许通过 Intent 动态修改 Engine 参数
android·前端·flutter
小 黄 鸡1 天前
Flutter 面试必会知识点详解(从基础到进阶)
flutter·面试·职场和发展
大龄秃头程序员2 天前
Flutter 最少代码验证:为什么子 Isolate 调原生,一定要先回到主 Isolate?
flutter
FungLeo2 天前
成为全栈·Flutter App 篇·分类、标签与搜索:移动端内容发现
flutter·移动开发·标签·搜索·分类管理·成为全栈