diff --git a/example/lib/src/first_method.dart b/example/lib/src/first_method.dart index bfe0b84..f298f10 100644 --- a/example/lib/src/first_method.dart +++ b/example/lib/src/first_method.dart @@ -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, diff --git a/example/lib/src/home.dart b/example/lib/src/home.dart index a6d60da..396e3e8 100644 --- a/example/lib/src/home.dart +++ b/example/lib/src/home.dart @@ -129,6 +129,7 @@ class HomePageScaffold extends StatelessWidget { showModalBottomSheet( context: context, builder: (BuildContext context) { + print('BottomSheet build'); return Container( height: 200.w + MediaQuery.of(context).viewInsets.bottom, diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index 2c8cf98..b94b915 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -42,7 +42,6 @@ void main() { return MediaQuery( data: currentData, child: ScreenUtilInit( - useInheritedMediaQuery: true, designSize: designSize, builder: (context, child) => MaterialApp( home: Material( diff --git a/lib/src/screenutil_init.dart b/lib/src/screenutil_init.dart index 77e677e..6c6fc1f 100644 --- a/lib/src/screenutil_init.dart +++ b/lib/src/screenutil_init.dart @@ -39,7 +39,6 @@ class ScreenUtilInit extends StatefulWidget { this.designSize = ScreenUtil.defaultSize, this.splitScreenMode = false, this.minTextAdapt = false, - this.useInheritedMediaQuery = false, this.scaleByHeight = false}) : super(key: key); @@ -47,7 +46,6 @@ class ScreenUtilInit extends StatefulWidget { final Widget? child; final bool splitScreenMode; final bool minTextAdapt; - final bool useInheritedMediaQuery; final bool scaleByHeight; final RebuildFactor rebuildFactor; @@ -62,59 +60,18 @@ class _ScreenUtilInitState extends State 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); @@ -123,62 +80,32 @@ class _ScreenUtilInitState extends State @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, + ), + )); + }, + ); } }