Showing
11 changed files
with
127 additions
and
133 deletions
.flutter_tool_state
0 → 100644
@@ -5,6 +5,17 @@ | @@ -5,6 +5,17 @@ | ||
5 | * @LastEditTime: 2020年6月20日 11:20:02 | 5 | * @LastEditTime: 2020年6月20日 11:20:02 |
6 | * @Description: Update log | 6 | * @Description: Update log |
7 | --> | 7 | --> |
8 | + | ||
9 | +# 3.1.1 | ||
10 | +- change readme | ||
11 | + | ||
12 | +# 3.1.0 | ||
13 | +- Use the way back to v2 version | ||
14 | +- Modify registration method | ||
15 | + | ||
16 | +# 3.0.2+1 | ||
17 | +- Guide users to use V2 version | ||
18 | + | ||
8 | # 3.0.2 | 19 | # 3.0.2 |
9 | - Change the unit of'statusBarHeight' and 'bottomBarHeight' to dp | 20 | - Change the unit of'statusBarHeight' and 'bottomBarHeight' to dp |
10 | 21 |
@@ -13,11 +13,6 @@ | @@ -13,11 +13,6 @@ | ||
13 | 13 | ||
14 | [Update log](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md) | 14 | [Update log](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md) |
15 | 15 | ||
16 | -## Note | ||
17 | -[v3](https://github.com/OpenFlutter/flutter_screenutil/tree/beta) requires `flutter >= 1.19.0`. | ||
18 | - | ||
19 | -[v2](https://github.com/OpenFlutter/flutter_screenutil) support all versions. | ||
20 | - | ||
21 | ## Usage: | 16 | ## Usage: |
22 | 17 | ||
23 | ### Add dependency: | 18 | ### Add dependency: |
@@ -28,7 +23,7 @@ dependencies: | @@ -28,7 +23,7 @@ dependencies: | ||
28 | flutter: | 23 | flutter: |
29 | sdk: flutter | 24 | sdk: flutter |
30 | # add flutter_screenutil | 25 | # add flutter_screenutil |
31 | - flutter_screenutil: ^3.0.2 | 26 | + flutter_screenutil: ^3.1.0 |
32 | ``` | 27 | ``` |
33 | ### Add the following imports to your Dart code: | 28 | ### Add the following imports to your Dart code: |
34 | ``` | 29 | ``` |
@@ -50,20 +45,20 @@ Please set the size of the design draft before use, the width and height of the | @@ -50,20 +45,20 @@ Please set the size of the design draft before use, the width and height of the | ||
50 | void main() { | 45 | void main() { |
51 | WidgetsFlutterBinding.ensureInitialized(); | 46 | WidgetsFlutterBinding.ensureInitialized(); |
52 | //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) | 47 | //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) |
53 | - ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false); | 48 | + ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); |
54 | runApp(MyApp()); | 49 | runApp(MyApp()); |
55 | } | 50 | } |
56 | 51 | ||
57 | //fill in the screen size of the device in the design | 52 | //fill in the screen size of the device in the design |
58 | 53 | ||
59 | //default value : width : 1080px , height:1920px , allowFontScaling:false | 54 | //default value : width : 1080px , height:1920px , allowFontScaling:false |
60 | -ScreenUtil.init(); | 55 | +ScreenUtil.init(context); |
61 | 56 | ||
62 | //If the design is based on the size of the iPhone6 (iPhone6 750*1334) | 57 | //If the design is based on the size of the iPhone6 (iPhone6 750*1334) |
63 | -ScreenUtil.init(designSize: Size(750, 1334)); | 58 | +ScreenUtil.init(context, designSize: Size(750, 1334)); |
64 | 59 | ||
65 | //If you want to set the font size is scaled according to the system's "font size" assist option | 60 | //If you want to set the font size is scaled according to the system's "font size" assist option |
66 | -ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: true); | 61 | +ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); |
67 | 62 | ||
68 | ``` | 63 | ``` |
69 | 64 |
@@ -17,11 +17,6 @@ | @@ -17,11 +17,6 @@ | ||
17 | 17 | ||
18 | [更新日志](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md) | 18 | [更新日志](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md) |
19 | 19 | ||
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 | - | ||
25 | ## 使用方法: | 20 | ## 使用方法: |
26 | 21 | ||
27 | ### 安装依赖: | 22 | ### 安装依赖: |
@@ -33,7 +28,7 @@ dependencies: | @@ -33,7 +28,7 @@ dependencies: | ||
33 | flutter: | 28 | flutter: |
34 | sdk: flutter | 29 | sdk: flutter |
35 | # 添加依赖 | 30 | # 添加依赖 |
36 | - flutter_screenutil: ^3.0.2 | 31 | + flutter_screenutil: ^3.1.0 |
37 | ``` | 32 | ``` |
38 | ### 在每个使用的地方导入包: | 33 | ### 在每个使用的地方导入包: |
39 | ``` | 34 | ``` |
@@ -57,18 +52,18 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -57,18 +52,18 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
57 | void main() { | 52 | void main() { |
58 | WidgetsFlutterBinding.ensureInitialized(); | 53 | WidgetsFlutterBinding.ensureInitialized(); |
59 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | 54 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) |
60 | - ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false); | 55 | + ScreenUtil.init(context,designSize: Size(750, 1334), allowFontScaling: false); |
61 | runApp(MyApp()); | 56 | runApp(MyApp()); |
62 | } | 57 | } |
63 | 58 | ||
64 | //默认 width : 1080px , height:1920px , allowFontScaling:false | 59 | //默认 width : 1080px , height:1920px , allowFontScaling:false |
65 | -ScreenUtil.init(); | 60 | +ScreenUtil.init(context); |
66 | 61 | ||
67 | //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | 62 | //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) |
68 | -ScreenUtil.init(designSize: Size(750, 1334)); | 63 | +ScreenUtil.init(context, designSize: Size(750, 1334)); |
69 | 64 | ||
70 | //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false | 65 | //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false |
71 | -ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: true); | 66 | +ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); |
72 | 67 | ||
73 | ``` | 68 | ``` |
74 | 69 | ||
@@ -180,12 +175,10 @@ Column( | @@ -180,12 +175,10 @@ Column( | ||
180 | ``` | 175 | ``` |
181 | 176 | ||
182 | ```dart | 177 | ```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()); | ||
189 | 182 | ||
190 | class MyApp extends StatelessWidget { | 183 | class MyApp extends StatelessWidget { |
191 | @override | 184 | @override |
@@ -196,11 +189,20 @@ class MyApp extends StatelessWidget { | @@ -196,11 +189,20 @@ class MyApp extends StatelessWidget { | ||
196 | theme: ThemeData( | 189 | theme: ThemeData( |
197 | primarySwatch: Colors.blue, | 190 | primarySwatch: Colors.blue, |
198 | ), | 191 | ), |
199 | - home: ExampleWidget(title: 'FlutterScreenUtil示例'), | 192 | + home: MyHomePage(), |
200 | ); | 193 | ); |
201 | } | 194 | } |
202 | } | 195 | } |
203 | 196 | ||
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 | + | ||
204 | class ExampleWidget extends StatefulWidget { | 206 | class ExampleWidget extends StatefulWidget { |
205 | const ExampleWidget({Key key, this.title}) : super(key: key); | 207 | const ExampleWidget({Key key, this.title}) : super(key: key); |
206 | 208 | ||
@@ -232,8 +234,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -232,8 +234,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
232 | child: Text( | 234 | child: Text( |
233 | '我的宽度:${0.5.wp}dp \n' | 235 | '我的宽度:${0.5.wp}dp \n' |
234 | '我的高度:${ScreenUtil().setHeight(200)}dp', | 236 | '我的高度:${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)), |
237 | ), | 238 | ), |
238 | ), | 239 | ), |
239 | Container( | 240 | Container( |
@@ -244,9 +245,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -244,9 +245,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
244 | child: Text( | 245 | child: Text( |
245 | '我的宽度:${375.w}dp \n' | 246 | '我的宽度:${375.w}dp \n' |
246 | '我的高度:${200.h}dp', | 247 | '我的高度:${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))), |
250 | ), | 249 | ), |
251 | ], | 250 | ], |
252 | ), | 251 | ), |
@@ -274,11 +273,17 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -274,11 +273,17 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
274 | children: <Widget>[ | 273 | children: <Widget>[ |
275 | Text( | 274 | Text( |
276 | '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', | 275 | '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', |
277 | - style: ts.t2, | 276 | + style: TextStyle( |
277 | + color: Colors.black, | ||
278 | + fontSize: 24.sp, | ||
279 | + ), | ||
278 | ), | 280 | ), |
279 | Text( | 281 | Text( |
280 | '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', | 282 | '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', |
281 | - style: ts.t1, | 283 | + style: TextStyle( |
284 | + color: Colors.black, | ||
285 | + fontSize: 24.ssp, | ||
286 | + ), | ||
282 | ), | 287 | ), |
283 | ], | 288 | ], |
284 | ) | 289 | ) |
@@ -289,7 +294,8 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -289,7 +294,8 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
289 | child: Icon(Icons.title), | 294 | child: Icon(Icons.title), |
290 | onPressed: () { | 295 | onPressed: () { |
291 | ScreenUtil.init( | 296 | ScreenUtil.init( |
292 | - designSize: Size(1500, 1334), | 297 | + context, |
298 | + designSize: Size(750, 1334), | ||
293 | allowFontScaling: false, | 299 | allowFontScaling: false, |
294 | ); | 300 | ); |
295 | setState(() {}); | 301 | setState(() {}); |
@@ -13,11 +13,6 @@ | @@ -13,11 +13,6 @@ | ||
13 | 13 | ||
14 | [Histórico de atualizações](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md) | 14 | [Histórico de atualizações](https://github.com/OpenFlutter/flutter_screenutil/blob/master/CHANGELOG.md) |
15 | 15 | ||
16 | -## Note | ||
17 | -[v3](https://github.com/OpenFlutter/flutter_screenutil/tree/beta) requer `flutter >= 1.19.0`. | ||
18 | - | ||
19 | -[v2](https://github.com/OpenFlutter/flutter_screenutil) support all versions. | ||
20 | - | ||
21 | ## Como usar: | 16 | ## Como usar: |
22 | 17 | ||
23 | ### Adicionando a dependência: | 18 | ### Adicionando a dependência: |
@@ -28,7 +23,7 @@ dependencies: | @@ -28,7 +23,7 @@ dependencies: | ||
28 | flutter: | 23 | flutter: |
29 | sdk: flutter | 24 | sdk: flutter |
30 | # add flutter_screenutil | 25 | # add flutter_screenutil |
31 | - flutter_screenutil: ^3.0.2 | 26 | + flutter_screenutil: ^3.1.0 |
32 | ``` | 27 | ``` |
33 | 28 | ||
34 | ### Adicione o seguinte import em seu código Dart: | 29 | ### Adicione o seguinte import em seu código Dart: |
@@ -53,18 +48,18 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej | @@ -53,18 +48,18 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej | ||
53 | void main() { | 48 | void main() { |
54 | WidgetsFlutterBinding.ensureInitialized(); | 49 | WidgetsFlutterBinding.ensureInitialized(); |
55 | //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) | 50 | //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) |
56 | - ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false); | 51 | + ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); |
57 | runApp(MyApp()); | 52 | runApp(MyApp()); |
58 | } | 53 | } |
59 | 54 | ||
60 | //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false | 55 | //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false |
61 | -ScreenUtil.init(); | 56 | +ScreenUtil.init(context); |
62 | 57 | ||
63 | //Se o design é baseado no iPhone6 (iPhone6 750*1334) | 58 | //Se o design é baseado no iPhone6 (iPhone6 750*1334) |
64 | -ScreenUtil.init(designSize: Size(750, 1334)); | 59 | +ScreenUtil.init(context, designSize: Size(750, 1334)); |
65 | 60 | ||
66 | //Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema | 61 | //Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema |
67 | -ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: true); | 62 | +ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); |
68 | 63 | ||
69 | ``` | 64 | ``` |
70 | 65 |
@@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
7 | FlutterApplication and put your custom class here. --> | 7 | FlutterApplication and put your custom class here. --> |
8 | <application | 8 | <application |
9 | android:name="io.flutter.app.FlutterApplication" | 9 | android:name="io.flutter.app.FlutterApplication" |
10 | - android:label="example" | 10 | + android:label="flutter_screenutil" |
11 | android:icon="@mipmap/ic_launcher"> | 11 | android:icon="@mipmap/ic_launcher"> |
12 | <activity | 12 | <activity |
13 | android:name=".MainActivity" | 13 | android:name=".MainActivity" |
@@ -3,14 +3,7 @@ import 'dart:ui'; | @@ -3,14 +3,7 @@ import 'dart:ui'; | ||
3 | import 'package:flutter/material.dart'; | 3 | import 'package:flutter/material.dart'; |
4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
5 | 5 | ||
6 | -import 'text_style.dart'; | ||
7 | - | ||
8 | -void main() { | ||
9 | - WidgetsFlutterBinding.ensureInitialized(); | ||
10 | - //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) | ||
11 | - ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false); | ||
12 | - runApp(MyApp()); | ||
13 | -} | 6 | +void main() => runApp(MyApp()); |
14 | 7 | ||
15 | class MyApp extends StatelessWidget { | 8 | class MyApp extends StatelessWidget { |
16 | @override | 9 | @override |
@@ -21,11 +14,20 @@ class MyApp extends StatelessWidget { | @@ -21,11 +14,20 @@ class MyApp extends StatelessWidget { | ||
21 | theme: ThemeData( | 14 | theme: ThemeData( |
22 | primarySwatch: Colors.blue, | 15 | primarySwatch: Colors.blue, |
23 | ), | 16 | ), |
24 | - home: ExampleWidget(title: 'FlutterScreenUtil Demo'), | 17 | + home: MyHomePage(), |
25 | ); | 18 | ); |
26 | } | 19 | } |
27 | } | 20 | } |
28 | 21 | ||
22 | +class MyHomePage extends StatelessWidget { | ||
23 | + @override | ||
24 | + Widget build(BuildContext context) { | ||
25 | + //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) | ||
26 | + ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
27 | + return ExampleWidget(title: 'FlutterScreenUtil Demo'); | ||
28 | + } | ||
29 | +} | ||
30 | + | ||
29 | class ExampleWidget extends StatefulWidget { | 31 | class ExampleWidget extends StatefulWidget { |
30 | const ExampleWidget({Key key, this.title}) : super(key: key); | 32 | const ExampleWidget({Key key, this.title}) : super(key: key); |
31 | 33 | ||
@@ -38,7 +40,10 @@ class ExampleWidget extends StatefulWidget { | @@ -38,7 +40,10 @@ class ExampleWidget extends StatefulWidget { | ||
38 | class _ExampleWidgetState extends State<ExampleWidget> { | 40 | class _ExampleWidgetState extends State<ExampleWidget> { |
39 | @override | 41 | @override |
40 | Widget build(BuildContext context) { | 42 | Widget build(BuildContext context) { |
41 | - // printScreenInformation(); | 43 | + //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) |
44 | + ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
45 | + | ||
46 | + printScreenInformation(); | ||
42 | return Scaffold( | 47 | return Scaffold( |
43 | appBar: AppBar( | 48 | appBar: AppBar( |
44 | title: Text(widget.title), | 49 | title: Text(widget.title), |
@@ -112,7 +117,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -112,7 +117,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
112 | ), | 117 | ), |
113 | Text( | 118 | Text( |
114 | 'My font size is 24px on the design draft and will change with the system.', | 119 | 'My font size is 24px on the design draft and will change with the system.', |
115 | - style: ts.t1, | 120 | + style: TextStyle( |
121 | + color: Colors.black, | ||
122 | + fontSize: 24.ssp, | ||
123 | + ), | ||
116 | ), | 124 | ), |
117 | ], | 125 | ], |
118 | ) | 126 | ) |
@@ -122,10 +130,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -122,10 +130,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
122 | floatingActionButton: FloatingActionButton( | 130 | floatingActionButton: FloatingActionButton( |
123 | child: Icon(Icons.title), | 131 | child: Icon(Icons.title), |
124 | onPressed: () { | 132 | onPressed: () { |
125 | - ScreenUtil.init( | ||
126 | - designSize: Size(750, 1334), | ||
127 | - allowFontScaling: false, | ||
128 | - ); | 133 | + ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); |
129 | setState(() {}); | 134 | setState(() {}); |
130 | }, | 135 | }, |
131 | ), | 136 | ), |
@@ -135,16 +140,13 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -135,16 +140,13 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
135 | void printScreenInformation() { | 140 | void printScreenInformation() { |
136 | print('Device width dp:${ScreenUtil().screenWidth}'); //Device width | 141 | print('Device width dp:${ScreenUtil().screenWidth}'); //Device width |
137 | print('Device height dp:${ScreenUtil().screenHeight}'); //Device height | 142 | print('Device height dp:${ScreenUtil().screenHeight}'); //Device height |
138 | - print( | ||
139 | - 'Device pixel density:${ScreenUtil().pixelRatio}'); //Device pixel density | 143 | + print('Device pixel density:${ScreenUtil().pixelRatio}'); //Device pixel density |
140 | print( | 144 | print( |
141 | 'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen | 145 | 'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen |
142 | print( | 146 | print( |
143 | 'Status bar height px:${ScreenUtil().statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px | 147 | 'Status bar height px:${ScreenUtil().statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px |
144 | - print( | ||
145 | - 'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}'); | ||
146 | - print( | ||
147 | - 'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}'); | 148 | + print('Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}'); |
149 | + print('Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}'); | ||
148 | print( | 150 | print( |
149 | 'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}'); | 151 | 'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}'); |
150 | print( | 152 | print( |
1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
2 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 2 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
3 | 3 | ||
4 | -import 'text_style.dart'; | ||
5 | - | ||
6 | -void main() { | ||
7 | - WidgetsFlutterBinding.ensureInitialized(); | ||
8 | - //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | ||
9 | - ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false); | ||
10 | - runApp(MyApp()); | ||
11 | -} | 4 | +void main() => runApp(MyApp()); |
12 | 5 | ||
13 | class MyApp extends StatelessWidget { | 6 | class MyApp extends StatelessWidget { |
14 | @override | 7 | @override |
@@ -19,11 +12,20 @@ class MyApp extends StatelessWidget { | @@ -19,11 +12,20 @@ class MyApp extends StatelessWidget { | ||
19 | theme: ThemeData( | 12 | theme: ThemeData( |
20 | primarySwatch: Colors.blue, | 13 | primarySwatch: Colors.blue, |
21 | ), | 14 | ), |
22 | - home: ExampleWidget(title: 'FlutterScreenUtil示例'), | 15 | + home: MyHomePage(), |
23 | ); | 16 | ); |
24 | } | 17 | } |
25 | } | 18 | } |
26 | 19 | ||
20 | +class MyHomePage extends StatelessWidget { | ||
21 | + @override | ||
22 | + Widget build(BuildContext context) { | ||
23 | + //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | ||
24 | + ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
25 | + return ExampleWidget(title: 'FlutterScreenUtil 示例'); | ||
26 | + } | ||
27 | +} | ||
28 | + | ||
27 | class ExampleWidget extends StatefulWidget { | 29 | class ExampleWidget extends StatefulWidget { |
28 | const ExampleWidget({Key key, this.title}) : super(key: key); | 30 | const ExampleWidget({Key key, this.title}) : super(key: key); |
29 | 31 | ||
@@ -55,8 +57,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -55,8 +57,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
55 | child: Text( | 57 | child: Text( |
56 | '我的宽度:${0.5.wp}dp \n' | 58 | '我的宽度:${0.5.wp}dp \n' |
57 | '我的高度:${ScreenUtil().setHeight(200)}dp', | 59 | '我的高度:${ScreenUtil().setHeight(200)}dp', |
58 | - style: TextStyle( | ||
59 | - color: Colors.white, fontSize: ScreenUtil().setSp(24)), | 60 | + style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(24)), |
60 | ), | 61 | ), |
61 | ), | 62 | ), |
62 | Container( | 63 | Container( |
@@ -67,9 +68,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -67,9 +68,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
67 | child: Text( | 68 | child: Text( |
68 | '我的宽度:${375.w}dp \n' | 69 | '我的宽度:${375.w}dp \n' |
69 | '我的高度:${200.h}dp', | 70 | '我的高度:${200.h}dp', |
70 | - style: TextStyle( | ||
71 | - color: Colors.white, | ||
72 | - fontSize: ScreenUtil().setSp(24))), | 71 | + style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(24))), |
73 | ), | 72 | ), |
74 | ], | 73 | ], |
75 | ), | 74 | ), |
@@ -97,11 +96,17 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -97,11 +96,17 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
97 | children: <Widget>[ | 96 | children: <Widget>[ |
98 | Text( | 97 | Text( |
99 | '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', | 98 | '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', |
100 | - style: ts.t2, | 99 | + style: TextStyle( |
100 | + color: Colors.black, | ||
101 | + fontSize: 24.sp, | ||
102 | + ), | ||
101 | ), | 103 | ), |
102 | Text( | 104 | Text( |
103 | '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', | 105 | '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', |
104 | - style: ts.t1, | 106 | + style: TextStyle( |
107 | + color: Colors.black, | ||
108 | + fontSize: 24.ssp, | ||
109 | + ), | ||
105 | ), | 110 | ), |
106 | ], | 111 | ], |
107 | ) | 112 | ) |
@@ -112,6 +117,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -112,6 +117,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
112 | child: Icon(Icons.title), | 117 | child: Icon(Icons.title), |
113 | onPressed: () { | 118 | onPressed: () { |
114 | ScreenUtil.init( | 119 | ScreenUtil.init( |
120 | + context, | ||
115 | designSize: Size(750, 1334), | 121 | designSize: Size(750, 1334), |
116 | allowFontScaling: false, | 122 | allowFontScaling: false, |
117 | ); | 123 | ); |
example/lib/text_style.dart
deleted
100644 → 0
1 | -import 'package:flutter/material.dart'; | ||
2 | -import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
3 | - | ||
4 | -class TextStyles { | ||
5 | - TextStyle t1 = TextStyle(fontSize: 24.ssp, color: Colors.black); | ||
6 | - TextStyle t2 = TextStyle(fontSize: 24.sp, color: Colors.black); | ||
7 | -} | ||
8 | - | ||
9 | -var ts = TextStyles(); | ||
10 | - | ||
11 | -class TextStyle2 { | ||
12 | - static TextStyle2 ts2; | ||
13 | - | ||
14 | - factory TextStyle2() { | ||
15 | - if (ts2 == null) { | ||
16 | - ts2 = TextStyle2(); | ||
17 | - } | ||
18 | - return ts2; | ||
19 | - } | ||
20 | - | ||
21 | - TextStyle t1 = TextStyle(fontSize: 24.ssp, color: Colors.black); | ||
22 | - TextStyle t2 = TextStyle(fontSize: 24.sp, color: Colors.black); | ||
23 | -} |
@@ -4,7 +4,6 @@ | @@ -4,7 +4,6 @@ | ||
4 | */ | 4 | */ |
5 | 5 | ||
6 | import 'package:flutter/material.dart'; | 6 | import 'package:flutter/material.dart'; |
7 | -import 'package:flutter/scheduler.dart'; | ||
8 | 7 | ||
9 | class ScreenUtil { | 8 | class ScreenUtil { |
10 | static ScreenUtil _instance; | 9 | static ScreenUtil _instance; |
@@ -18,26 +17,14 @@ class ScreenUtil { | @@ -18,26 +17,14 @@ class ScreenUtil { | ||
18 | /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. | 17 | /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. |
19 | bool allowFontScaling = false; | 18 | bool allowFontScaling = false; |
20 | 19 | ||
21 | - double _pixelRatio; | ||
22 | - double _screenWidth; | ||
23 | - double _screenHeight; | ||
24 | - double _statusBarHeight; | ||
25 | - double _bottomBarHeight; | ||
26 | - double _textScaleFactor; | 20 | + static double _pixelRatio; |
21 | + static double _screenWidth; | ||
22 | + static double _screenHeight; | ||
23 | + static double _statusBarHeight; | ||
24 | + static double _bottomBarHeight; | ||
25 | + static double _textScaleFactor; | ||
27 | 26 | ||
28 | - ScreenUtil._() { | ||
29 | - final window = SchedulerBinding.instance?.window; | ||
30 | - assert( | ||
31 | - window != null, | ||
32 | - '\nYou need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()`, before initializing ScreenUtil.', | ||
33 | - ); | ||
34 | - _pixelRatio = window.devicePixelRatio; | ||
35 | - _screenWidth = window.physicalSize.width / _pixelRatio; | ||
36 | - _screenHeight = window.physicalSize.height / _pixelRatio; | ||
37 | - _statusBarHeight = window.padding.top / _pixelRatio; | ||
38 | - _bottomBarHeight = window.padding.bottom / _pixelRatio; | ||
39 | - _textScaleFactor = window.textScaleFactor; | ||
40 | - } | 27 | + ScreenUtil._(); |
41 | 28 | ||
42 | factory ScreenUtil() { | 29 | factory ScreenUtil() { |
43 | assert( | 30 | assert( |
@@ -47,7 +34,8 @@ class ScreenUtil { | @@ -47,7 +34,8 @@ class ScreenUtil { | ||
47 | return _instance; | 34 | return _instance; |
48 | } | 35 | } |
49 | 36 | ||
50 | - static void init({ | 37 | + static void init( |
38 | + BuildContext context, { | ||
51 | Size designSize = defaultSize, | 39 | Size designSize = defaultSize, |
52 | bool allowFontScaling = false, | 40 | bool allowFontScaling = false, |
53 | }) { | 41 | }) { |
@@ -55,6 +43,13 @@ class ScreenUtil { | @@ -55,6 +43,13 @@ class ScreenUtil { | ||
55 | _instance | 43 | _instance |
56 | ..uiSize = designSize | 44 | ..uiSize = designSize |
57 | ..allowFontScaling = allowFontScaling; | 45 | ..allowFontScaling = allowFontScaling; |
46 | + MediaQueryData mediaQuery = MediaQuery.of(context); | ||
47 | + _pixelRatio = mediaQuery.devicePixelRatio; | ||
48 | + _screenWidth = mediaQuery.size.width; | ||
49 | + _screenHeight = mediaQuery.size.height; | ||
50 | + _statusBarHeight = mediaQuery.padding.top; | ||
51 | + _bottomBarHeight = mediaQuery.padding.bottom; | ||
52 | + _textScaleFactor = mediaQuery.textScaleFactor; | ||
58 | } | 53 | } |
59 | 54 | ||
60 | /// 每个逻辑像素的字体像素数,字体的缩放比例 | 55 | /// 每个逻辑像素的字体像素数,字体的缩放比例 |
@@ -119,9 +114,12 @@ class ScreenUtil { | @@ -119,9 +114,12 @@ class ScreenUtil { | ||
119 | ///Font size adaptation method | 114 | ///Font size adaptation method |
120 | ///@param [fontSize] The size of the font on the UI design, in px. | 115 | ///@param [fontSize] The size of the font on the UI design, in px. |
121 | ///@param [allowFontScaling] | 116 | ///@param [allowFontScaling] |
122 | - num setSp(num fontSize, {bool allowFontScalingSelf}) => allowFontScalingSelf == null | ||
123 | - ? (allowFontScaling ? (fontSize * scaleText) : ((fontSize * scaleText) / _textScaleFactor)) | ||
124 | - : (allowFontScalingSelf | ||
125 | - ? (fontSize * scaleText) | ||
126 | - : ((fontSize * scaleText) / _textScaleFactor)); | 117 | + num setSp(num fontSize, {bool allowFontScalingSelf}) => |
118 | + allowFontScalingSelf == null | ||
119 | + ? (allowFontScaling | ||
120 | + ? (fontSize * scaleText) | ||
121 | + : ((fontSize * scaleText) / _textScaleFactor)) | ||
122 | + : (allowFontScalingSelf | ||
123 | + ? (fontSize * scaleText) | ||
124 | + : ((fontSize * scaleText) / _textScaleFactor)); | ||
127 | } | 125 | } |
1 | name: flutter_screenutil | 1 | name: flutter_screenutil |
2 | description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models | 2 | description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models |
3 | -version: 3.0.2 | 3 | +version: 3.1.1 |
4 | homepage: https://github.com/OpenFlutter/flutter_screenutil/tree/beta | 4 | homepage: https://github.com/OpenFlutter/flutter_screenutil/tree/beta |
5 | 5 | ||
6 | environment: | 6 | environment: |
-
Please register or login to post a comment