what is differencebetween spacing and runSpacing in Flutter Wrap() Widget? - flutter

what is difference between spacing and run space named parameter Flutter Wrap() Widget and also run alignment property please explain that

spacing is the space between the children in the main axis and runSpacing is the space in the cross axis.
Consider this example (for default alignment)
SizedBox(
width: 300,
child: Wrap(
spacing: 20.0, // Horizontal space.
runSpacing: 30.0, // Vertical space.
children: <Widget>[
apple,
ball,
champion,
destructor,
eagle,
],
),
)

Widget Wrap spacing means the space between two widget in main axis
Widget Wrap runSpacing means the space between two widget in cross axis
Check this video. https://www.youtube.com/watch?v=z5iw2SeFx2M

spacing: 10.0 : gap between adjacent box (How much space to place between children in a run in the main axis.)
runSpacing: 20.0 : gap between lines (How much space to place between the runs themselves in the cross axis.)

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 create a dynamic page in 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.

How to reduce the vertical space between the widgets in gridview Flutter?

How to reduce the vertical space between the widgets in a gridview? What do the crossaxisspacing and the mainaxisspacing actually do here? I tried playing around with them but still I am unable to clearly grasp their use.
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
children: componentsList.map((value){
return value;
}).toList(),
);
}
If your GridView scrolls vertically, the main axis is the vertical axis. So the mainAxisSpacing would be the vertical spacing of the elements and the crossAxisSpacing would be the horizontal spacing of the elements.
And to avoid confusions, remember that all the items of the GridView will have the same height, if you want to change the size you need to set the aspectRatio.

how to fix the height of bars in flutter horizontal bar charts

In flutter charts, the horizontal chart automatically stretches according to the height of the screen. Is there any way through which I may fix the height of the bars so that it may not stretch to the entire screen?
Simple add your chart in the ConstrainedBox like this -
ConstrainedBox(
constraints: BoxConstraints.expand(height: 200.0), // give the height according to you
child : Your_Chart_here(), // your chart here
),
You can try to wrap BarChart in to SizedBox (to set size), wrapped with SingleChildScrollView (to scroll it).
In pseudocode
- SingleChildScrollView
- SizedBox
- charts.BarChart
Flutter
SingleChildScrollView(
child: SizedBox(
height: 2000,
child: HorizontalBarLabelChart(
getData(),
),
),
),
Size calculation
Height of the SizedBox can be calculated base on number of data, e.g.:
numerOfElements * 50
Before
For 100 elements
After
For 100 elements (so height is 5000 because 100 * 50)

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.