Skip to content

Commit

Permalink
添加新的初始化方式
Browse files Browse the repository at this point in the history
  • Loading branch information
李卓原 committed Mar 11, 2021
1 parent f9908fb commit f29a851
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 32 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option
Please set the size of the design draft before use, the width and height of the design draft.

The first way:
```dart
void main() => runApp(MyApp());
Expand All @@ -57,6 +58,24 @@ class MyApp extends StatelessWidget {
}
}
```
The second way:
```
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//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 360*690
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'FlutterScreenUtil Demo'),
);
}
}
```

### Use:

Expand Down
22 changes: 20 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致)
一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸:

方式一:
```dart
//填入设计稿中设备的屏幕尺寸
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//填入设计稿中设备的屏幕尺寸,单位dp
return ScreenUtilInit(
designSize: Size(360, 690),
allowFontScaling: false,
Expand All @@ -69,6 +69,24 @@ class MyApp extends StatelessWidget {
}
}
```
方式二:
```
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//填入设计稿中设备的屏幕尺寸 (例如:360*690) , 单位dp
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'FlutterScreenUtil Demo'),
);
}
}
```

### 使用

Expand Down
21 changes: 20 additions & 1 deletion README_PT.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
Por favor, defina a largura e altura do protótipo de design antes de usar (em pixels).
Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou seja, no arquivo de entrada, defina apenas uma vez) para garantir que o tamanho de ajuste seja o mesmo antes de cada uso:

The first way:
```dart
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//Preencha o tamanho da tela do dispositivo no protótipo de design , in dp
//Set the fit size (fill in the screen size of the device in the design,in dp)
return ScreenUtilInit(
designSize: Size(360, 690),
allowFontScaling: false,
Expand All @@ -61,6 +62,24 @@ class MyApp extends StatelessWidget {
}
}
```
The second way:
```
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//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 360*690
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'FlutterScreenUtil Demo'),
);
}
}
```

### Uso:

Expand Down
29 changes: 11 additions & 18 deletions example/lib/main_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//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)
return ScreenUtilInit(
designSize: Size(360, 690),
allowFontScaling: false,
builder: () => MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'FlutterScreenUtil Demo'),
//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 360*690
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: 'FlutterScreenUtil Demo'),
);
}
}
Expand Down Expand Up @@ -64,9 +61,7 @@ class _HomePageState extends State<HomePage> {
child: Text(
'我的设计稿宽度: 180dp \n'
'我的设计稿高度: 200dp',
style: TextStyle(
color: Colors.white,
fontSize: ScreenUtil().setSp(12))),
style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12))),
),
],
),
Expand Down Expand Up @@ -133,10 +128,8 @@ class _HomePageState extends State<HomePage> {
print('状态栏高度:${ScreenUtil().statusBarHeight}dp');
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
print(
'宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
print(
'高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
print('屏幕宽度的0.5:${0.5.sw}dp');
print('屏幕高度的0.5:${0.5.sh}dp');
Expand Down
15 changes: 5 additions & 10 deletions lib/screen_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class ScreenUtil {
}

static void init(
BoxConstraints constraints,
Orientation orientation, {
BoxConstraints constraints, {
Orientation orientation = Orientation.portrait,
Size designSize = defaultSize,
bool allowFontScaling = false,
}) {
Expand Down Expand Up @@ -125,12 +125,7 @@ class ScreenUtil {
///Font size adaptation method
///- [fontSize] The size of the font on the UI design, in dp.
///- [allowFontScaling]
double setSp(num fontSize, {bool? allowFontScalingSelf}) =>
allowFontScalingSelf == null
? (allowFontScaling
? (fontSize * scaleText) * _textScaleFactor
: (fontSize * scaleText))
: (allowFontScalingSelf
? (fontSize * scaleText) * _textScaleFactor
: (fontSize * scaleText));
double setSp(num fontSize, {bool? allowFontScalingSelf}) => allowFontScalingSelf == null
? (allowFontScaling ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText))
: (allowFontScalingSelf ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText));
}
2 changes: 1 addition & 1 deletion lib/screenutil_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ScreenUtilInit extends StatelessWidget {
if (constraints.maxWidth != 0) {
ScreenUtil.init(
constraints,
orientation,
orientation: orientation,
designSize: designSize,
allowFontScaling: allowFontScaling,
);
Expand Down

0 comments on commit f29a851

Please sign in to comment.