@@ -28,7 +28,7 @@ dependencies:
2828 flutter:
2929 sdk: flutter
3030 # 添加依赖
31- flutter_screenutil: ^3.1.0
31+ flutter_screenutil: ^4.0.0-beta
3232```
3333### 在每个使用的地方导入包:
3434```
@@ -38,32 +38,42 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
3838### 属性
3939
4040| 属性| 类型| 默认值| 描述|
41- | :---| :---| :---| :---|
42- | width| double| 1080px| 设计稿中设备的宽度,单位px|
43- | height| double| 1920px| 设计稿中设备的高度,单位px|
41+ | :---| :---| :---| :---|
42+ | designSize| Size| Size(1080, 1920)| 设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)|
4443| allowFontScaling| bool| false| 设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放|
4544
4645### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放
47- 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px )
48- 一定在MaterialApp的home中的页面设置(即入口文件, 只需设置一次),以保证在每次使用之前设置好了适配尺寸:
46+ 在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致 )
47+ 一定要进行初始化( 只需设置一次),以保证在每次使用之前设置好了适配尺寸:
4948
5049```
5150//填入设计稿中设备的屏幕尺寸
52- void main() {
53- WidgetsFlutterBinding.ensureInitialized();
54- //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
55- ScreenUtil.init(context,designSize: Size(750, 1334), allowFontScaling: false);
56- runApp(MyApp());
57- }
5851
52+ void main() => runApp(MyApp());
53+
54+ class MyApp extends StatelessWidget {
55+ @override
56+ Widget build(BuildContext context) {
57+ return LayoutBuilder(
58+ builder: (context, constraints) {
59+ //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
60+ ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false);
61+
62+ return MaterialApp(
63+ ...
64+ );
65+ },
66+ );
67+ }
68+ }
5969//默认 width : 1080px , height:1920px , allowFontScaling:false
60- ScreenUtil.init(context );
70+ ScreenUtil.init(constraints );
6171
6272//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
63- ScreenUtil.init(context , designSize: Size(750, 1334));
73+ ScreenUtil.init(constraints , designSize: Size(750, 1334));
6474
6575//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
66- ScreenUtil.init(context , designSize: Size(750, 1334), allowFontScaling: true);
76+ ScreenUtil.init(constraints , designSize: Size(750, 1334), allowFontScaling: true);
6777
6878```
6979
@@ -73,7 +83,7 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
7383#### 传入设计稿的px尺寸 px px px !
7484``` dart
7585 ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸
76- ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸
86+ ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸(一般根据宽度适配即可)
7787 ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体
7888 ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放)
7989 ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //适配字体(不会根据系统的“字体大小”辅助选项来进行缩放)
@@ -82,11 +92,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
8292 ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度
8393 ScreenUtil.screenHeight (sdk>=2.6 : 1.sh) //设备高度
8494 ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的
85- ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px
95+ ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高
8696 ScreenUtil.textScaleFactor //系统字体缩放比例
8797
88- ScreenUtil().scaleWidth // 实际宽度的dp与设计稿px的比例
89- ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例
98+ ScreenUtil().scaleWidth // 实际宽度的dp与设计稿宽度的比例
99+ ScreenUtil().scaleHeight // 实际高度的dp与设计稿高度度的比例
90100
91101 0.2.sw //屏幕宽度的0.2倍
92102 0.5.sh //屏幕高度的50%
@@ -95,11 +105,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
95105
96106#### 适配尺寸
97107
98- 传入设计稿的px尺寸 :
108+ 传入设计稿的尺寸(单位与初始化时的单位相同) :
99109
100110根据屏幕宽度适配 ` width: ScreenUtil().setWidth(540) ` ,
101111
102- 根据屏幕高度适配 ` height: ScreenUtil().setHeight(200) ` ,
112+ 根据屏幕高度适配 ` height: ScreenUtil().setHeight(200) ` , 一般来说,控件高度也根据宽度进行适配
103113
104114** 注意**
105115
@@ -113,7 +123,7 @@ setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的
113123//UI上是长方形:
114124Container(
115125 width: ScreenUtil().setWidth(375),
116- height: ScreenUtil().setHeight(200 ),
126+ height: ScreenUtil().setHeight(375 ),
117127 ),
118128
119129//如果你想显示一个正方形:
@@ -141,10 +151,10 @@ height:200.h
141151```
142152
143153#### 适配字体:
144- 传入设计稿的px尺寸 :
154+ 传入设计稿的字体大小 :
145155
146156```
147- //传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
157+ //传入字体大小(单位和初始化时的单位保持一致) ,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
148158ScreenUtil().setSp(28)
149159或
15016028.sp (dart sdk>=2.6)
@@ -183,36 +193,34 @@ void main() => runApp(MyApp());
183193class MyApp extends StatelessWidget {
184194 @override
185195 Widget build(BuildContext context) {
186- return MaterialApp(
187- debugShowCheckedModeBanner: false,
188- title: 'Flutter_ScreenUtil',
189- theme: ThemeData(
190- primarySwatch: Colors.blue,
191- ),
192- home: MyHomePage(),
196+ return LayoutBuilder(
197+ builder: (context, constraints) {
198+ //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334)
199+ ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false);
200+
201+ return MaterialApp(
202+ debugShowCheckedModeBanner: false,
203+ title: 'Flutter_ScreenUtil',
204+ theme: ThemeData(
205+ primarySwatch: Colors.blue,
206+ ),
207+ home: HomePage(title: 'FlutterScreenUtil Demo'),
208+ );
209+ },
193210 );
194211 }
195212}
196213
197- class MyHomePage extends StatelessWidget {
198- @override
199- Widget build(BuildContext context) {
200- //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
201- ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false);
202- return ExampleWidget(title: 'FlutterScreenUtil 示例');
203- }
204- }
205-
206- class ExampleWidget extends StatefulWidget {
207- const ExampleWidget({Key key, this.title}) : super(key: key);
214+ class HomePage extends StatefulWidget {
215+ const HomePage({Key key, this.title}) : super(key: key);
208216
209217 final String title;
210218
211219 @override
212- _ExampleWidgetState createState() => _ExampleWidgetState ();
220+ _HomePageState createState() => _HomePageState ();
213221}
214222
215- class _ExampleWidgetState extends State<ExampleWidget > {
223+ class _HomePageState extends State<HomePage > {
216224 @override
217225 Widget build(BuildContext context) {
218226 printScreenInformation();
@@ -290,42 +298,20 @@ class _ExampleWidgetState extends State<ExampleWidget> {
290298 ],
291299 ),
292300 ),
293- floatingActionButton: FloatingActionButton(
294- child: Icon(Icons.title),
295- onPressed: () {
296- ScreenUtil.init(
297- context,
298- designSize: Size(750, 1334),
299- allowFontScaling: false,
300- );
301- setState(() {});
302- },
303- ),
304301 );
305302 }
306303
307304 void printScreenInformation() {
308- print('设备宽度:${ScreenUtil().screenWidth}'); //Device width
309- print('设备高度:${ScreenUtil().screenHeight}'); //Device height
310- print('设备的像素密度:${ScreenUtil().pixelRatio}'); //Device pixel density
311- print(
312- '底部安全区距离:${ScreenUtil().bottomBarHeight}dp',
313- ); //Bottom safe zone distance,suitable for buttons with full screen
314- print(
315- '状态栏高度:${ScreenUtil().statusBarHeight}dp',
316- ); //Status bar height , Notch will be higher Unit px
317-
305+ print('设备宽度:${1.sw}');
306+ print('设备高度:${1.sh}');
307+ print('设备的像素密度:${ScreenUtil().pixelRatio}');
308+ print('底部安全区距离:${ScreenUtil().bottomBarHeight}dp');
309+ print('状态栏高度:${ScreenUtil().statusBarHeight}dp');
318310 print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
319311 print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
320-
321- print(
322- '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}',
323- );
324- print(
325- '高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}',
326- );
312+ print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
313+ print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
327314 print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
328-
329315 print('屏幕宽度的0.5:${0.5.sw}');
330316 print('屏幕高度的0.5:${0.5.sh}');
331317 }
0 commit comments