Flutter: How can I limit children width of SliverChildListDelegate? - flutter

I have a Flutter project in which SliverList is used inside a CustomScrollView. The SliverList has its delegate property set to SliverChildListDelegate. The SliverChildListDelegate is populated with widgets that have their width set to 200. On building the app the widgets take the width of the whole app regardless. If these same widgets are put in a Column under SingleChildScrollView the width is respected and set to 200. How can I limit the with of the children widgets inside SliverChildListDelegate?

This looks more like a trick than an official solution. I just wrap it with Row and put that Widget in between 2 Spacers. E.g:
SliverList(
delegate: SliverChildListDelegate([
Row(
children: [
Spacer(),
Container(
height: 6,
width: 200,
color: Colors.red,
),
Spacer(),
],
),
]),
)

Related

Containers with dynamic sizes inside a listview widget

I'm creating a listview widget and i want it to have varying container sizes that change from user inputs inside it, the problem is when i wrap my columns or rows in expanded widgets, the code breaks.
is there a way to set a min and max height for a list view widget?
ListView(
children: [
Column(
children: [
Row(
children: [
Expanded(
child: Container(
color: Colors.yellow,
),
),
Container(
width: 2,
color: kKillTeamOrange,
),
Expanded(
child: Container(
color: Colors.green,
),
),
],
),
],
),
],
),
I tried wrapping the listview in a constrained container but no luck, the other option would be to have the buttons set the height state but there are a lot of buttons to code that. and using expanded widgets is great for varying screen sizes.
any help would be greatly appreciated.
cheers
Two ways came to mind:
CustomScrollView
ListView (But using cards)
Both ways are responsive to screen sizes
LISTVIEW WITH CARD:
CUSTOM SCROLLVIEW:
RESULT:
hope this helps...

Flutter: Vertical Overflow Problems When Nesting Multiple Gridviews Inside Of Column

I'm having trouble with managing vertical overflow in my Flutter application.
Essentially, I have a main column with a container child that holds another column with 3 containers each containing a gridview:
stateful widget
Scaffold
body: Center
Column
Container
Column
Container
Gridview
Container
Gridview
Container
Gridview
What I've Tried:
Method 1:
to try and solve the vertical overflow, I have tried wrapping the containers in expanded, but the problem with that is it smushes everything together and I want all the content of the gridviews to be displayed at full height, with the ability to scroll down through the main column.
Method 2:
so next i tried wrapping the main column in a singlechildscrollview and removing the expanded wrappers from the containers, but that is giving me the "vertical viewport was given unbounded height" error.
What I Want:
ultimately, i want the containers to size automatically, to fit the content, and to be able to scroll down to view all of the content ion the main column.
i'm aware of the sizedbox solution, but again, i want the containers to resize automatically to fit the content inside.
what are my best options for achieving this?
UPDATE
these two official Flutter video explains this situation and the solution very well
https://youtu.be/ORiTTaVY6mM
https://youtu.be/LUqDNnv_dh0
it looks like what i want is to use sliverlists and slivergrids inside of a customscrollview instead of gridviews and listviews inside of a column
customscrollview
slivergrid
slivergrid
slivergrid
NOT
column
gridview
gridview
gridview
Try below code hope its help to you. try to wrap your GridView inside Expanded Widget
Center(
child: Column(
children: [
Expanded(
child: Container(
constraints: BoxConstraints(
maxHeight: double.infinity,
),
child: GridView(
shrinkWrap: true,
),
),
),
Expanded(
child: Container(
constraints: BoxConstraints(
maxHeight: double.infinity,
),
child: GridView(
shrinkWrap: true,
),
),
),
Expanded(
child: Container(
constraints: BoxConstraints(
maxHeight: double.infinity,
),
child: GridView(
shrinkWrap: true,
),
),
),
],
),
),
Or try flutter_staggered_grid_view Package hope its help to you.

How to make equal width or height constraints between widget in flutter?

In iOS's native AutoLayout constraints, It very easy to make a equal width constraints between ViewA and ViewB . And these two views will get a same width always.
But in flutter ,it seems can't be achived this kindof constraints so easily?
Do I have to make a SizedBox with specific width for both WidgetA and WidgetB explicitly?
You can always put your widgets in a Row(), and wrap every widget in Expanded(). This way they will always have 50% width of the parent Row(). And if you need to constrain the width, just wrap the Row() in a SizedBox().
SizedBox(
width: 500.0,
child: Row(
children: [
Expanded(
child: Container(),
),
Expanded(
child: Container(),
),
],
),
),

In Flutter, how can I have an adjusting vertical layout, a mix between column and listview behavior?

As far as I see, Column and ListView both have a very distinct usage when used for a base root layouting.
Column is used when the screen has few components (such as login screen). We can add some Expanded components to adjust white spaces in between, so when the keyboard is visible, the screen shrink to keep everything visible.
ListView is used when the screen has many components that potentially need scrolling. We can't use Expanded component in ListView. When using ListView, appearing keyboard does not change the white spaces, only change the size of outer ListView, while the inner content is wrapped in scroll view.
Now the problem is, how if I want to have screen like this:
When all the contents' combined vertical size is not longer than available height quota given from parent (in this case, screen's height), then the components behave like inside Column: expanding or shrinking to fill available white spaces according to rules set by Expanded.
When all the content's combined vertical size is longer than available height quota, then the components behave like inside ListView: all the possible expanding components will shrink into their minimum size (ignoring Expanded), and the screen is scrollable so user can see the rest of the screen below.
Is this possible to be done in Flutter? How?
EDIT: based on Reign's comment, I have isolated some code from SingleChildScrollView manual, but it looks like it still can't handle if its children contains Expanded.
Widget columnRoot({
MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween,
AssetImage backgroundImage,
List<Widget> children
}) =>
LayoutBuilder(builder: (BuildContext context, BoxConstraints viewportConstraints) =>
SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewportConstraints.maxHeight,
),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: backgroundImage,
fit: BoxFit.cover),
color: Colors.white
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: mainAxisAlignment,
children: children
),
)
)
)
);
Widget content(BuildContext context) => columnRoot(children: [
Container(color: Colors.red, height: 100.0),
Expanded(Container(color: Colors.green)), // without this line, there's no layout error
Container(color: Colors.blue, height: 100.0),
]);
Error:
RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I added some code you can test with also with some explanation.
Copy paste and run the code
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: SingleChildScrollView( //Since setting it to scrollable, your widget Column with expanded children wont work as it supposed to be because it wont know its parent height
//Since its already scrollable `Expanded` will expand or shrink now based on it child widget (Expanded(child: SomeHeight widget)) refer: #10 example
child: IntrinsicHeight( //This will fix the expanded widget error
child: Container(
//Test remove this height
// height: 400, //But when you set its height before its parent scroll widget, `Expanded` will expand based on its available space
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.max,
children: [
Container(color: Colors.red, height: 100.0),
//#10
//Experiment with this
Expanded(
child: Container(
color: Colors.purple,
// height: 100.0, //initialized height, remove parent container height: 400
// child: Text("This is also considered as min height"),
),
),
Container(color: Colors.blue, height: 100.0),
],
),
),
),
),
),
);
}
}

Flutter: How to adjust the height of a container as the maximum height of its sibling in a row

I have two widgets in a row. The first widget is a black line with width 5.0, the second widget is a text widget whose size may vary according to the content. I want the first container to have the same height as of the second widget
One way is to make both widget child of an IntrinsicHeight widget and then declare the height of the first container as double.infinity. This should solve your problem. Example code is given below:
IntrinsicHeight(
child: Row(
children: <Widget>[
Container( //this is the first container
height: double.infinity
),
secondWidget(
)
],
)
Let me know if you have further query. Happy coding!
You can use Expanded widget. It divides siblings to the same height or width.
here is the code:
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.red,
),
),
Expanded(
child: Container(
color: Colors.green,
),
)
And here is the result:
That is so easy. I wish it could help.