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