OpenFlutter / flutter_screenutil

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

Use of setting MediaQuery on ScreenUtilInit widget #489

Closed shreejeetp closed 1 year ago

shreejeetp commented 1 year ago

I was looking up the code of the library. Im using v 5.2.0 rn. At the top, you guys are using MediaQuery to set mediaquery data, but i cant see where is the need to set the mediaquery data here. Apologies if this isn't the right place to ask.

Reference code:

class ScreenUtilInit extends StatelessWidget {
  /// A helper widget that initializes [ScreenUtil]
  ScreenUtilInit({
    required this.builder,
    this.designSize = ScreenUtil.defaultSize,
    this.splitScreenMode = false,
    this.minTextAdapt = false,
    Key? key,
  }) : super(key: key);

  final Widget Function() builder;
  final bool splitScreenMode;
  final bool minTextAdapt;

  /// The [Size] of the device in the design draft, in dp
  final Size designSize;

  @override
  Widget build(BuildContext context) {
    return MediaQuery(       //  <- - - - - - This one
      data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window),
      child: LayoutBuilder(builder: (_, BoxConstraints constraints) {
        if (constraints.maxWidth != 0) {
          final Orientation orientation =
              constraints.maxWidth > constraints.maxHeight
                  ? Orientation.landscape
                  : Orientation.portrait;
          ScreenUtil.init(constraints,
              context: _,
              orientation: orientation,
              designSize: designSize,
              splitScreenMode: splitScreenMode,
              minTextAdapt: minTextAdapt);
          return builder();
        }
        return Container();
      }),
    );
  }
}
lizhuoyuan commented 1 year ago

Use it to get screen data

shreejeetp commented 1 year ago

This line is causing issues in latest release in regards to opening the keyboard and app rebuilding. cant we just do a init with screen dimensions and update on change and not set it for whole app?

lizhuoyuan commented 1 year ago

The latest version will only cause the components of the current dialog class to be rebuilt, and will not cause the application to be rebuilt. try that plz

shreejeetp commented 1 year ago

I've tried the latest one (5.8.4). Unfortunately, its rebuilding the whole app for me. I've initialised ScreenUtilInit on top of material app and when the keyboard opens, it refreshes the whole app and the textfield also looses focus resulting in keyboard being closed.

Edit: I just wanted to add that it only rebuilds when useInheritedMediaQuery is set, but if its not set, the keyboard doesn't shift the app up when its opened.

naivetoby commented 1 year ago

I've tried the latest one (5.8.4). Unfortunately, its rebuilding the whole app for me. I've initialised ScreenUtilInit on top of material app and when the keyboard opens, it refreshes the whole app and the textfield also looses focus resulting in keyboard being closed.

Edit: I just wanted to add that it only rebuilds when useInheritedMediaQuery is set, but if its not set, the keyboard doesn't shift the app up when its opened.

Choose any of the following to rebuild when the keyboard opens:

  1. Set Scaffold's resizeToAvoidBottomInset to true
  2. MediaQuery.viewInsetsOf(context);

Reference:

https://github.com/flutter/flutter/pull/116090#issuecomment-1356014347

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.