Flutter keep certain elements fixed during scrolling? - flutter

How to keep some elements fixed (and above scrollable parts) during scroll?
Does it need to be inside a SingleChildScrollView?
Made a visual example of my problem:
I need the blue part to be scrollable, and that it goes behind the green part that needs to stay fixed.
This is a simplified part of my code:
body: SingleChildScrollView(
child: ListView(
children: [
Obx(()=>Container(THIS CONTAINER SHOULD STAY FIXED DURING SCROLL)),
Obx(()=>Column(THIS PART SHOULD BE SCROLLABLE)),
Obx(()=>Text(THIS PART SHOULD BE SCROLLABLE)),
Obx(()=>Row(THIS PART SHOULD BE SCROLLABLE))
],
)
),

If you put your ListView widget and FixedContainer widget in a Column, it will work.
For example:
Column(
children: [
Obx(()=>Container(THIS CONTAINER SHOULD STAY FIXED DURING SCROLL)),
Expanded(
child: ListView(
Obx(()=>Column(THIS PART SHOULD BE SCROLLABLE)),
Obx(()=>Text(THIS PART SHOULD BE SCROLLABLE)),
Obx(()=>Row(THIS PART SHOULD BE SCROLLABLE)),
),
),
],
)

Related

Flutter move row within column to the bottom of the page

I am trying to create a footer at the bottom of the page using SizedBox however I am having issues positioning it correctly.
I am using the following layout:
body: SingleChildScrollView(
child:Column(
children: [
Center(),
Center(),
Spacer(),
SizedBox()
]
)
)
When I try to run it I get a renderflex error, after doing some searching around I saw people mention that i should wrap my column in Expanded or SizedBox however both of those do not work as they also give renderflex error's on their own. I also tried using align combined with mainAxisAlignment but that doesn't move anything.
Thanks in advance!
If you're using scaffold, there is an option called bottomSheet , it exactly what you're looking for.
maybe something like this?
body:Stack(
children: [
SingleChildScrollView(
child:Column(
children: [
Center(),
Center(),
Spacer(),
]
),
)`
Positioned (
bottom:0,
child:SizedBox(),
)
]
),

Restricting the size of widgets programmatically

I'd like to create the layout shown in the picture below. This shows up fine:
Column( Row, ListView()))
While this renders errors:
Column( Row, Row( ListView(), Column ))
As soon as I replace the inner ListView() with Row( ListView(), Column ), nothing shows up and I get varying error messages, based on various changes I did.
Most often, I see a 'viewport has unlimited horizontal size'.
I guess Listview to be the source of the problem. That said, I'm no aware how to fix it.
Furthermore, I did find various hints on SO, but none fixed the problem.
How do I fix the problem?
Update 1
While the right Column() may be fixed width, the ListView() should take all remaining space of the screen [= availableWidth - WidhtOf(Column())].
put the ListView inside a Container And give it height/width
code
Column(
children: [
Row(
children: [],
),
Row(
children: [
Container(
height: 100,
width: 100,
child: ListView(
children: [
Text(
"1 C/G")
],
)),
Column(
children: [],
)
],
)
],
),
Both Row and Column have a mainAxisSizeparameter where you can set it to MainAxisSize.min so they will take their children's size.
Now, about the ListView, there is a shrinkWrap parameter that makes it so its max size will be its children's size. Another idea might be using a Flexible widget around the ListView, it does almost the same as Expanded, but the difference is: Expanded forces the child to occupy the remaining space, while Flexible allows the children to have any given size smaller than that, and that's because of its fit parameter that by default is FlexFit.loose.

Making a container scrollable

I have this view in my app
and this is the code for it:
return Container(
color: Colors.black,
child: ListView(
children: <Widget>[
Column(
children: <Widget>[
...
],
)
],
),
);
But if I click on my form I get stuck and the list of items is hidden by the keyboard, so I'd like to make my Container scrollable.
How can I do?
Instead of wrapping your Column with a ListView try with a SingleChildScrollView instead. You can also try adding a bottom padding equal to keyboard size MediaQuery.of(context).viewInsets.bottom.

Flutter: how to center a child, but set a limit on how big it can grow on one side of the margin

I have a home/login screen which is made up of a column that fills the entire screen like so:
Column(
children: <Widget>[
Expanded(
child: Container(
child: Logo(),
),
),
showThis ? This() : That(),
],
),
The second child of the column is dynamic and can have different heights, and this screen will have inputs so the keyboard will also affect the height.
I want to center Logo() vertically within the container when it's small (e.g. when keyboard is active), but limit how much the 'top margin' is able to grow, so that when the keyboard is hidden and This()/That() is small enough, Logo() will be in a static position on the screen, say 150 from the top (no longer centred vertically).
One method I have tried was using 2 empty Expanded() above and below Logo() and wrapping the top part in a ConstraintedBox(), but I am not able to get it to behave correctly.
Have you tried with Center() and also if you wanna in a specific position use Stack, for example
Stack(
children: <Widget>[
showThis(),
Positioned(top: 150, child: Logo())
],
)
This is what ended up working for me:
Column(
children: <Widget>[
Expanded(
child: Column(
children: <Widget>[
// constrain top spacing with a max height
Flexible(child: Container(height: 160)),
_logo(),
Spacer(),
],
),
),
_bottomWidget(),
],
),

How do I place a widget in the center of the screen while ignoring column?

There is a widget (for example text). In the real example it comes after another widget in Column. How to put it in the center of the screen? As I understand it, the Column does not. How can this be done? Or are there other ways to specify the location of items?
Column takes all the available space of the parent and by default aligns the items to start
You can change fix this changes the MainAxisAlignment to center.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("test"),
],
),
or changing parents height and move it to the center (be careful if you add many items it will cause overflow)
child: Center(
child: Container(
height: 25,
child: Column(
children: <Widget>[
Text("test"),
],
),
),
),
Using Stack widget you can do it:
var textEmpty = Text("Some Text");
Stack(children: <Widget>[
Column(children: <Widget>[
Container(
,margin: EdgeInsets.only(top: 100.0)
,child: Center(child: textEmpty, ),)],),
listView,
]);
Use Center Wigdet instead of Container. Hope this helps!
No worries where it comes from. It depends on the view where you have added it.
If you want to show in the center then just use Center widget
If you want to use Container widget then, you have to add the width and height both to double.infinite
If you want to use it in the same widget then you can use the Stack widget as well.
It totally depends on the requirement of the view and in which way you are tried to implement it.