How can I initialize screen util package in a different package ? Or is it correct way to do it?

271

You just initialise your screen utils on your first page/screen or your initial route. You can initialise it inside your init() method of your stateful widget or initialise it inside your build() function of your stateless widget. Hope this is clear.

Share:
271
leylekseven
Author by

leylekseven

Updated on January 03, 2023

Comments

  • leylekseven
    leylekseven over 1 year

    Firstly, I am sorry for my poor english.

    I am trying success to extract a very complex widget as package and use it on different projects. For this reason, I need to use ScreenUtil package (for responsive design.) in that widget-package.

    So in the end, I succeed the extract my widget as a package but I am confused some point because as you know, screen_util package requires to be initialized like;

    ScreenUtilInit(
            designSize: const Size(750, 1334),
            builder: () => const MyApp(),
          )
    

    So my question, how can I success to initialize the screen_util package correctly for my usage ? (I don't have any builder function because my widgets created like class MyWidget extends SizedBox(important not like stateless or statefull widget!)) so,

    I added a function for initializing operation like ;

    void myComponentInitializer({
      required BuildContext context
    }){
    
      ScreenUtil.init(
          BoxConstraints(
              maxWidth: MediaQuery.of(context).size.width,
              maxHeight: MediaQuery.of(context).size.height
          ),
          context: context,
          designSize: const Size(750, 1334),
          minTextAdapt: true
      );
    
      ScreenUtil.setContext(context);
    }
    

    and before using the package, I am calling that function but I can't be sure, is it a best way to initialize screen_util for my widget or not. If it is not the right way, can you show me the right way ?

    So I am open any idea for this.

    Thanks a lot.

  • leylekseven
    leylekseven about 2 years
    Firstly sorry if I couldn't understand but I think your answer is not related my question. Because I am trying to ask, how can I initialize ScreenUtil for a package with no material app or no any widget hierarchy.
  • leylekseven
    leylekseven about 2 years
    Okay, I am doing this but my way is correct ? I mean, I am not getting any error and it seems correctly but is it the correct way ? (can you check my question last code part, that function on my package and I am calling it on project first build method.)
  • Md. Kamrul Amin
    Md. Kamrul Amin about 2 years
    As long as you call your myComponentInitializer function just in your initialise or first screen. This should be fine. If you dont put your screen utils in your first screen, you might get errors when you open ur app through notifications.