Skip to content

Commit 3eff306

Browse files
author
李卓原
committed
merge v4
update readme
1 parent 4f87900 commit 3eff306

File tree

11 files changed

+118
-78
lines changed

11 files changed

+118
-78
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class MyApp extends StatelessWidget {
8282
ScreenUtil().scaleWidth //The ratio of actual width to UI design
8383
ScreenUtil().scaleHeight //The ratio of actual height to UI design
8484
85+
ScreenUtil().orientation //Screen orientation
8586
0.2.sw //0.2 times the screen width
8687
0.5.sh //50% of screen height
8788
```
@@ -181,11 +182,31 @@ Column(
181182
)
182183
```
183184

185+
#### Setting font does not change with system font size
186+
187+
```
188+
MaterialApp(
189+
debugShowCheckedModeBanner: false,
190+
title: 'Flutter_ScreenUtil',
191+
theme: ThemeData(
192+
primarySwatch: Colors.blue,
193+
),
194+
builder: (context, widget) {
195+
return MediaQuery(
196+
///Setting font does not change with system font size
197+
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
198+
child: widget,
199+
);
200+
},
201+
home: HomePage(title: 'FlutterScreenUtil Demo'),
202+
),
203+
```
204+
184205
[widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115)
185206

186207
### Example:
187208

188-
[example demo](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib/main_zh.dart)
209+
[example demo](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib/main.dart)
189210

190211
### Effect:
191212

README_CN.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,22 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
4949
```dart
5050
//填入设计稿中设备的屏幕尺寸
5151
52-
class HomePage extends StatelessWidget {
53-
const HomePage({Key key}) : super(key: key);
52+
void main() => runApp(MyApp());
5453
54+
class MyApp extends StatelessWidget {
5555
@override
5656
Widget build(BuildContext context) {
57-
// 初始化
58-
ScreenUtil.init(
59-
// 设备像素大小(必须在首页中获取)
60-
BoxConstraints(
61-
maxWidth: MediaQuery.of(context).size.width,
62-
maxHeight: MediaQuery.of(context).size.height,
63-
),
64-
// 设计尺寸
65-
designSize: Size(750, 1334),
57+
return ScreenUtilInit(
58+
designSize: Size(360, 690),
6659
allowFontScaling: false,
67-
);
68-
return Container(
69-
child: child,
60+
builder: () => MaterialApp(
61+
debugShowCheckedModeBanner: false,
62+
title: 'Flutter_ScreenUtil',
63+
theme: ThemeData(
64+
primarySwatch: Colors.blue,
65+
),
66+
home: HomePage(title: 'FlutterScreenUtil Demo'),
67+
),
7068
);
7169
}
7270
}
@@ -94,6 +92,8 @@ class HomePage extends StatelessWidget {
9492
ScreenUtil().scaleWidth // 实际宽度设计稿宽度的比例
9593
ScreenUtil().scaleHeight // 实际高度与设计稿高度度的比例
9694
95+
ScreenUtil().orientation //屏幕方向
96+
9797
0.2.sw //屏幕宽度的0.2倍
9898
0.5.sh //屏幕高度的50%
9999
```
@@ -182,6 +182,26 @@ Column(
182182
)
183183
```
184184

185+
#### 设置字体不随系统字体大小进行改变
186+
187+
```
188+
MaterialApp(
189+
debugShowCheckedModeBanner: false,
190+
title: 'Flutter_ScreenUtil',
191+
theme: ThemeData(
192+
primarySwatch: Colors.blue,
193+
),
194+
builder: (context, widget) {
195+
return MediaQuery(
196+
///设置文字大小不随系统设置改变
197+
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
198+
child: widget,
199+
);
200+
},
201+
home: HomePage(title: 'FlutterScreenUtil Demo'),
202+
),
203+
```
204+
185205

186206
[widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115)
187207

README_PT.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class MyApp extends StatelessWidget {
8484
ScreenUtil().scaleWidth //The ratio of actual width to UI design
8585
ScreenUtil().scaleHeight //The ratio of actual height to UI design
8686
87+
ScreenUtil().orientation //Screen orientation
88+
8789
0.2.sw //0,2 vezes a largura da tela
8890
0.5.sh //50% altura da tela
8991
```
@@ -334,7 +336,7 @@ class _HomePageState extends State<HomePage> {
334336

335337
### Exemplo:
336338

337-
[Demonstração](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib/main_zh.dart)
339+
[Demonstração](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib/main.dart)
338340

339341
Efeito:
340342

example/ios/Flutter/flutter_export_environment.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ export "FLUTTER_APPLICATION_PATH=/Users/lizhuoyuan/Development/Project/flutter_s
55
export "FLUTTER_TARGET=/Users/lizhuoyuan/Development/Project/flutter_screenutil/example/lib/main.dart"
66
export "FLUTTER_BUILD_DIR=build"
77
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
8-
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
9-
export "FLUTTER_FRAMEWORK_DIR=/Users/lizhuoyuan/Development/flutter/bin/cache/artifacts/engine/ios"
108
export "FLUTTER_BUILD_NAME=1.0.0"
119
export "FLUTTER_BUILD_NUMBER=1"
12-
export "DART_DEFINES=flutter.inspector.structuredErrors%3Dtrue"
1310
export "DART_OBFUSCATION=false"
1411
export "TRACK_WIDGET_CREATION=true"
1512
export "TREE_SHAKE_ICONS=false"
16-
export "PACKAGE_CONFIG=.packages"
13+
export "PACKAGE_CONFIG=/Users/lizhuoyuan/Development/Project/flutter_screenutil/example/.dart_tool/package_config.json"

example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/main.dart

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class HomePage extends StatefulWidget {
3434
class _HomePageState extends State<HomePage> {
3535
@override
3636
Widget build(BuildContext context) {
37-
//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)
38-
3937
printScreenInformation();
4038
return Scaffold(
4139
appBar: AppBar(
@@ -105,20 +103,17 @@ class _HomePageState extends State<HomePage> {
105103
textAlign: TextAlign.center,
106104
),
107105
SizedBox(
108-
height: 50.h,
106+
height: 10.h,
109107
),
110108
Text('System font scaling factor:${ScreenUtil().textScaleFactor}'),
111109
Column(
112110
crossAxisAlignment: CrossAxisAlignment.start,
113111
children: <Widget>[
114-
Container(
115-
height: 32.h,
116-
child: Text(
117-
'My font size is 16sp on the design draft and will not change with the system.',
118-
style: TextStyle(
119-
color: Colors.black,
120-
fontSize: 16.sp,
121-
),
112+
Text(
113+
'My font size is 16sp on the design draft and will not change with the system.',
114+
style: TextStyle(
115+
color: Colors.black,
116+
fontSize: 16.nsp,
122117
),
123118
),
124119
Text(
@@ -147,5 +142,6 @@ class _HomePageState extends State<HomePage> {
147142
print('System font scaling:${ScreenUtil().textScaleFactor}');
148143
print('0.5 times the screen width:${0.5.sw}dp');
149144
print('0.5 times the screen height:${0.5.sh}dp');
145+
print('Screen orientation:${ScreenUtil().orientation}');
150146
}
151147
}

example/lib/main_zh.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MyApp extends StatelessWidget {
88
Widget build(BuildContext context) {
99
//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)
1010
return ScreenUtilInit(
11-
designSize: Size(750, 1334),
11+
designSize: Size(360, 690),
1212
allowFontScaling: false,
1313
builder: () => MaterialApp(
1414
debugShowCheckedModeBanner: false,
@@ -51,9 +51,9 @@ class _HomePageState extends State<HomePage> {
5151
height: 200.h,
5252
color: Colors.red,
5353
child: Text(
54-
'我的实际宽度:${0.5.sw}dp \n'
55-
'我的实际高度:${ScreenUtil().setHeight(200)}dp',
56-
style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12)),
54+
'我的实际宽度:${180.w}dp \n'
55+
'我的实际高度:${200.h}dp',
56+
style: TextStyle(color: Colors.white, fontSize: 12.sp),
5757
),
5858
),
5959
Container(
@@ -64,7 +64,9 @@ class _HomePageState extends State<HomePage> {
6464
child: Text(
6565
'我的设计稿宽度: 180dp \n'
6666
'我的设计稿高度: 200dp',
67-
style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12))),
67+
style: TextStyle(
68+
color: Colors.white,
69+
fontSize: ScreenUtil().setSp(12))),
6870
),
6971
],
7072
),
@@ -131,10 +133,13 @@ class _HomePageState extends State<HomePage> {
131133
print('状态栏高度:${ScreenUtil().statusBarHeight}dp');
132134
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
133135
print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
134-
print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
135-
print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
136+
print(
137+
'宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
138+
print(
139+
'高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
136140
print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
137141
print('屏幕宽度的0.5:${0.5.sw}dp');
138142
print('屏幕高度的0.5:${0.5.sh}dp');
143+
print('屏幕方向:${ScreenUtil().orientation}');
139144
}
140145
}

lib/flutter_screenutil.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
library flutter_screenutil;
77

88
export 'size_extension.dart';
9-
export 'screenutil.dart';
9+
export 'screen_util.dart';
1010
export 'screenutil_init.dart';

lib/screenutil.dart renamed to lib/screen_util.dart

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
44
*/
55

6-
import 'dart:ui' as ui;
7-
86
import 'dart:math';
7+
import 'dart:ui' as ui;
98

109
import 'package:flutter/material.dart';
1110

@@ -21,6 +20,9 @@ class ScreenUtil {
2120
/// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
2221
late bool allowFontScaling;
2322

23+
///屏幕方向
24+
static late Orientation _orientation;
25+
2426
static late double _pixelRatio;
2527
static late double _textScaleFactor;
2628
static late double _screenWidth;
@@ -35,22 +37,23 @@ class ScreenUtil {
3537
}
3638

3739
static void init(
38-
BoxConstraints constraints,
39-
Orientation orientation,
40-
{
40+
BoxConstraints constraints,
41+
Orientation orientation, {
4142
Size designSize = defaultSize,
4243
bool allowFontScaling = false,
4344
}) {
4445
_instance = ScreenUtil._();
4546
_instance
4647
..uiSize = designSize
4748
..allowFontScaling = allowFontScaling;
48-
if(orientation == Orientation.portrait){
49+
50+
_orientation = orientation;
51+
if (orientation == Orientation.portrait) {
4952
_screenWidth = constraints.maxWidth;
50-
_screenHeight = constraints.maxHeight;
51-
}else{
53+
_screenHeight = constraints.maxHeight;
54+
} else {
5255
_screenWidth = constraints.maxHeight;
53-
_screenHeight = constraints.maxWidth;
56+
_screenHeight = constraints.maxWidth;
5457
}
5558

5659
var window = WidgetsBinding.instance?.window ?? ui.window;
@@ -60,6 +63,10 @@ class ScreenUtil {
6063
_textScaleFactor = window.textScaleFactor;
6164
}
6265

66+
///获取屏幕方向
67+
///Get screen orientation
68+
Orientation get orientation => _orientation;
69+
6370
/// 每个逻辑像素的字体像素数,字体的缩放比例
6471
/// The number of font pixels for each logical pixel.
6572
double get textScaleFactor => _textScaleFactor;
@@ -119,12 +126,7 @@ class ScreenUtil {
119126
///Font size adaptation method
120127
///- [fontSize] The size of the font on the UI design, in dp.
121128
///- [allowFontScaling]
122-
double setSp(num fontSize, {bool? allowFontScalingSelf}) =>
123-
allowFontScalingSelf == null
124-
? (allowFontScaling
125-
? (fontSize * scaleText) * _textScaleFactor
126-
: (fontSize * scaleText))
127-
: (allowFontScalingSelf
128-
? (fontSize * scaleText) * _textScaleFactor
129-
: (fontSize * scaleText));
129+
double setSp(num fontSize, {bool? allowFontScalingSelf}) => allowFontScalingSelf == null
130+
? (allowFontScaling ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText))
131+
: (allowFontScalingSelf ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText));
130132
}

lib/screenutil_init.dart

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'package:flutter/widgets.dart';
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_screenutil/screen_util.dart';
23

3-
import 'screenutil.dart';
44

55
class ScreenUtilInit extends StatelessWidget {
66
/// A helper widget that initializes [ScreenUtil]
@@ -21,23 +21,20 @@ class ScreenUtilInit extends StatelessWidget {
2121

2222
@override
2323
Widget build(BuildContext context) {
24-
return LayoutBuilder(
25-
builder: (_, BoxConstraints constraints) {
26-
return OrientationBuilder(
27-
builder: (_, Orientation orientation) {
28-
// ignore: missing_return
29-
if (constraints.maxWidth != 0) {
30-
ScreenUtil.init(
31-
constraints,
32-
orientation,
33-
designSize: designSize,
34-
allowFontScaling: allowFontScaling,
35-
);
36-
}
37-
return builder();
38-
},
39-
);
40-
},
41-
);
24+
return LayoutBuilder(builder: (_, BoxConstraints constraints) {
25+
return OrientationBuilder(
26+
builder: (_, Orientation orientation) {
27+
if (constraints.maxWidth != 0) {
28+
ScreenUtil.init(
29+
constraints,
30+
orientation,
31+
designSize: designSize,
32+
allowFontScaling: allowFontScaling,
33+
);
34+
}
35+
return builder();
36+
},
37+
);
38+
});
4239
}
4340
}

0 commit comments

Comments
 (0)