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-04-30 23:38:14 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
1d2a41c5d20ee2b81164a09e0479147542ba0cb8
1d2a41c5
1 parent
8e7570e0
Bug Fix: Assertion Failed (Find MediaQuery in ancestors)
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
138 additions
and
18 deletions
example/lib/main.dart
example/lib/src/first_method.dart
example/lib/src_zh/first_method.dart
lib/src/screen_util.dart
lib/src/screenutil_init.dart
example/lib/main.dart
View file @
1d2a41c
import
'package:flutter/widgets.dart'
;
/*
import 'package:flutter/widgets.dart';
import 'src/first_method.dart' as firstMethod;
import 'src/second_method.dart' as secondMethod;
...
...
@@ -6,3 +6,106 @@ void main() {
const method = int.fromEnvironment('method', defaultValue: 1);
runApp(method == 1 ? firstMethod.MyApp() : secondMethod.MyApp());
}
*/
import
'dart:async'
;
import
'package:flutter/material.dart'
;
import
'package:flutter_screenutil/flutter_screenutil.dart'
;
void
main
(
)
{
runApp
(
ScreenUtilInit
(
builder:
(
child
)
=>
MaterialApp
(
key:
GlobalObjectKey
(
'screenutil'
),
theme:
ThemeData
(
textTheme:
TextTheme
(
bodyText2:
TextStyle
(
fontSize:
32
.
sp
),
),
),
home:
child
,
),
child:
ThirdPage
(),
));
}
class
MyStatelessElement
<
T
extends
TestPage
>
extends
StatelessElement
{
MyStatelessElement
(
T
widget
)
:
super
(
widget
);
@override
T
get
widget
=>
super
.
widget
as
T
;
@override
void
mount
(
Element
?
parent
,
Object
?
newSlot
)
{
super
.
mount
(
parent
,
newSlot
);
print
(
'
${widget.text()}
is mounted'
);
}
@override
void
unmount
()
{
print
(
'
${widget.text()}
is unmounted'
);
super
.
unmount
();
}
}
abstract
class
TestPage
extends
StatelessWidget
{
String
text
()
=>
runtimeType
.
toString
();
Widget
goto
();
@override
StatelessElement
createElement
()
=>
MyStatelessElement
(
this
);
@override
Widget
build
(
BuildContext
context
)
{
Timer
(
const
Duration
(
seconds:
5
),
()
{
Navigator
.
of
(
context
).
pushAndRemoveUntil
(
MaterialPageRoute
(
builder:
(
context
)
=>
goto
()),
(
route
)
=>
false
,
);
});
return
Scaffold
(
body:
SafeArea
(
child:
Padding
(
padding:
const
EdgeInsets
.
all
(
20
).
r
,
child:
Column
(
mainAxisSize:
MainAxisSize
.
min
,
children:
[
TextField
(),
Text
(
text
(),
style:
TextStyle
(
fontSize:
32
.
sp
),
),
Text
(
text
()),
Expanded
(
child:
ListView
.
separated
(
shrinkWrap:
true
,
itemBuilder:
(
context
,
index
)
=>
Text
(
'
$index
'
),
separatorBuilder:
(
_
,
__
)
=>
Container
(
height:
50
.
h
,
color:
Colors
.
green
,
),
itemCount:
10
,
),
),
],
),
),
),
);
}
}
class
FirstPage
extends
TestPage
{
@override
Widget
goto
()
=>
SecondPage
();
}
class
SecondPage
extends
TestPage
{
@override
Widget
goto
()
=>
FirstPage
();
}
class
ThirdPage
extends
TestPage
{
@override
Widget
goto
()
=>
FirstPage
();
}
...
...
example/lib/src/first_method.dart
View file @
1d2a41c
...
...
@@ -9,20 +9,19 @@ class MyApp extends StatelessWidget {
Widget
build
(
BuildContext
context
)
{
// In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it
return
ScreenUtilInit
(
builder:
(
c
ontext
)
{
builder:
(
c
hild
)
{
return
MaterialApp
(
debugShowCheckedModeBanner:
false
,
// Use this line to prevent extra rebuilds
useInheritedMediaQuery:
true
,
title:
'First Method'
,
// You can use the library anywhere in the app even in theme
theme:
ThemeData
(
primarySwatch:
Colors
.
blue
,
textTheme:
TextTheme
(
bodyText2:
TextStyle
(
fontSize:
16
.
sp
)),
),
home:
HomePage
(
title:
'First Method'
)
,
home:
child
,
);
},
child:
HomePage
(
title:
'First Method'
),
);
}
}
...
...
example/lib/src_zh/first_method.dart
View file @
1d2a41c
...
...
@@ -9,7 +9,7 @@ class MyApp extends StatelessWidget {
Widget
build
(
BuildContext
context
)
{
// In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it
return
ScreenUtilInit
(
builder:
(
c
ontext
)
{
builder:
(
c
hild
)
{
return
MaterialApp
(
debugShowCheckedModeBanner:
false
,
title:
'第一种方法'
,
...
...
@@ -18,9 +18,10 @@ class MyApp extends StatelessWidget {
primarySwatch:
Colors
.
blue
,
textTheme:
TextTheme
(
bodyText2:
TextStyle
(
fontSize:
30
.
sp
)),
),
home:
HomePage
(
title:
'第一种方法'
)
,
home:
child
,
);
},
child:
HomePage
(
title:
'第一种方法'
),
);
}
}
...
...
lib/src/screen_util.dart
View file @
1d2a41c
...
...
@@ -69,8 +69,19 @@ class ScreenUtil {
}
}
static
void
setContext
(
BuildContext
context
)
{
_instance
.
context
=
context
;
/// ### Experimental
/// 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
);
if
(
withDescendants
)
{
(
context
as
Element
).
visitChildren
((
element
)
{
registerToBuild
(
element
,
true
);
});
}
}
/// Initializing the library.
...
...
@@ -89,6 +100,8 @@ class ScreenUtil {
?
MediaQuery
.
of
(
context
!).
nonEmptySizeOrNull
()
:
null
;
mediaQueryContext
?.
visitChildren
((
el
)
=>
context
=
el
);
deviceSize
??=
deviceData
?.
size
??
designSize
;
orientation
??=
deviceData
?.
orientation
??
(
deviceSize
.
width
>
deviceSize
.
height
...
...
@@ -102,7 +115,7 @@ class ScreenUtil {
..
_orientation
=
orientation
..
_screenWidth
=
deviceSize
.
width
..
_screenHeight
=
deviceSize
.
height
..
context
=
mediaQueryContext
;
..
context
=
mediaQueryContext
!=
null
?
context
:
null
;
}
///获取屏幕方向
...
...
lib/src/screenutil_init.dart
View file @
1d2a41c
...
...
@@ -6,14 +6,16 @@ import 'screen_util.dart';
class
ScreenUtilInit
extends
StatelessWidget
{
/// A helper widget that initializes [ScreenUtil]
const
ScreenUtilInit
({
required
this
.
builder
,
this
.
builder
,
this
.
child
,
this
.
designSize
=
ScreenUtil
.
defaultSize
,
this
.
splitScreenMode
=
false
,
this
.
minTextAdapt
=
false
,
Key
?
key
,
})
:
super
(
key:
key
);
final
WidgetBuilder
builder
;
final
Widget
Function
(
Widget
?
child
)?
builder
;
final
Widget
?
child
;
final
bool
splitScreenMode
;
final
bool
minTextAdapt
;
...
...
@@ -25,11 +27,13 @@ class ScreenUtilInit extends StatelessWidget {
bool
firstFrameAllowed
=
false
;
RendererBinding
.
instance
!.
deferFirstFrame
();
return
MediaQuery
.
fromWindow
(
child:
Builder
(
builder:
(
context
)
{
if
(
MediaQuery
.
of
(
context
).
size
==
Size
.
zero
)
return
const
SizedBox
();
return
LayoutBuilder
(
builder:
(
context
,
constraints
)
{
if
(
constraints
.
biggest
==
Size
.
zero
)
return
const
SizedBox
.
shrink
();
ScreenUtil
.
init
(
context
,
null
,
deviceSize:
constraints
.
biggest
,
designSize:
designSize
,
splitScreenMode:
splitScreenMode
,
minTextAdapt:
minTextAdapt
,
...
...
@@ -40,8 +44,8 @@ class ScreenUtilInit extends StatelessWidget {
firstFrameAllowed
=
true
;
}
return
builder
(
context
);
}),
return
builder
?.
call
(
child
)
??
child
!;
},
);
}
}
...
...
Please
register
or
login
to post a comment