Mounir-Bouaiche

Fix getting inherited MediaQuery when using DevicePreview

  1 +//
  2 +// Generated file. Do not edit.
  3 +//
  4 +
  5 +// ignore_for_file: directives_ordering
  6 +// ignore_for_file: lines_longer_than_80_chars
  7 +// ignore_for_file: depend_on_referenced_packages
  8 +
  9 +import 'package:shared_preferences_web/shared_preferences_web.dart';
  10 +
  11 +import 'package:flutter_web_plugins/flutter_web_plugins.dart';
  12 +
  13 +// ignore: public_member_api_docs
  14 +void registerPlugins(Registrar registrar) {
  15 + SharedPreferencesPlugin.registerWith(registrar);
  16 + registrar.registerMessageHandler();
  17 +}
  1 +import 'package:device_preview/device_preview.dart';
  2 +import 'package:flutter/foundation.dart';
1 import 'package:flutter/widgets.dart'; 3 import 'package:flutter/widgets.dart';
2 import 'src/first_method.dart' as firstMethod; 4 import 'src/first_method.dart' as firstMethod;
3 import 'src/second_method.dart' as secondMethod; 5 import 'src/second_method.dart' as secondMethod;
4 6
5 void main() { 7 void main() {
6 const method = int.fromEnvironment('method', defaultValue: 1); 8 const method = int.fromEnvironment('method', defaultValue: 1);
7 - runApp(method == 1 ? firstMethod.MyApp() : secondMethod.MyApp()); 9 + runApp(
  10 + DevicePreview(
  11 + enabled: kDebugMode && kIsWeb,
  12 + builder: (context) {
  13 + return method == 1 ? firstMethod.MyApp() : secondMethod.MyApp();
  14 + },
  15 + ),
  16 + );
8 } 17 }
@@ -9,9 +9,11 @@ class MyApp extends StatelessWidget { @@ -9,9 +9,11 @@ class MyApp extends StatelessWidget {
9 Widget build(BuildContext context) { 9 Widget build(BuildContext context) {
10 // In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it 10 // In first method you only need to wrap [MaterialApp] with [ScreenUtilInit] and that's it
11 return ScreenUtilInit( 11 return ScreenUtilInit(
  12 + useInheritedMediaQuery: true,
12 builder: (_, child) { 13 builder: (_, child) {
13 return MaterialApp( 14 return MaterialApp(
14 debugShowCheckedModeBanner: false, 15 debugShowCheckedModeBanner: false,
  16 + useInheritedMediaQuery: true,
15 title: 'First Method', 17 title: 'First Method',
16 // You can use the library anywhere in the app even in theme 18 // You can use the library anywhere in the app even in theme
17 theme: ThemeData( 19 theme: ThemeData(
@@ -19,8 +19,13 @@ dependencies: @@ -19,8 +19,13 @@ dependencies:
19 19
20 flutter_screenutil: 20 flutter_screenutil:
21 path: ../ 21 path: ../
  22 +
  23 + device_preview: ^1.1.0
22 24
23 dev_dependencies: 25 dev_dependencies:
24 flutter_test: 26 flutter_test:
25 sdk: flutter 27 sdk: flutter
26 test: ^1.15.7 28 test: ^1.15.7
  29 +
  30 +flutter:
  31 + uses-material-design: true
@@ -66,9 +66,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit> @@ -66,9 +66,7 @@ class _ScreenUtilInitState extends State<ScreenUtilInit>
66 66
67 MediaQueryData get newData { 67 MediaQueryData get newData {
68 if (widget.useInheritedMediaQuery) { 68 if (widget.useInheritedMediaQuery) {
69 - final el = context.getElementForInheritedWidgetOfExactType<MediaQuery>();  
70 - final mq = el?.widget as MediaQuery?;  
71 - final data = mq?.data; 69 + final data = MediaQuery.maybeOf(context);
72 70
73 if (data != null) { 71 if (data != null) {
74 wrappedInMediaQuery = true; 72 wrappedInMediaQuery = true;