一,flutter_screenutil库的地址:
https://pub.dev/packages/flutter_screenutil代码地址:
https://github.com/OpenFlutter/flutter_screenutil
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
二,在项目中引入
在pubspec.yaml中添加:flutter_screenutil: ^5.6.1效果如图:
![](/i/l/?n=23&i=blog/1938691/202302/1938691-20230208144952312-35110492.png)
三,编写代码:
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, ), ]), ], ), ); } }
四,测试效果
![](/i/l/?n=23&i=blog/1938691/202302/1938691-20230208144952339-1873860153.png)
五,查看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
标签:20,5.6,title,适配,screenutil,https,com,flutter From: https://www.cnblogs.com/architectforest/p/17101754.html