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 ancestor could be found starting from the context that was passed to MediaQuery.of(). #333

Closed kishangohel closed 2 years ago

kishangohel commented 2 years ago

This is breaking whole application. This kind of updates tested first and release.

please add solution if any one find.

@override
  Widget build(BuildContext context) {
    return DevicePreview(
      enabled: kDebugMode,
      builder: (context) => ScreenUtilInit(
        designSize: const Size(411, 683), // with reference of pixel 2 5.0"
        minTextAdapt: true,
        splitScreenMode: true,
        builder: () => GetMaterialApp(
          useInheritedMediaQuery: true,
          locale: DevicePreview.locale(context),
          builder: DevicePreview.appBuilder,
          debugShowCheckedModeBanner: false,
          initialRoute: AppRoutes.home,
          getPages: AppRoutes.routes,
          theme: lightTheme,
          darkTheme: darkTheme,
          themeMode: ThemeMode.light,
        ),
      ),
    );
  }
fly-qp commented 2 years ago

Experiencing the same problem on version 5.1.0. Downgrading to version 5.0.4 fixed this issue for me.

kishangohel commented 2 years ago

@lizhuoyuan please suggest or add working solution as most of developers getting same error after upgrading to 5.1.1 I have tried both solutions mentioned in document but not working. I have static file which has all Dimensions with static key

techouse commented 2 years ago

Duplicate of https://github.com/OpenFlutter/flutter_screenutil/issues/329

@kishangohel check my comment here how I dealt with the issue https://github.com/OpenFlutter/flutter_screenutil/issues/329#issuecomment-1025002303

error404sushant commented 2 years ago

Remove device preview and wrap Screen Util with MaterialApp. That will fix the crash.

CurrentIndex commented 2 years ago
// main.dart
void main() {
  runApp(const APP());
}

class APP extends StatelessWidget {
  const APP({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      builder: (context, widget) {
        return MediaQuery(
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: widget!,
        );
      },
      home: ScreenUtilInit(
        designSize: const Size(360, 690),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: () {
          // ScreenUtil.setContext(context);
          return Theme(
            data: ThemeData(
              textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)),
            ),
            child: SignPage(),
          );
        },
      ),
    );
  }
}
//sign_page.dart
class SignPage extends StatelessWidget {
  SignPage({Key? key}) : super(key: key);

  final ctrl = Get.put(SignCtrl());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "汉字 ABC abc !@#%^&",
          style: TextStyle(fontSize: 10.sp),
        ),
      ),
    );
  }
}

这样可以解决问题,我想可能是(flutter2.8)的问题,更改了某些渲染问题? This can solve the problem. I think it may be the problem of (fluent 2.8), which has changed some rendering problems?

W2NJL commented 2 years ago

This was absolutely destroying me tonight and I thought it was something I did. So glad to find this thread.

kishangohel commented 2 years ago

Remove device preview and wrap Screen Util with MaterialApp. That will fix the crash.

I want device preview to test multiple device UI behaviour. So I can't remove it.

sohrabonline commented 2 years ago

add ScreenUtil.setContext(context); in builder

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(360, 690),
      minTextAdapt: true,
      builder: () {
        return GetMaterialApp(
          builder: (context, widget) {
            **ScreenUtil.setContext(context);**
            return MediaQuery(
                data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                child: widget!);
          },
          debugShowCheckedModeBanner: false,
          theme: settingService.getThemeData(context),
          home: SplashPage(),
        );
      },
    );
  }
error404sushant commented 2 years ago

Found the fix Main.dart

void main() { WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]) .then((_) { runApp(MaterialApp( debugShowCheckedModeBanner: false, home: App())); }); }

App.dart

return ScreenUtilInit( builder: (){ return MaterialApp( theme: ThemeData( fontFamily: "LatoRegular" ), debugShowCheckedModeBanner: false, routes: { '/splash':(context)=>SplashScreen() },initialRoute: "/splash", ); }, //designSize: Size(screenWidth,screenHeight), );