Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
flutter_screenutil
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
李卓原
2021-04-09 11:06:49 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
70322175c205b573ebc439a7af05936040e2ea28
70322175
1 parent
20ac1de0
5.0.0,null-safety最终版本.
更新内容参考changelog.md
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
55 additions
and
74 deletions
CHANGELOG.md
README.md
README_CN.md
README_PT.md
example/README.md
example/lib/main.dart
example/lib/main_zh.dart
lib/flutter_screenutil.dart
lib/screen_util.dart
lib/screenutil_init.dart
lib/size_extension.dart
pubspec.yaml
CHANGELOG.md
View file @
7032217
# 5.0.0
-Breaking change. Use a new way to set font scaling
-Deprecated ssp and nsp
# 5.0.0-nullsafety.11
-
revert 5.0.0-nullsafety.10
-
fix #230
...
...
README.md
View file @
7032217
...
...
@@ -35,7 +35,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
|Property|Type|Default Value|Description|
|:---|:---|:---|:---|
|designSize|Size|Size(360, 690)|The size of the device in the design draft, in dp|
|allowFontScaling|bool|false|Sets whether the font size is scaled according to the system's "font size" assist option|
### 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.
...
...
@@ -92,7 +91,6 @@ class _HomePageState extends State<HomePage> {
maxWidth: MediaQuery.of(context).size.width,
maxHeight: MediaQuery.of(context).size.height),
designSize: Size(360, 690),
allowFontScaling: false,
orientation: Orientation.portrait);
return Scaffold();
}
...
...
@@ -110,8 +108,6 @@ class _HomePageState extends State<HomePage> {
ScreenUtil
().
setHeight
(
200
)
(
dart
sdk
>=
2.6
:
200
.
h
)
//Adapted to screen height , under normal circumstances, the height still uses x.w
ScreenUtil
().
radius
(
200
)
(
dart
sdk
>=
2.6
:
200
.
r
)
//Adapt according to the smaller of width or height
ScreenUtil
().
setSp
(
24
)
(
dart
sdk
>=
2.6
:
24
.
sp
)
//Adapter font
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
true
)
(
dart
sdk
>=
2.6
:
24
.
ssp
)
//Adapter font(fonts will scale to respect Text Size accessibility settings)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
false
)
(
dart
sdk
>=
2.6
:
24
.
nsp
)
//Adapter font(fonts will not scale to respect Text Size accessibility settings)
ScreenUtil
().
pixelRatio
//Device pixel density
ScreenUtil
().
screenWidth
(
dart
sdk
>=
2.6
:
1
.
sw
)
//Device width
...
...
@@ -194,15 +190,6 @@ Container(
ScreenUtil
().
setSp
(
28
)
28
.
sp
//Incoming font size,the unit is pixel,fonts will scale to respect Text Size accessibility settings
//(If somewhere follow the global allowFontScaling setting)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
true
)
28
.
ssp
//(If somewhere does not follow the global allowFontScaling setting)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
false
)
28
.
nsp
//for example:
Column
(
crossAxisAlignment:
CrossAxisAlignment
.
start
,
...
...
README_CN.md
View file @
7032217
...
...
@@ -40,7 +40,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
|属性|类型|默认值|描述|
|:---|:---|:---|:---|
|designSize|Size|Size(360, 690)|设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)|
|allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放|
### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致)
...
...
@@ -103,7 +102,6 @@ class _HomePageState extends State<HomePage> {
maxWidth: MediaQuery.of(context).size.width,
maxHeight: MediaQuery.of(context).size.height),
designSize: Size(360, 690),
allowFontScaling: false,
orientation: Orientation.portrait);
return Scaffold();
}
...
...
@@ -119,8 +117,6 @@ class _HomePageState extends State<HomePage> {
ScreenUtil
().
setHeight
(
200
)
(
sdk
>=
2.6
:
200
.
h
)
//根据屏幕高度适配尺寸(一般根据宽度适配即可)
ScreenUtil
().
radius
(
200
)
(
sdk
>=
2.6
:
200
.
r
)
//根据宽度或高度中的较小者进行调整
ScreenUtil
().
setSp
(
24
)
(
sdk
>=
2.6
:
24
.
sp
)
//适配字体
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
true
)
(
sdk
>=
2.6
:
24
.
ssp
)
//适配字体(根据系统的“字体大小”辅助选项来进行缩放)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
false
)
(
sdk
>=
2.6
:
24
.
nsp
)
//适配字体(不会根据系统的“字体大小”辅助选项来进行缩放)
ScreenUtil
.
pixelRatio
//设备的像素密度
ScreenUtil
.
screenWidth
(
sdk
>=
2.6
:
1
.
sw
)
//设备宽度
...
...
@@ -197,12 +193,6 @@ ScreenUtil().setSp(28)
或
28
.
sp
(
dart
sdk
>=
2.6
)
//传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
true
)
或
24
.
ssp
(
dart
sdk
>=
2.6
)
//for example:
Column
(
...
...
README_PT.md
View file @
7032217
...
...
@@ -38,7 +38,6 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
|:---|:---|:---|:---|
|width|double|360dp|A largura do dispositivo no protótipo de design, em dp|
|height|double|690dp|A altura do dispositivo no protótipo de design, em dp|
|allowFontScaling|bool|false|Defina caso o tamanho da fonte seja dimensionado de acordo com a opção "tamanho de fonte" na acessibilidade do sistema|
### Inicialize e defina o tamanho de ajuste e tamanho da fonte para dimensionar de acordo com a opção "tamanho de fonte" na acessibilidade do sistema
Por favor, defina a largura e altura do protótipo de design antes de usar (em pixels).
...
...
@@ -54,7 +53,6 @@ class MyApp extends StatelessWidget {
//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
,
builder:
()
=>
MaterialApp
(
...
),
...
...
@@ -112,8 +110,6 @@ class _HomePageState extends State<HomePage> {
ScreenUtil
().
setHeight
(
200
)
(
sdk
>=
2.6
:
200
.
h
)
//Adapted to screen height
ScreenUtil
().
radius
(
200
)
(
dart
sdk
>=
2.6
:
200
.
r
)
//Adapt according to the smaller of width or height
ScreenUtil
().
setSp
(
24
)
(
sdk
>=
2.6
:
24
.
sp
)
//Adapter font
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
true
)
(
sdk
>=
2.6
:
24
.
ssp
)
//Adapter font(fonts will scale to respect Text Size accessibility settings)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
false
)
(
sdk
>=
2.6
:
24
.
nsp
)
//Adapter font(fonts will not scale to respect Text Size accessibility settings)
ScreenUtil
.
pixelRatio
//Device pixel density
ScreenUtil
.
screenWidth
(
sdk
>=
2.6
:
1
.
sw
)
//Device width
...
...
@@ -189,8 +185,7 @@ Container(
ScreenUtil
().
setSp
(
28
)
//Tamanho da fonte informado,em pixels,a fonte irá dimensionar respeitando a opção "Tamanho de Fonte" nas configurações de acessibilidade
//(Se em algum lugar não seguir a configuração global da propriedade allowFontScaling)
ScreenUtil
().
setSp
(
24
,
allowFontScalingSelf:
true
)
ScreenUtil
().
setSp
(
24
)
//Exemplo:
...
...
@@ -240,7 +235,7 @@ class MyApp extends StatelessWidget {
return
LayoutBuilder
(
builder:
(
context
,
constraints
)
{
//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)
ScreenUtil
.
init
(
constraints
,
designSize:
Size
(
750
,
1334
)
,
allowFontScaling:
false
);
ScreenUtil
.
init
(
constraints
,
designSize:
Size
(
750
,
1334
));
return
MaterialApp
(
debugShowCheckedModeBanner:
false
,
...
...
example/README.md
View file @
7032217
...
...
@@ -10,7 +10,7 @@
void
main
(
)
{
WidgetsFlutterBinding
.
ensureInitialized
();
//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)
ScreenUtil
.
init
(
designSize:
Size
(
750
,
1334
)
,
allowFontScaling:
false
);
ScreenUtil
.
init
(
designSize:
Size
(
750
,
1334
));
runApp
(
MyApp
());
}
...
...
example/lib/main.dart
View file @
7032217
...
...
@@ -9,13 +9,19 @@ class MyApp extends StatelessWidget {
//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
,
),
builder:
(
context
,
widget
)
{
return
MediaQuery
(
//Setting font does not change with system font size
data:
MediaQuery
.
of
(
context
).
copyWith
(
textScaleFactor:
1.0
),
child:
widget
,
);
},
home:
HomePage
(
title:
'FlutterScreenUtil Demo'
),
),
);
...
...
@@ -41,7 +47,7 @@ class _HomePageState extends State<HomePage> {
),
body:
SingleChildScrollView
(
child:
Column
(
crossAxisAlignment:
CrossAxisAlignment
.
center
,
crossAxisAlignment:
CrossAxisAlignment
.
start
,
children:
<
Widget
>[
Row
(
children:
<
Widget
>[
...
...
@@ -82,7 +88,8 @@ class _HomePageState extends State<HomePage> {
width:
100
.
r
,
height:
100
.
r
,
color:
Colors
.
green
,
child:
Text
(
'I am a square with a side length of 100'
,
child:
Text
(
'I am a square with a side length of 100'
,
style:
TextStyle
(
color:
Colors
.
white
,
fontSize:
ScreenUtil
().
setSp
(
12
),
...
...
@@ -106,21 +113,23 @@ class _HomePageState extends State<HomePage> {
height:
10
.
h
,
),
Text
(
'System font scaling factor:
${ScreenUtil().textScaleFactor}
'
),
SizedBox
(
height:
5
),
Column
(
crossAxisAlignment:
CrossAxisAlignment
.
start
,
children:
<
Widget
>[
Text
(
'
My font size is 16sp on the design draft and
will not change with the system.'
,
'
16sp,
will not change with the system.'
,
style:
TextStyle
(
color:
Colors
.
black
,
fontSize:
16
.
n
sp
,
fontSize:
16
.
sp
,
),
textScaleFactor:
1.0
,
),
Text
(
'
My font size is 16sp on the design draft and
will change with the system.'
,
'
16sp,if data is not set in MediaQuery,my font size
will change with the system.'
,
style:
TextStyle
(
color:
Colors
.
black
,
fontSize:
16
.
s
s
p
,
fontSize:
16
.
sp
,
),
),
],
...
...
@@ -138,7 +147,8 @@ class _HomePageState extends State<HomePage> {
print
(
'Bottom safe zone distance dp:
${ScreenUtil().bottomBarHeight}
dp'
);
print
(
'Status bar height dp:
${ScreenUtil().statusBarHeight}
dp'
);
print
(
'The ratio of actual width to UI design:
${ScreenUtil().scaleWidth}
'
);
print
(
'The ratio of actual height to UI design:
${ScreenUtil().scaleHeight}
'
);
print
(
'The ratio of actual height to UI design:
${ScreenUtil().scaleHeight}
'
);
print
(
'System font scaling:
${ScreenUtil().textScaleFactor}
'
);
print
(
'0.5 times the screen width:
${0.5.sw}
dp'
);
print
(
'0.5 times the screen height:
${0.5.sh}
dp'
);
...
...
example/lib/main_zh.dart
View file @
7032217
...
...
@@ -35,7 +35,6 @@ class _HomePageState extends State<HomePage> {
maxWidth:
MediaQuery
.
of
(
context
).
size
.
width
,
maxHeight:
MediaQuery
.
of
(
context
).
size
.
height
),
designSize:
Size
(
360
,
690
),
allowFontScaling:
false
,
orientation:
Orientation
.
portrait
);
printScreenInformation
();
return
Scaffold
(
...
...
@@ -67,7 +66,9 @@ 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
))),
),
],
),
...
...
@@ -105,17 +106,18 @@ class _HomePageState extends State<HomePage> {
crossAxisAlignment:
CrossAxisAlignment
.
start
,
children:
<
Widget
>[
Text
(
'我的文字大小在设计稿上是16dp,不会随着系统的文字缩放比例变化'
,
'我的文字大小在设计稿上是16dp,
因为设置了`textScaleFactor`,所以
不会随着系统的文字缩放比例变化'
,
style:
TextStyle
(
color:
Colors
.
black
,
fontSize:
16
.
sp
,
),
textScaleFactor:
1.0
,
),
Text
(
'我的文字大小在设计稿上是16dp,会随着系统的文字缩放比例变化'
,
style:
TextStyle
(
color:
Colors
.
black
,
fontSize:
16
.
s
s
p
,
fontSize:
16
.
sp
,
),
),
],
...
...
@@ -134,8 +136,10 @@ 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'
);
...
...
lib/flutter_screenutil.dart
View file @
7032217
...
...
@@ -5,6 +5,13 @@
library
flutter_screenutil
;
export
'size_extension.dart'
;
export
'screen_util.dart'
;
export
'screenutil_init.dart'
;
import
'dart:math'
;
import
'dart:ui'
as
ui
;
import
'package:flutter/material.dart'
;
part
'screen_util.dart'
;
part
'screenutil_init.dart'
;
part
'size_extension.dart'
;
...
...
lib/screen_util.dart
View file @
7032217
...
...
@@ -2,11 +2,7 @@
* Created by 李卓原 on 2018/9/29.
* email: zhuoyuan93@gmail.com
*/
import
'dart:math'
;
import
'dart:ui'
as
ui
;
import
'package:flutter/material.dart'
;
part of
flutter_screenutil
;
class
ScreenUtil
{
static
const
Size
defaultSize
=
Size
(
360
,
690
);
...
...
@@ -16,10 +12,6 @@ class ScreenUtil {
/// Size of the phone in UI Design , dp
late
Size
uiSize
;
/// 控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为false。
/// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
late
bool
allowFontScaling
;
///屏幕方向
late
Orientation
_orientation
;
...
...
@@ -40,11 +32,9 @@ class ScreenUtil {
BoxConstraints
constraints
,
{
Orientation
orientation
=
Orientation
.
portrait
,
Size
designSize
=
defaultSize
,
bool
allowFontScaling
=
false
,
})
{
_instance
=
ScreenUtil
.
_
()
..
uiSize
=
designSize
..
allowFontScaling
=
allowFontScaling
..
_orientation
=
orientation
..
_screenWidth
=
constraints
.
maxWidth
..
_screenHeight
=
constraints
.
maxHeight
;
...
...
@@ -118,8 +108,5 @@ class ScreenUtil {
///- [fontSize] UI设计上字体的大小,单位dp.
///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
)
=>
fontSize
*
scaleText
;
}
...
...
lib/screenutil_init.dart
View file @
7032217
import
'package:flutter/material.dart'
;
import
'package:flutter_screenutil/screen_util.dart'
;
part of
flutter_screenutil
;
class
ScreenUtilInit
extends
StatelessWidget
{
/// A helper widget that initializes [ScreenUtil]
ScreenUtilInit
({
required
this
.
builder
,
this
.
designSize
=
ScreenUtil
.
defaultSize
,
this
.
allowFontScaling
=
false
,
Key
?
key
,
})
:
super
(
key:
key
);
...
...
@@ -15,9 +13,6 @@ class ScreenUtilInit extends StatelessWidget {
/// The [Size] of the device in the design draft, in dp
final
Size
designSize
;
/// Sets whether the font size is scaled according to the system's "font size" assist option
final
bool
allowFontScaling
;
@override
Widget
build
(
BuildContext
context
)
{
return
LayoutBuilder
(
builder:
(
_
,
BoxConstraints
constraints
)
{
...
...
@@ -28,7 +23,6 @@ class ScreenUtilInit extends StatelessWidget {
constraints
,
orientation:
orientation
,
designSize:
designSize
,
allowFontScaling:
allowFontScaling
,
);
return
builder
();
}
...
...
lib/size_extension.dart
View file @
7032217
import
'package:flutter_screenutil/screen_util.dart'
;
part of
flutter_screenutil
;
extension
SizeExtension
on
num
{
///[ScreenUtil.setWidth]
...
...
@@ -14,10 +14,13 @@ extension SizeExtension on num {
double
get
sp
=>
ScreenUtil
().
setSp
(
this
);
///[ScreenUtil.setSp]
double
get
ssp
=>
ScreenUtil
().
setSp
(
this
,
allowFontScalingSelf:
true
);
@Deprecated
(
'please use [sp]'
)
double
get
ssp
=>
ScreenUtil
().
setSp
(
this
);
///[ScreenUtil.setSp]
double
get
nsp
=>
ScreenUtil
().
setSp
(
this
,
allowFontScalingSelf:
false
);
@Deprecated
(
'please use [sp] , and set textScaleFactor: 1.0 , for example: Text("text", textScaleFactor: 1.0)'
)
double
get
nsp
=>
ScreenUtil
().
setSp
(
this
);
///屏幕宽度的倍数
///Multiple of screen width
...
...
pubspec.yaml
View file @
7032217
name
:
flutter_screenutil
description
:
A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
version
:
5.0.0
-nullsafety.11
version
:
5.0.0
homepage
:
https://github.com/OpenFlutter/flutter_screenutil
environment
:
...
...
Please
register
or
login
to post a comment