I have a Flutter app and now I want to make a web version of it when I am trying to run it the web app is occupying a full desktop screen which disturbs the design. This is the code that I am using
MediaQuery(
data: new MediaQueryData(),
child: MaterialApp(
home: ScreenUtilInit(
splitScreenMode: true,
designSize: const Size(390, 844),
minTextAdapt: true,
builder: (context, child) {
return MainHomePageN();
})))
I want the web view to be similar to the height and width of a mobile screen and the remaining screen to be black. So is there a way to achieve so by adding a constrain at a single place.
6
Answers
I guess this should do it:
In general, just wrapping your entire app in a
SizedBox
that is in aCenter
should work, for exampleBut I’m not familiar
ScreenUtilInit
, and it might be that the inner workings of it doesn’t work well with this solution.This will show the mobile design in web, by adjusting width:
Code:
Use it like this :
I suppose this is what you are trying to achieve, but without a design/picture/image is hard to know.
so, if you are running on web don’t use ScreenUtilInit (I haven’t used that package before), so you can set the width you want and fill the remaining space with a black color.
I think there are many ways to achieve it. But to adapt only the web code and change the minimal of original code lets try with LayoutBuilder.
We dont have clues about your original code here, but add a variable in constructor of your screens. They will be double? width and double? height. The trick will be: If we are on web, we will have a Layoutbuilder passing custom width and custom height.
So in initState of your screens, check:
With this we have setup the screen size. Now we Just need to insert the black feature in each side. So, in your main (or in a root point) add a LayoutBuilder. I will give you a sample. Lets say you will place it in your main.dart file:
The issue with your approach is that you’re setting that size only to MainHomePageN. The moment you push a new screen on the stack, it no longer has those sizes. (I’m not familiar with ScreenUtilInit so I might be wrong)
A very over-engineered solution would be to use auto_route like this:
Then in the routing file you’d have something like this:
Please check the documentation of the package, because I have not tested this code yet.
Another approach is to simply create a widget with the desired size which you put at the top of each scaffold. (This will take a little more refactoring)