1717
1818[ 更新日志] ( https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md )
1919
20- ## Note
21- [ v3] ( https://github.com/OpenFlutter/flutter_screenutil/tree/beta ) 可用于` flutter >= 1.19.0 `
22-
23- [ v2] ( https://github.com/OpenFlutter/flutter_screenutil ) 可用于所有版本。
24-
2520## 使用方法:
2621
2722### 安装依赖:
@@ -33,7 +28,7 @@ dependencies:
3328 flutter:
3429 sdk: flutter
3530 # 添加依赖
36- flutter_screenutil: ^3.0.2
31+ flutter_screenutil: ^3.1.0
3732```
3833### 在每个使用的地方导入包:
3934```
@@ -57,18 +52,18 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
5752void main() {
5853 WidgetsFlutterBinding.ensureInitialized();
5954 //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
60- ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false);
55+ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false);
6156 runApp(MyApp());
6257}
6358
6459//默认 width : 1080px , height:1920px , allowFontScaling:false
65- ScreenUtil.init();
60+ ScreenUtil.init(context );
6661
6762//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
68- ScreenUtil.init(designSize: Size(750, 1334));
63+ ScreenUtil.init(context, designSize: Size(750, 1334));
6964
7065//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
71- ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: true);
66+ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
7267
7368```
7469
@@ -180,12 +175,10 @@ Column(
180175```
181176
182177``` dart
183- void main() {
184- WidgetsFlutterBinding.ensureInitialized();
185- //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
186- ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false);
187- runApp(MyApp());
188- }
178+ import 'package:flutter/material.dart';
179+ import 'package:flutter_screenutil/flutter_screenutil.dart';
180+
181+ void main() => runApp(MyApp());
189182
190183class MyApp extends StatelessWidget {
191184 @override
@@ -196,11 +189,20 @@ class MyApp extends StatelessWidget {
196189 theme: ThemeData(
197190 primarySwatch: Colors.blue,
198191 ),
199- home: ExampleWidget(title: 'FlutterScreenUtil示例' ),
192+ home: MyHomePage( ),
200193 );
201194 }
202195}
203196
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+
204206class ExampleWidget extends StatefulWidget {
205207 const ExampleWidget({Key key, this.title}) : super(key: key);
206208
@@ -232,8 +234,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
232234 child: Text(
233235 '我的宽度:${0.5.wp}dp \n'
234236 '我的高度:${ScreenUtil().setHeight(200)}dp',
235- style: TextStyle(
236- color: Colors.white, fontSize: ScreenUtil().setSp(24)),
237+ style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(24)),
237238 ),
238239 ),
239240 Container(
@@ -244,9 +245,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
244245 child: Text(
245246 '我的宽度:${375.w}dp \n'
246247 '我的高度:${200.h}dp',
247- style: TextStyle(
248- color: Colors.white,
249- fontSize: ScreenUtil().setSp(24))),
248+ style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(24))),
250249 ),
251250 ],
252251 ),
@@ -274,11 +273,17 @@ class _ExampleWidgetState extends State<ExampleWidget> {
274273 children: <Widget>[
275274 Text(
276275 '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化',
277- style: ts.t2,
276+ style: TextStyle(
277+ color: Colors.black,
278+ fontSize: 24.sp,
279+ ),
278280 ),
279281 Text(
280282 '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化',
281- style: ts.t1,
283+ style: TextStyle(
284+ color: Colors.black,
285+ fontSize: 24.ssp,
286+ ),
282287 ),
283288 ],
284289 )
@@ -289,7 +294,8 @@ class _ExampleWidgetState extends State<ExampleWidget> {
289294 child: Icon(Icons.title),
290295 onPressed: () {
291296 ScreenUtil.init(
292- designSize: Size(1500, 1334),
297+ context,
298+ designSize: Size(750, 1334),
293299 allowFontScaling: false,
294300 );
295301 setState(() {});
0 commit comments