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
Mounir-Bouaiche
2022-05-05 13:56:59 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
cc2f4a33cea7d97f0ad32b2ac67b344b877fe866
cc2f4a33
1 parent
6189076b
Commit to test if fix is working
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
17 deletions
example/lib/src/home.dart
lib/src/screen_util.dart
example/lib/src/home.dart
View file @
cc2f4a3
...
...
@@ -23,6 +23,10 @@ class HomePageScaffold extends StatelessWidget {
Widget
build
(
BuildContext
context
)
{
printScreenInformation
();
/// Uncomment if you wanna force current widget to be rebuilt with updated values
/// Use it only if you use the second method, or if you use ScreenUtilInit's child.
/// Note: don't use it along with ScreenUtil.init()
// ScreenUtil.registerToBuild(context);
return
Scaffold
(
appBar:
AppBar
(
title:
Text
(
title
),
...
...
lib/src/screen_util.dart
View file @
cc2f4a3
...
...
@@ -10,11 +10,11 @@ import 'package:flutter/widgets.dart';
class
ScreenUtil
{
static
const
Size
defaultSize
=
Size
(
360
,
690
);
static
late
ScreenUtil
_instance
;
static
ScreenUtil
_instance
=
ScreenUtil
.
_
()
;
/// UI设计中手机尺寸 , dp
/// Size of the phone in UI Design , dp
late
Size
uiSize
;
late
Size
_
uiSize
;
///屏幕方向
late
Orientation
_orientation
;
...
...
@@ -22,7 +22,7 @@ class ScreenUtil {
late
double
_screenWidth
;
late
double
_screenHeight
;
late
bool
_minTextAdapt
;
BuildContext
?
context
;
BuildContext
?
_
context
;
late
bool
_splitScreenMode
;
ScreenUtil
.
_
();
...
...
@@ -69,16 +69,21 @@ class ScreenUtil {
}
}
Set
<
Element
>?
_elementsToRebuild
;
/// ### Experimental
/// Register current page and all its descendants to rebuild
/// Register current page and all its descendants to rebuild
.
/// Helpful when building for web and desktop
static
void
registerToBuild
(
BuildContext
context
,
[
bool
withDescendants
=
false
,
])
{
MediaQuery
.
maybeOf
(
context
);
final
instance
=
ScreenUtil
();
(
instance
.
_elementsToRebuild
??=
{}).
add
(
context
as
Element
);
// MediaQuery.maybeOf(context);
if
(
withDescendants
)
{
(
context
as
Element
)
.
visitChildren
((
element
)
{
context
.
visitChildren
((
element
)
{
registerToBuild
(
element
,
true
);
});
}
...
...
@@ -108,14 +113,16 @@ class ScreenUtil {
?
Orientation
.
landscape
:
Orientation
.
portrait
);
_instance
=
ScreenUtil
.
_
()
..
uiSize
=
designSize
_instance
..
_uiSize
=
designSize
..
_splitScreenMode
=
splitScreenMode
..
_minTextAdapt
=
minTextAdapt
..
_orientation
=
orientation
..
_screenWidth
=
deviceSize
.
width
..
_screenHeight
=
deviceSize
.
height
..
context
=
mediaQueryContext
!=
null
?
context
:
null
;
..
_context
=
context
;
_instance
.
_elementsToRebuild
?.
forEach
((
el
)
=>
el
.
markNeedsBuild
());
}
///获取屏幕方向
...
...
@@ -125,41 +132,41 @@ class ScreenUtil {
/// 每个逻辑像素的字体像素数,字体的缩放比例
/// The number of font pixels for each logical pixel.
double
get
textScaleFactor
=>
context
!=
null
?
MediaQuery
.
of
(
context
!).
textScaleFactor
:
1
;
_context
!=
null
?
MediaQuery
.
of
(
_
context
!).
textScaleFactor
:
1
;
/// 设备的像素密度
/// The size of the media in logical pixels (e.g, the size of the screen).
double
?
get
pixelRatio
=>
context
!=
null
?
MediaQuery
.
of
(
context
!).
devicePixelRatio
:
1
;
_context
!=
null
?
MediaQuery
.
of
(
_
context
!).
devicePixelRatio
:
1
;
/// 当前设备宽度 dp
/// The horizontal extent of this size.
double
get
screenWidth
=>
context
!=
null
?
MediaQuery
.
of
(
context
!).
size
.
width
:
_screenWidth
;
_context
!=
null
?
MediaQuery
.
of
(
_
context
!).
size
.
width
:
_screenWidth
;
///当前设备高度 dp
///The vertical extent of this size. dp
double
get
screenHeight
=>
context
!=
null
?
MediaQuery
.
of
(
context
!).
size
.
height
:
_screenHeight
;
_context
!=
null
?
MediaQuery
.
of
(
_
context
!).
size
.
height
:
_screenHeight
;
/// 状态栏高度 dp 刘海屏会更高
/// The offset from the top, in dp
double
get
statusBarHeight
=>
context
==
null
?
0
:
MediaQuery
.
of
(
context
!).
padding
.
top
;
_context
==
null
?
0
:
MediaQuery
.
of
(
_
context
!).
padding
.
top
;
/// 底部安全区距离 dp
/// The offset from the bottom, in dp
double
get
bottomBarHeight
=>
context
==
null
?
0
:
MediaQuery
.
of
(
context
!).
padding
.
bottom
;
_context
==
null
?
0
:
MediaQuery
.
of
(
_
context
!).
padding
.
bottom
;
/// 实际尺寸与UI设计的比例
/// The ratio of actual width to UI design
double
get
scaleWidth
=>
screenWidth
/
uiSize
.
width
;
double
get
scaleWidth
=>
screenWidth
/
_
uiSize
.
width
;
/// /// The ratio of actual height to UI design
double
get
scaleHeight
=>
(
_splitScreenMode
?
max
(
screenHeight
,
700
)
:
screenHeight
)
/
uiSize
.
height
;
_
uiSize
.
height
;
double
get
scaleText
=>
_minTextAdapt
?
min
(
scaleWidth
,
scaleHeight
)
:
scaleWidth
;
...
...
Please
register
or
login
to post a comment