Percentage based layouts in Flutter - flutter

Coming from an Android background I'm used to using ConstraintLayout to define percentage based margins and sizes for my UI. I was wondering if there is a widget in Flutter that can achieve something similar. I've been looking at the widget documentation but I haven't found anything yet.

In flutter, percentage sizes are represented using FractionalOffset which is basically 0 < {x,y} < 1 coordinates
There are a few widgets available using this logic, like:
FractionallySizedBox
Align
These, combined with a stack should allow about any layout you need without using LayoutBuilder or similar.

Related

Using a LayoutBuilder with Slivers

I use a layoutbuilder in flutter to create a responsive design. However, lately I've gotten into slivers and the layoutbuilder does not work with them by default. I've found a sliverlayoutbuilder class, but it does not appear to have the width I'm looking for.
What's the best way to get width when using slivers?

Flutter different screen size

I'm working on a flutter project but having problem to make the widgets and texts to fit different phone screens. Tried looking for answers on youtube but I could not get any. Your help is appreciated. Thanks.rt
Here are some resources for your need.
For responsive layout,
https://medium.com/flutter-community/tagged/responsive-design
https://medium.com/#TidingsSP/flutter-responsive-widget-development-using-a-modular-approach-bf8d63838c76
https://pub.dev/packages/responsive_framework (responsive layout plugin)
https://pub.dev/packages/sizer (responsive layout plugin)
For responsive font,
https://pub.dev/packages/auto_size_text ( best option if you wanna resize font automatically based on parent widget's size )
For responsive widget,
Consider using Flexible, Expanded, MediaQuery.of(context).size.width, MediaQuery.of(context).size.height for widgets' dimensions instead of fixed sizes.
Use FittedBox, LayoutBuilder.
Have fun!!

Is there a way to calculate a dynamically sized widget?

I'd like to add a custom showMore widget if the skills widget which has a dynamic size(width) exceeds the screen width. On clicking the showMore widget it should show all the skills in a wrap .else show less.
In flutter
Constraints go down and sizes go up
See this documentation.
Flutter uses a single pass algorithm to render your application. This is a technical choice to ensure performance but it comes with limitations.
One of them is that, when you are building the widget tree, you only have access to the constraints of the parent, and not any size of any widget (since they are not rendered yet).
So a short answer to your question is:
No, you cannot do what you are trying to do (displaying something if some widgets are not fitting on the screen) since you don't have access to any sizes in the build method.
An alternative solution would be to use Wrap to wrap your chips or use a ListView on the horizontal axis to make the list of chips horizontally scrollable.
Anyway, if you really want to do this, you can hardcode the sizes of your chip and access the device size with MediaQuery.of(context).size or by using the LayoutBuilder and using contraints.maxWidth as the parent's width. Then you can check whether or not numberOfChips * chipSize <= maxWidth. But I wouldn't recommend it as the design wouldn't be responsive:
All the chips will have the same size, so you'll end up with a big chip for "c" and maybe a long name like "python" won't fit in and you'll end up with overflow issues.
What if the user changes the font size of his device? You will also end up with overflow issues.

which is the best technique for responsive flutter

creating responsive widget kinda isMobile?Fontsize 12 : fontsize 24 or use sizer package,what is the best responsive method for web,tablet,mobile?
flutter documentation did give some solution but , not effective for all purposes
AspectRatio
CustomSingleChildLayout
CustomMultiChildLayout
FittedBox
FractionallySizedBox
LayoutBuilder
MediaQuery
MediaQueryData
OrientationBuilder
if we using one by one , all methods have it's own defects .
mobile side and tablet side it will work , but in the case of windows and web , it won't.
if we combine these methods , also get some responsive issue , because of all methods are depend on the device height ,device width ,device aspectRatio , device Offset ,
also so much plugins are available but they have defects too , in the case of flutter_screenutil it depend on width , height . so if changes in one of the property it won't count. that means it won't change layout in web
so i am using this way combine 4 methods together
flutter_screenutil , LayoutBuilder , MediaQuery , AspectRatio
by using this way we can achieve,maintain the 3 layouts in one code
please use wisely according to your use case
the best place to find FLUTTER solutions/documentations is the official flutter/dart websites.
For this question:
Creating responsive and adaptive apps

Flutter padding, margin and alginment best practices

What is the best practice in flutter for padding,margin etc.
example:
I made a simple drawer in flutter. To make everything to position in places I want. I used padding extensively.
Now the issue is when I test it on different phone of different size. the positions are inconsistent across different devices.
From the Flutter Docs:
Design discussion Why use a Padding widget rather than a Container
with a Container.padding property? There isn't really any difference
between the two. If you supply a Container.padding argument, Container
simply builds a Padding widget for you.
Container doesn't implement its properties directly. Instead,
Container combines a number of simpler widgets together into a
convenient package. For example, the Container.padding property causes
the container to build a Padding widget and the Container.decoration
property causes the container to build a DecoratedBox widget. If you
find Container convenient, feel free to use it. If not, feel free to
build these simpler widgets in whatever combination meets your needs.
In fact, the majority of widgets in Flutter are simply combinations of
other simpler widgets. Composition, rather than inheritance, is the
primary mechanism for building up widgets.
So, no worries about padding widgets.
A good practice to get some spaces inside a Column() or Row() is using SizedBox(). Then you add an extra space setting up width or height.