flutter:适配屏幕(flutter_screenutil: ^5.6.1 / flutter 3.7.0 )
一,flutter_screenutil库的地址:
https://pub.dev/packages/flutter_screenutil
代码地址:
https://github.com/OpenFlutter/flutter_screenutil
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/06/04/flutter-shi-pei-ping-mu-flutterscreenutil-5-6-1-flutter-3-7/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,在项目中引入
在pubspec.yaml中添加:
flutter_screenutil: ^5.6.1
效果如图:

然后点击 Pub get 下载库
三,编写代码:
import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { //设置尺寸(填写设计中设备的屏幕尺寸)如果设计基于750dp * 1334dp的屏幕 ScreenUtil.init( context, designSize: Size(750, 1334) ); return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Column( children: <Widget>[ Row( children: <Widget>[ Container( decoration: new BoxDecoration( color: Colors.blue, ), width:345.w, height: 200.w, margin:EdgeInsets.only(top:20.w,left:20.w), ), Container( decoration: new BoxDecoration( color: Colors.red, ), width:345.w, height: 200.w, margin:EdgeInsets.only(top:20.w,left:20.w), ), ]), Row( children: <Widget>[ Container( decoration: new BoxDecoration( color: Colors.green, ), width:345.w, height: 200.w, margin:EdgeInsets.only(top:20.w,left:20.w), alignment: Alignment.topLeft, ), ]), ], ), ); } }
四,测试效果

五,查看flutter的版本:
liuhongdi@liuhongdideMBP ~ % flutter --version Flutter 3.7.0 • channel stable • https://github.com/flutter/flutter.git Framework • revision b06b8b2710 (2 weeks ago) • 2023-01-23 16:55:55 -0800 Engine • revision b24591ed32 Tools • Dart 2.19.0 • DevTools 2.20.1
分类:
flutter
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!