1

I have trouble designing a flutter program.
When I run the program, and resize the program, everything breaks down and is not normal.

You can see in the picture below.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Mohebbikhah
  • 53
  • 1
  • 9
  • https://stackoverflow.com/a/71198656/7706354 – lava Feb 22 '22 at 07:50
  • Can you include your code-snippet? check more about [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) – Md. Yeasin Sheikh Feb 22 '22 at 09:03
  • You're resizing the window (and so you're on desktop), or you're just rotating the device (and so you're on mobile)? The approach changes a lot depending on this – il_boga Feb 22 '22 at 09:34
  • I develop application on my laptop @il_boga – Mohebbikhah Feb 22 '22 at 09:56
  • Ok, then you just have to develop your app to behave correctly when layout changes. There's no quick solution to that. You might find some help in this [package](https://pub.dev/packages/bitsdojo_window), until an official solution for desktop window sizes is released. – il_boga Feb 22 '22 at 10:01
  • I use the same package but the only problem is resizing. Is there a method or command for it to have a call to resize? @il_boga – Mohebbikhah Feb 22 '22 at 10:10
  • No, I don't think it has a method that triggers on resize. Instead, you can have a different layout by checking the window width. I'll write an answer with a sample, wait a second – il_boga Feb 22 '22 at 10:23
  • look, happens when changing size. It should be an order that connects the widget to the sides, like Android Studio where the widgets are attached to the side of the screen. @il_boga – Mohebbikhah Feb 22 '22 at 10:33

3 Answers3

4

If you want to listen on resize events, you must wrap your root widget in this:

  @override
  Widget build(BuildContext context) {
    return NotificationListener<SizeChangedLayoutNotification>(
      onNotification: (notification) {
        //here you can't use setState, so you can't have a bool on which differentiate the layout, 
        //but you should be able to call build(context) to force a rebuild
      },
      child: SizeChangedLayoutNotifier(
        child:  MediaQuery.of(context).size.width> 400?  //set your width threshold here
        //widget layout for small window
       :
        //widget layout for big window

As for your last comment, the fact that the widget doesn't fit the width is not flutter's fault. You are using widgets with a fixed size or others that won't extend by default to fit the available space. Scaffold should do that, for example: I tested the above code in an app of mine with a Scaffold as root, and the widgets stretch to adapt to the window's width.

il_boga
  • 1,305
  • 1
  • 13
  • 21
  • 1
    What do you think of LayoutBuilder? – Mohebbikhah Feb 22 '22 at 11:00
  • 1
    That's another good widget for the job, I didn't think of it. Probably also a simpler solution than the one I put in the answer. The thing I don't know if it will trigger a rebuild on resize, because the resizing itself doesn't make the widget tree rebuild. – il_boga Feb 22 '22 at 11:04
  • Maybe I can use obs values and change the size of the program in each build of the page and save it, and after changing that program, change it accordingly. – Mohebbikhah Feb 22 '22 at 12:19
1

Use LayoutBuilder, taken from this answer. Confirmed it to work, feels like a much intended solution for the problem

Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
0

Use MediaQuery to make it responsive on different screen sizes

    Container(
      color: Colors.red,
      width: MediaQuery.of(context).size.width,
      child: Text(
        'Always full width container',
      ),
    ),
Ante Bule
  • 1,834
  • 1
  • 2
  • 8
  • Does this MediaQuery change its value by changing the program size? – Mohebbikhah Feb 22 '22 at 10:36
  • @Mohebbikhah It adopts to every screen size – Ante Bule Feb 22 '22 at 12:04
  • I tested but I still have the same problem – Mohebbikhah Feb 22 '22 at 12:21
  • I am not able to test it now on desktop, but I don't see the reason why it behaves like you say it does. Are you sure that there isn't any width limitations in your parent widgets or something like that? I cannot say anything precisely without taking a look at your code, but I am not able to test it on desktop anyways so I hope you will find your solution. – Ante Bule Feb 22 '22 at 12:39
  • Can you connect to my desktop with the `Supremo` app? – Mohebbikhah Feb 23 '22 at 04:03
  • Sorry, I am not used to doing that and it would be better for community to post your source code so that we can try and find a bug there. I just run one of the example projects on my desktop and it works and behaves (resizes) fine, so it should be something in your code that is causing it to break. Maybe you can create new project and run it to see does the same bug still exists in there. – Ante Bule Feb 23 '22 at 07:35
  • Yes, I will create a new project and announce the result – Mohebbikhah Feb 23 '22 at 07:53