Skip to content

Commit

Permalink
删除screenutilInit添加的部分监听
Browse files Browse the repository at this point in the history
  • Loading branch information
李卓原 committed May 15, 2023
1 parent 264f66b commit f73f556
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 101 deletions.
1 change: 0 additions & 1 deletion example/lib/src/first_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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(
useInheritedMediaQuery: true,
builder: (_, child) {
return MaterialApp(
debugShowCheckedModeBanner: false,
Expand Down
1 change: 1 addition & 0 deletions example/lib/src/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class HomePageScaffold extends StatelessWidget {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
print('BottomSheet build');
return Container(
height: 200.w +
MediaQuery.of(context).viewInsets.bottom,
Expand Down
1 change: 0 additions & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void main() {
return MediaQuery(
data: currentData,
child: ScreenUtilInit(
useInheritedMediaQuery: true,
designSize: designSize,
builder: (context, child) => MaterialApp(
home: Material(
Expand Down
125 changes: 26 additions & 99 deletions lib/src/screenutil_init.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ class ScreenUtilInit extends StatefulWidget {
this.designSize = ScreenUtil.defaultSize,
this.splitScreenMode = false,
this.minTextAdapt = false,
this.useInheritedMediaQuery = false,
this.scaleByHeight = false})
: super(key: key);

final ScreenUtilInitBuilder builder;
final Widget? child;
final bool splitScreenMode;
final bool minTextAdapt;
final bool useInheritedMediaQuery;
final bool scaleByHeight;
final RebuildFactor rebuildFactor;

Expand All @@ -62,59 +60,18 @@ class _ScreenUtilInitState extends State<ScreenUtilInit>
with WidgetsBindingObserver {
MediaQueryData? _mediaQueryData;

bool wrappedInMediaQuery = false;

WidgetsBinding get binding => WidgetsFlutterBinding.ensureInitialized();

MediaQueryData get mediaQueryData => _mediaQueryData!;

MediaQueryData get newData {
if (widget.useInheritedMediaQuery) {
final data = MediaQuery.maybeOf(context);

if (data != null) {
wrappedInMediaQuery = true;
return data;
}
}

return MediaQueryData.fromView(View.of(context));
}

Widget get child {
return widget.builder.call(context, widget.child);
}

_updateTree(Element el) {
el.markNeedsBuild();
el.visitChildren(_updateTree);
}

@override
void initState() {
super.initState();
// mediaQueryData = newData;
binding.addObserver(this);
}

@override
void didChangeMetrics() {
final old = _mediaQueryData!;
final data = newData;

if (widget.scaleByHeight || widget.rebuildFactor(old, data)) {
_mediaQueryData = data;
_updateTree(context as Element);
}
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
if (_mediaQueryData == null) _mediaQueryData = newData;
didChangeMetrics();
}

@override
void dispose() {
binding.removeObserver(this);
Expand All @@ -123,62 +80,32 @@ class _ScreenUtilInitState extends State<ScreenUtilInit>

@override
Widget build(BuildContext _context) {
if (mediaQueryData.size == Size.zero) return const SizedBox.shrink();
if (!wrappedInMediaQuery) {
return MediaQuery(
// key: GlobalObjectKey('mediaQuery'),
data: mediaQueryData,
child: Builder(
builder: (__context) {
final deviceData = MediaQuery.maybeOf(__context);
final deviceSize = deviceData?.size ?? widget.designSize;
ScreenUtil.init(__context,
designSize: widget.designSize,
splitScreenMode: widget.splitScreenMode,
minTextAdapt: widget.minTextAdapt,
scaleByHeight: widget.scaleByHeight);
return Container(
width: deviceSize.width,
height: deviceSize.height,
child: FittedBox(
fit: BoxFit.none,
alignment: Alignment.center,
child: Container(
width: widget.scaleByHeight
? (deviceSize.height * widget.designSize.width) /
widget.designSize.height
: deviceSize.width,
height: deviceSize.height,
child: child,
),
));
},
),
);
}

ScreenUtil.init(_context,
designSize: widget.designSize,
splitScreenMode: widget.splitScreenMode,
minTextAdapt: widget.minTextAdapt,
scaleByHeight: widget.scaleByHeight);
final deviceData = MediaQuery.maybeOf(_context);

final deviceSize = deviceData?.size ?? widget.designSize;
return Container(
width: deviceSize.width,
height: deviceSize.height,
child: FittedBox(
fit: BoxFit.none,
alignment: Alignment.center,
child: Container(
width: widget.scaleByHeight
? (deviceSize.height * widget.designSize.width) /
widget.designSize.height
: deviceSize.width,
if (_mediaQueryData?.size == Size.zero) return const SizedBox.shrink();
return Builder(
builder: (__context) {
final deviceData = MediaQuery.maybeOf(__context);
final deviceSize = deviceData?.size ?? widget.designSize;
ScreenUtil.init(__context,
designSize: widget.designSize,
splitScreenMode: widget.splitScreenMode,
minTextAdapt: widget.minTextAdapt,
scaleByHeight: widget.scaleByHeight);
return Container(
width: deviceSize.width,
height: deviceSize.height,
child: child,
),
));
child: FittedBox(
fit: BoxFit.none,
alignment: Alignment.center,
child: Container(
width: widget.scaleByHeight
? (deviceSize.height * widget.designSize.width) /
widget.designSize.height
: deviceSize.width,
height: deviceSize.height,
child: child,
),
));
},
);
}
}

0 comments on commit f73f556

Please sign in to comment.