李卓原

添加新的初始化方式

... ... @@ -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());
... ... @@ -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:
... ...
... ... @@ -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,
... ... @@ -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'),
);
}
}
```
### 使用
... ...
... ... @@ -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,
... ... @@ -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:
... ...
... ... @@ -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'),
);
}
}
... ... @@ -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))),
),
],
),
... ... @@ -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');
... ...
... ... @@ -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,
}) {
... ... @@ -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));
}
... ...
... ... @@ -26,7 +26,7 @@ class ScreenUtilInit extends StatelessWidget {
if (constraints.maxWidth != 0) {
ScreenUtil.init(
constraints,
orientation,
orientation: orientation,
designSize: designSize,
allowFontScaling: allowFontScaling,
);
... ...