OpenFlutter / flutter_screenutil

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

[Feature/Enhancement] Does not take the MediaQueryData of the parent #572

Open Blyynd opened 2 weeks ago

Blyynd commented 2 weeks ago

I have a specific case where i was in need of in ensureScreenSize (for a web container where the size of the screen is not the all screen, to render an actual mobile experience on web). I saw that is it deprecated in release notes.

I have to fork your package to force a mediaQueryData (initializing mq in your code)

    const double designWidth = 396;
    const double designHeight = 852;

    printOnDebug("Is web : ${kIsWeb.toString()}");

    double width = designWidth;
    double height = MediaQuery.sizeOf(context).height;
    if(!kIsWeb) {
      width = MediaQuery.sizeOf(context).width;
    }

    final customSize = Size(width, height);
    MediaQueryData data = MediaQuery.of(context).copyWith(
        size: customSize
    );

    return MediaQuery(
        data: data,
        child : Builder(
            builder: (context) {
              return ScreenUtilInit(
                designSize: const Size(designWidth, designHeight),
                data: data,
                minTextAdapt: true,
                builder: (context, child) {

                ....

In your class

class ScreenUtilInit extends StatefulWidget {
  /// A helper widget that initializes [ScreenUtil]
  const ScreenUtilInit({
    Key? key,
    this.builder,
    this.child,
    this.rebuildFactor = RebuildFactors.size,
    this.data,
    this.designSize = ScreenUtil.defaultSize,
    this.splitScreenMode = false,
    this.minTextAdapt = false,
    this.useInheritedMediaQuery = false,
    this.ensureScreenSize = false,
    this.enableScaleWH,
    this.enableScaleText,
    this.responsiveWidgets,
    this.excludeWidgets,
    this.fontSizeResolver = FontSizeResolvers.width,
  }) : super(key: key);

  final ScreenUtilInitBuilder? builder;
  final Widget? child;
  final bool splitScreenMode;
  final bool minTextAdapt;
  final bool useInheritedMediaQuery;
  final bool ensureScreenSize;
  final MediaQueryData? data;
  final bool Function()? enableScaleWH;
  final bool Function()? enableScaleText;
  final RebuildFactor rebuildFactor;
  final FontSizeResolver fontSizeResolver;

  /// The [Size] of the device in the design draft, in dp
  final Size designSize;
  final Iterable<String>? responsiveWidgets;
  final Iterable<String>? excludeWidgets;

and passing to mq :

  @override
  Widget build(BuildContext context) {
    MediaQueryData? mq = _mediaQueryData;
    if(widget.data != null) mq = widget.data;

It would be nice to have that native in your package