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(),
)
Related
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 %
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.
I am trying to create a coursel with the active dots indicator.
like the one on Instagram I'd like to create a listview when the number of dots grows.
When they are too many you probably wont have an issue of layout but if they are like 2 then I'd like to keep them centered.
I'd also like to know how to shrink the ones on the edges to indicate continuity if possible I can't quite figure out how you can achieve that.
I tried to get the active and center the active dot with something like this
_scrollTo(int index) {
// get the screen width. This is used to check if we have an element off screen
RenderBox tabsContainer =
_tabsContainerKey.currentContext.findRenderObject();
double screenWidth = tabsContainer.size.width;
// get the button we want to scroll to
RenderBox renderBox = _tabKeys[index].currentContext.findRenderObject();
// get its size
double size = renderBox.size.width;
// and position
double position = renderBox.localToGlobal(Offset.zero).dx;
print(position);
// this is how much the button is away from the center of the screen and how much we must scroll to get it into place
double offset = (position + size / 2) - screenWidth / 2;
// if the button is to the left of the middle
if (offset < 0) {
// get the first button
renderBox = _tabKeys[0].currentContext.findRenderObject();
// get the position of the first button of the TabBar
position = renderBox.localToGlobal(Offset(-20.0, 0.0)).dx;
// if the offset pulls the first button away from the left side, we limit that movement so the first button is stuck to the left side
if (position > offset) offset = position;
} else {
// if the button is to the right of the middle
// get the last button
renderBox = _tabKeys.last.currentContext.findRenderObject();
// get its position
position = renderBox.localToGlobal(Offset.zero).dx;
// and size
size = renderBox.size.width;
// if the last button doesn't reach the right side, use it's right side as the limit of the screen for the TabBar
if (position + size < screenWidth) screenWidth = position + size;
// if the offset pulls the last button away from the right side limit, we reduce that movement so the last button is stuck to the right side limit
if (position + size - offset < screenWidth) {
offset = position + size - screenWidth;
}
}
// scroll the calculated amount
_scrollController.animateTo(offset + _scrollController.offset,
duration: new Duration(milliseconds: widget.duration),
curve: Curves.easeInOut);
}
If you want to center the children on ListView widget. You can do this
Center(
child: ListView(
shrinkWrap: true, //make children centered if children is below minimum height or width of its parent
children: [],
),
)
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.
I have created a scrollable area and i am trying to add a scrollbar underneath the images (the blue scrollbar in the linked image)
Code that i currently have for the scrollable area.
scroll = new ScrollComponent
opacity: 1.00
shadowBlur: 0
scroll.size = screen
Info.parent = scroll.content
scroll.scrollVertical = false
Scroll
I think you're trying to make a scrollbar that moves with the scrolling and shows how far along the user has scrolled, right?
Here's some code how to do that:
scrollbar.parent = scroll
# First make the width of the scrollbar relative to the size of the content.
scrollbar.width = (scroll.width / scroll.content.width) * scroll.width
# When the scroll is moved
scroll.onMove ->
# Calculate the width that we should scroll over
width = scroll.content.width - scroll.width
# Calculate the percentage that has currently been scrolled
percentage = scroll.scrollX / width
# Calculate how much space there for the scrollbar to move in
freeSpace = scroll.width - scrollbar.width
# Set the position of the scrollbar relative to the free space and the percentage scrolled
scrollbar.x = freeSpace * percentage
A full example is here: https://framer.cloud/QtcLD