OpenFlutter / flutter_screenutil

Flutter screen adaptation, font adaptation, get screen information
https://pub.dartlang.org/packages/flutter_screenutil
Apache License 2.0
3.88k stars 494 forks source link

No MediaQuery widget ancestor found #336

Closed longfit closed 2 years ago

longfit commented 2 years ago

flutter master 2.10-0-1.0.pre.333 After I upgrade from 5.0.1 to 5.1.1 version and modified , it can work properly

But I have upgraded flutter master 2.11.0-0.0.pre.455 Cannot work properly

Error Shown

Exception has occurred. FlutterError (No MediaQuery widget ancestor found. LayoutBuilder widgets require a MediaQuery widget ancestor. The specific widget that could not find a MediaQuery ancestor was: LayoutBuilder The ownership chain for the affected widget is: "LayoutBuilder ← ScreenUtilInit ← MyApp ← [root]" No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.)

Based On flutter_screenutil 5.1.1 Widget can work ( flutter master 2.10-0-1.0.pre.333) , Fail flutter master 2.11.0-0.0.pre.455

ScreenUtil().setSp(19) which came out above error

based on method 1 Where is wrong caused new flutter master 2.11.0-0.0.pre.455

class MyApp extends StatelessWidget { // This widget is the root of your application.

final ThemeData myTheme = ThemeData(); @override Widget build(BuildContext context) { // ScreenUtil = ScreenUtil(width: 750, height: 1334)..init(context);

// var _userViewModel;
return ScreenUtilInit(
    // designSize: Size(750, 1334),
    designSize: Size(360, 690),
    minTextAdapt: true,
    splitScreenMode: true,
    // allowFontScaling: false,
    builder: () => MultiProvider(
            providers: [
              // ChangeNotifierProvider<CartViewModel>.value(value: _cartViewModel),
              // ChangeNotifierProvider<UserViewModel>.value(
              //     value: _userViewModel),
              ChangeNotifierProvider(create: (_) => UserViewModel()),
              ChangeNotifierProvider(create: (_) => MunitViewModel()),
              ChangeNotifierProvider(create: (_) => VendorViewModel()),
              ChangeNotifierProvider(create: (_) => StockTakenViewModel()),
              ChangeNotifierProvider(create: (_) => MedicineViewModel()),
              ChangeNotifierProvider(create: (_) => MediSearchViewModel()),
              ChangeNotifierProvider(create: (_) => PoViewModel()),
              ChangeNotifierProvider(create: (_) => PoDetViewModel()),
            ],
            child: MaterialApp(
              localizationsDelegates: [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              supportedLocales: [
                const Locale('zh', 'CH'),
                const Locale('en', 'US'),
              ],
              locale: const Locale('zh'),
              title: 'Login',
              theme: ThemeData(
                brightness: Brightness.light,
                primaryColor: primaryColor,
                //primaryColor: Colors.blue,
                // primarySwatch: Colors.blue,
                unselectedWidgetColor: Colors.redAccent,
                colorScheme: ColorScheme.fromSwatch()
                    .copyWith(secondary: accentColor),

                //  colorScheme: myTheme.colorScheme.copyWith(secondaryColor: myColor),
              ),
              debugShowCheckedModeBanner: false,
              home: LoginYst(),
              // builder: EasyLoading.init(),
              //below builder modified by wing 2022/02/07
              builder: EasyLoading.init(builder: (context, widget) {
                //add this line
                ScreenUtil.setContext(context);

                return MediaQuery(
                  //Setting font does not change with system font size
                  data:
                      MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  child: widget,
                );
              }),
              // builder: EasyLoading.init(),
            )));

} }

best Regards

lizhuoyuan commented 2 years ago

Not yet adapted for the latest version

radhen-thirstydevs commented 2 years ago

Is there any solution for this issue?

lizhuoyuan commented 2 years ago

try this:

 return MediaQuery(
      data: MediaQueryData.fromWindow(WidgetsBinding.instance.window),
      child: ScreenUtilInit(
...

or try master

longfit commented 2 years ago

Same result FlutterError (No MediaQuery widget ancestor found. LayoutBuilder widgets require a MediaQuery widget ancestor.

class MyApp extends StatelessWidget { // This widget is the root of your application.

final ThemeData myTheme = ThemeData(); @override Widget build(BuildContext context) {

return
 MediaQuery(
  data: MediaQueryData.fromWindow(WidgetsBinding.instance.window),
  child: 

 ScreenUtilInit(
    // designSize: Size(750, 1334),
    designSize: Size(360, 690),
    minTextAdapt: true,
    splitScreenMode: true,
    // allowFontScaling: false,
    builder: () => MultiProvider(
            providers: [
              // ChangeNotifierProvider<CartViewModel>.value(value: _cartViewModel),
              // ChangeNotifierProvider<UserViewModel>.value(
              //     value: _userViewModel),
              ChangeNotifierProvider(create: (_) => UserViewModel()),
              ChangeNotifierProvider(create: (_) => MunitViewModel()),
              ChangeNotifierProvider(create: (_) => VendorViewModel()),
              ChangeNotifierProvider(create: (_) => StockTakenViewModel()),
              ChangeNotifierProvider(create: (_) => MedicineViewModel()),
              ChangeNotifierProvider(create: (_) => MediSearchViewModel()),
              ChangeNotifierProvider(create: (_) => PoViewModel()),
              ChangeNotifierProvider(create: (_) => PoDetViewModel()),
            ],
            child: MaterialApp(
              localizationsDelegates: [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              supportedLocales: [
                const Locale('zh', 'CH'),
                const Locale('en', 'US'),
              ],
              locale: const Locale('zh'),
              title: '一心堂',

              debugShowCheckedModeBanner: false,
              home: LoginYst(),
              // builder: EasyLoading.init(),
              //below builder modified by wing 2022/02/07
              builder: EasyLoading.init(builder: (context,home) {
                //add this line
                ScreenUtil.setContext(context);

                return MediaQuery(
                  //Setting font does not change with system font size
                  data:
                      MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  child: home,
                );
              }),
              // builder: EasyLoading.init(),
            ))));

} }

lizhuoyuan commented 2 years ago

change to

ScreenUtilInit(
  MaterialApp(

Adjusting the position of ScreenUtilInit Or use the second initialization method