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
李卓原
2022-01-12 17:52:22 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
69d6e292994fb9e8d3cfd3689c4c820a113f8eac
69d6e292
1 parent
f7a0f7cb
fix #310
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
33 additions
and
40 deletions
CHANGELOG.md
example/lib/main.dart
lib/screen_util.dart
lib/screenutil_init.dart
pubspec.yaml
CHANGELOG.md
View file @
69d6e29
# 5.0.4
-
add setContext() , the first initialization method requires calling
-
fix # 310
# 5.0.3
-
init method add "context" param
...
...
example/lib/main.dart
View file @
69d6e29
...
...
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import
'package:flutter_screenutil/flutter_screenutil.dart'
;
void
main
(
)
{
WidgetsFlutterBinding
.
ensureInitialized
();
runApp
(
MyApp
());
}
...
...
@@ -22,6 +21,7 @@ class MyApp extends StatelessWidget {
textTheme:
TextTheme
(
button:
TextStyle
(
fontSize:
45
.
sp
)),
),
builder:
(
context
,
widget
)
{
ScreenUtil
.
setContext
(
context
);
return
MediaQuery
(
//Setting font does not change with system font size
data:
MediaQuery
.
of
(
context
).
copyWith
(
textScaleFactor:
1.0
),
...
...
lib/screen_util.dart
View file @
69d6e29
...
...
@@ -15,13 +15,10 @@ class ScreenUtil {
///屏幕方向
late
Orientation
_orientation
;
late
double
_pixelRatio
;
late
double
_textScaleFactor
;
late
double
_screenWidth
;
late
double
_screenHeight
;
late
double
_statusBarHeight
;
late
double
_bottomBarHeight
;
late
bool
_minTextAdapt
;
late
BuildContext
?
context
;
ScreenUtil
.
_
();
...
...
@@ -29,6 +26,10 @@ class ScreenUtil {
return
_instance
;
}
static
void
setContext
(
BuildContext
context
)
{
_instance
.
context
=
context
;
}
static
void
init
(
BoxConstraints
constraints
,
{
BuildContext
?
context
,
...
...
@@ -45,17 +46,7 @@ class ScreenUtil {
..
_screenHeight
=
splitScreenMode
?
max
(
constraints
.
maxHeight
,
700
)
:
constraints
.
maxHeight
;
var
windowData
;
if
(
context
!=
null
)
{
windowData
=
MediaQuery
.
of
(
context
);
}
else
{
windowData
=
WidgetsBinding
.
instance
?.
window
??
ui
.
window
;
}
_instance
.
_pixelRatio
=
windowData
.
devicePixelRatio
;
_instance
.
_statusBarHeight
=
windowData
.
padding
.
top
;
_instance
.
_bottomBarHeight
=
windowData
.
padding
.
bottom
;
_instance
.
_textScaleFactor
=
windowData
.
textScaleFactor
;
if
(
context
!=
null
)
setContext
(
context
);
}
///获取屏幕方向
...
...
@@ -64,11 +55,11 @@ class ScreenUtil {
/// 每个逻辑像素的字体像素数,字体的缩放比例
/// The number of font pixels for each logical pixel.
double
get
textScaleFactor
=>
_
textScaleFactor
;
double
get
textScaleFactor
=>
MediaQuery
.
of
(
context
!).
textScaleFactor
;
/// 设备的像素密度
/// The size of the media in logical pixels (e.g, the size of the screen).
double
get
pixelRatio
=>
_p
ixelRatio
;
double
?
get
pixelRatio
=>
MediaQuery
.
of
(
context
!).
deviceP
ixelRatio
;
/// 当前设备宽度 dp
/// The horizontal extent of this size.
...
...
@@ -80,11 +71,11 @@ class ScreenUtil {
/// 状态栏高度 dp 刘海屏会更高
/// The offset from the top, in dp
double
get
statusBarHeight
=>
_statusBarHeight
/
_pixelRatio
;
double
get
statusBarHeight
=>
MediaQuery
.
of
(
context
!).
padding
.
top
;
/// 底部安全区距离 dp
/// The offset from the bottom, in dp
double
get
bottomBarHeight
=>
_bottomBarHeight
/
_pixelRatio
;
double
get
bottomBarHeight
=>
MediaQuery
.
of
(
context
!).
padding
.
bottom
;
/// 实际尺寸与UI设计的比例
/// The ratio of actual width to UI design
...
...
lib/screenutil_init.dart
View file @
69d6e29
...
...
@@ -19,24 +19,21 @@ class ScreenUtilInit extends StatelessWidget {
@override
Widget
build
(
BuildContext
context
)
{
return
MediaQuery
(
data:
MediaQueryData
(),
child:
LayoutBuilder
(
builder:
(
_
,
BoxConstraints
constraints
)
{
if
(
constraints
.
maxWidth
!=
0
)
{
final
Orientation
orientation
=
constraints
.
maxWidth
>
constraints
.
maxHeight
?
Orientation
.
landscape
:
Orientation
.
portrait
;
ScreenUtil
.
init
(
constraints
,
context:
_
,
orientation:
orientation
,
designSize:
designSize
,
splitScreenMode:
splitScreenMode
,
minTextAdapt:
minTextAdapt
);
return
builder
();
}
return
Container
();
}),
);
return
LayoutBuilder
(
builder:
(
_
,
BoxConstraints
constraints
)
{
if
(
constraints
.
maxWidth
!=
0
)
{
final
Orientation
orientation
=
constraints
.
maxWidth
>
constraints
.
maxHeight
?
Orientation
.
landscape
:
Orientation
.
portrait
;
ScreenUtil
.
init
(
constraints
,
context:
_
,
orientation:
orientation
,
designSize:
designSize
,
splitScreenMode:
splitScreenMode
,
minTextAdapt:
minTextAdapt
);
return
builder
();
}
return
Container
();
});
}
}
...
...
pubspec.yaml
View file @
69d6e29
name
:
flutter_screenutil
description
:
A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
version
:
5.0.
3
version
:
5.0.
4
homepage
:
https://github.com/OpenFlutter/flutter_screenutil
publish_to
:
https://pub.dev
environment
:
sdk
:
"
>=2.12.0
<3.0.0"
...
...
Please
register
or
login
to post a comment