How to create a dynamic page in flutter - flutter

I'd like to list some components in a row and when a component reaches the end of the page it should just move to the next row. In this way I expect the page to be adjusted dynamically to the size of the screen. I don’t have a code example because it’s a theoretical question.

You can use the screen width/height to calculate the size of the row widgets.
To get the screen size do the following:
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;

final height = MediaQuery.of(context).size.height; final width = MediaQuery.of(context).size.width;
// or you can use screen util package it make your screen responsive
and make your code inside SizedBox
it will work
like this :
SizedBox( height: 100, width: width * 0.85 // this mean width is 85% of screen width// child: //your code ),

The obvious answer is Wrap. Give it some children, and it lays them out by default start to end horizontally, and when the next child doesn't fit, it starts a second line.
You don't even need to put it in a row, but you can certainly use it as part of a row or part of a column.

Related

can the top, bottom etc of Positioned widget in a stack be specified as percentage?

When using the Positioned widget in flutter, can I specify the top and bottom parameter as a percentage of the stack instead of exact pixels?
I have an Idea! I don't know, how do you like it.
You can't use percentages directly for exact pixels.
but you can use MediaQuery to use percentages.
final size = MediaQuery.of(context).size; //this var has 100% of your screen
now you can use a percentage of your screenSize in the top, bottom, right, and left properties.
like
top: size.height / 2, //it will be divide your screenHeight by 2 means 50%
I have 2 suggestions :
first :
in the Stack Widget, there is an alignment property, where you can choose a value from the Alignment enum to align the Positioned widget
you can also wrap Positioned widget with Align widget
if this doesn't fit in your case
in that alignment property you can use Alignment constructor to specify a y and x values to align your widget, so in example :
alignment: Alignment(0, 0)
will center your widget
alignment: Alignment(-1, -1)
will align your widget to the left top
alignment: Alignment(1, 1)
will align your widget to the right bottom
with this, you can use those between those values to get into a specific alignment
with multiplying by your calculated percentage to get into specific align based on %

How to make the application display correctly on various screens?

The application looks good on a Pixel 4 XL (resolution of 1440 x 3040) but is overflowing on a Samsung A5 (720 x 1280).
I expected the components to be scaled based on the resolution/ppi, but it seems that I misunderstood.
So how can I fix this ?
Any help is greatly appreciated.
Thanks.
One way of achieving a great looking UI on different screen sizes is to size your widgets relatively to the screen size, and not with pixel amounts, since when the screen's width or height is smaller or bigger, and the widgets are the same size, the UI will either go out of bounds or you will have empty spaces in your UI.
how to achieve that:
in your build function you can store the screen size in a variable:
Widget build(BuildContext context) {
final screenSize = MediaQuery.of(context).size;
...
then in your widgets that you are returning in the build function, for example a container, change the pixel values to values relative to the screen size:
instead of
return Container(width: 50, height: 70);
write stuff like:
return Container(width: screenSize.width * 0.1,
height: screenSize.height * 0.12);

Flutter: AnimatedContainer change height while sliding

I'm using Sliding Up Panel package to use sliding panel. Sliding panel located above Scaffold's bottomNavigationBar and it has AnimatedContainer. Sliding panel has an onPanelSlide function witch indicates it state from 0.0 (closed / unexpended) to 1.0 (open / expanded). In gif below i have red sliding panel than on expanded set's AnimatedContainers height to 0 and to initial height at closed state. How can i make it change it height dynamically so it would set it height to 0 at the same time as sliding panel would fully open?
You could simply use a Container with height parameter set every onPanelSlide call
const NAV_BAR_HEIGHT = 100.0;
double height = NAV_BAR_HEIGHT;
onPanelSlide: (double position) {
setState((){
height = NAV_BAR_HEIGHT - position * NAV_BAR_HEIGHT;
});
}
Container(
height:height,
child: MyNavBar(),
)

How to adapt crossAxisCount of GridView based on its width in Flutter?

What is the best practice to adapt the number of columns (crossAxisCount) of a GridView based on its width in Flutter?
Maybe I can better explain the intended behaviour by referencing HTML:
I create 6 boxes (e.g. DIVs) with a width of 200 px (and same height) each.
(a) If the element surrounding these boxes has a width of 400 px, it would automatically show 2 boxes per row in 3 rows.
(b) If the element surrounding these boxes has a width of 700 px, it would automatically show 3 boxes per row in 2 rows.
Use Wrap. It can put renderboxes as many as fit to a row, then wrap to the next row. (formerly a comment, but it looks like the answer!)
I do this like below. You can try..
int getItemCountPerRow(BuildContext context) {
double minTileWidth = 200; //in your case
double availableWidth = MediaQuery.of(context).size.width;
int i = availableWidth ~/ minTileWidth;
return i;
}

Flutter - How to proportionally center a view (centered with multiplier offset)

I'm wondering if in Flutter there are any good ways of imitating the iOS Xcode constraint where you center a view inside another (say, vertically), and supply a multiplier such that instead of being exactly centered (50% of the way down the parent view), it's positioned at 30% down, or 70% down, or whatever.
(Rather than use a fixed margin from the top of the screen, I'd like to "float" a header view down by 20% of the screen height...)
FractionallySizedBox is enough by itself to handle such layout
FractionallySizedBox(
heightFactor: .5,
widthFactor: 1.0,
alignment: Alignment.topCenter,
child: child,
)
This will top center a widget taking helf the height of its parent and full width
All my FractionallySizedBox efforts have been unreliable, but here's a way that's proven far stabler for me - using LayoutBuilder and SizedBox as a spacer:
LayoutBuilder(builder: (context, constraints) => Column(
children: <Widget>[
SizedBox(height: (constraints.maxHeight - constraints.minHeight) * 0.2,),
myWidget
],
))
This way constraints give me the ability to calculate 20% of the parent height, and apply that as a spacing using a simple SizedBox.
Set in container of parent view
Container(alignment: Alignment.center, ...)
I found one way, but I'm not sure it's the neatest yet:
For vertical proportional centering:
Embed your layout inside a Center widget that is itself inside a FractionallySizedBox. Provided that FractionallySizedBox is at the top of the screen, by changing its heightFactor you effectively change the centering position caused by the Center widget.
new FractionallySizedBox(
heightFactor: someHeightFactor,
child: Center(
child: myChildWidget
),
);
i.e. if parentHeight = the height of the parent widget to this FractionallySizedBox, and parentY = the (absolute) y origin of that parent widget, then setting heightFactor = 0.6 would center your UI child inside a region measuring 0.6 * parentHeight, therefore with an absolute y center = parentY + 0.3 * parentHeight.
Horizontal proportional centering would be the same but using widthFactor on the FractionallySizedBox.
Use FractionallySizedBox to size a widget relative to all available space, and wrap it with a Container or Align to specify the alignment of it.
For example:
Container(
alignment: Alignment(0, -0.5),
child: FractionallySizedBox(
heightFactor: 0.5,
widthFactor: 0.8,
child: Container(color: Colors.blue),
),
)
This makes a blue box that's 50% of screen height and 80% of screen width, positioned at 25% down vertically.
Note, for the Alignment class, it takes in 2 parameters, for x and y axis alignment, ranges from -1 to +1. For example, (0,0) is center, (-1, -1) is top left corner, so here (0, -0.5) centers it horizontally and lifts it up half way vertically, resulting in 25% padding from the top.