Flutter: Using Infinite Scroll ListView Horizontally - flutter

I am using the flutter package infiniteListView in order to get a horizontal list of infinitely scrolling list of days that users can click on.
This is the following error I am getting
The following assertion was thrown during performLayout():
RenderFlex children have non-zero flex but incoming height constraints are unbounded.
When a column is in a parent that does not provide a finite height constraint, for example
if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting aflex
on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
space in the vertical direction.
Here is my code
Widget build(BuildContext context) {
return Container(
// Leave margin here for top bar
color: Colors.grey[900],
padding: EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 5.0),
child: (Column(children: [
Expanded(
child: InfiniteListView.builder(
scrollDirection: Axis.horizontal,
controller: _infiniteController,
anchor: 0.5,
itemBuilder: (BuildContext context, int index) {
return Material(
child: InkWell(
onTap: () {},
child: ListTile(
title: Text('Item #$index'),
subtitle: Text('Subtitle $index'),
trailing: const Icon(Icons.chevron_right),
),
),
);
}),
),
])));
}

Solution 1:
Column has Unbounded Constriants (Infinite Height). Wrap your
InfiniteLiewView into a SizedBox(height 300: child:
//InfiniteListView);
Solution 2:
If that does not work, Pass shrinkWrap: true to InfiniteLiewView,
Solution 3:
Wrap you Column into a SizedBox(height 400, child: //code) .
Try this out, Let me know then.

wrap your ListTile with Fixed width, ListTile by default takes full width(something like double.infinity). also our Axis.horizontal, will take double.infinity, and this is where errors come.
Widget build(BuildContext context) {
return Scaffold(
body: Container(
// Leave margin here for top bar
color: Colors.grey[900],
padding: EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 5.0),
child: (Column(mainAxisSize: MainAxisSize.min, children: [
Expanded(
child: InfiniteListView.builder(
// itemCount: 222,
scrollDirection: Axis.horizontal,
controller: _infiniteController,
anchor: 0.5,
itemBuilder: (BuildContext context, int index) {
return Material(
child: InkWell(
onTap: () {},
child: SizedBox(
width: 100,
child: ListTile(
title: Text('Item #$index'),
subtitle: Text('Subtitle $index'),
trailing: const Icon(Icons.chevron_right),
),
),
),
);
},
),
),
])),
),
);
}

Related

how to give expanded height to listview while it is used inside single child scrollview in flutter?

Here is a demo where I have taken some container, textfield and listview builder,
I have wrapped all in SingleChildScrollView to avoid pixel error while opening keypad,
Here I want to give possible height to listview ,depend on item count and spacer between listview and save button
Here is my code:
List<String> paybylist=[
'A','B','C','D','E','F','G',
'H','I','J','K','L','M','N',
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(children: [
Container(
height: 100,
color: Colors.red,
child: Center(child: Text('Container1')),
),
Container(
height: 50,
//color: Colors.green,
child: Center(
child: CustomTextField(),
),
),
Text('Select Payment Mode'),
Container(
height: 400,//height should be based on available space
color: Colors.grey,
child:ListView.builder(
itemCount: paybylist.length,
itemBuilder: (context,index){
return ListTile(
title: Text(paybylist[index]),
);
}) ,
),
//want to add available space between listview and save button
Padding(
padding: const EdgeInsets.all(8.0),
child: MaterialButton(
color: Colors.blue,
onPressed: (){},child: Center(child: Text('Save')),),
)
],),
),
);
}
}
You can add shrinkWrap:true in your ListView.builder() and add physics: NeverScrollableScrollPhysics() to it otherwise your list won't scroll.
Here's an example
#override
Widget build(BuildContext context) {
List<String> paybylist = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
];
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
Container(
height: 100,
color: Colors.red,
child: Center(child: Text('Container1')),
),
Container(
height: 50,
//color: Colors.green,
child: Center(
child: TextField(),
),
),
Text('Select Payment Mode'),
Container(
// height: 400,
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: paybylist.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(paybylist[index]),
);
}),
),
//want to add available space between listview and save button
Padding(
padding: const EdgeInsets.all(8.0),
child: MaterialButton(
color: Colors.blue,
onPressed: () {},
child: Center(child: Text('Save')),
),
)
],
),
),
);
}
Also you can add your save button either in Stack or BottomNavigationBar, that way the button will be always visible.
You cannot add Expanded inside the Scrollable Widget in flutter,
To fill the available gap, place the save button in bottomNavigationBar
So now the ListView doesn't need fixed height of 400 and it can span to have the full height. In fact it will span only the height of children
Make sure you add shrinkWrap :true and physics: NeverScrollableScrollPhysics() inside the ListView.builder()
Your present structure
Scaffold
|_body:
|_SingleChildScrollView
|_Column
|_Container
|_Container
|_Continaer (400 height)
|_ListView
|_Container (Save Button)
Change it to :
Scaffold
|_body:
|_SingleChildScrollView
|_Column
|_Container
|_Container
|_ListView
|shrinkWrap:true
|physics: NeverScrollableScrollPhysics()
|_bottomNavigationBar
|_Container (Save Button)

ListView inside Alert Dialog displays empty transparent window

Below is my Alertdialog:
return AlertDialog(
title: const Text('Choose Device'),
content: ListView.builder(
shrinkWrap: true,
itemCount: listDevices.length,
itemBuilder: (_, int index) => deviceList(listDevices[index]),
),
);
Below is deviceList function:
Widget deviceList(String strDevice) => SizedBox(
height: 150.0,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
children: <Widget>[
const Icon(Icons.toys),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(strDevice),
),
],
),
const SizedBox(
height: 20.0,
),
],
),
);
Its not displaying Listview and am getting error as:
The following assertion was thrown during paint():
RenderBox was not laid out: RenderPhysicalShape#aedc0 relayoutBoundary=up2
'package:flutter/src/rendering/box.dart':
Failed assertion: line 2001 pos 12: 'hasSize'
What might be the issue? Thanks.
You can use like this:
List<String> listDevices = <String>['iPhone 11', 'Samsung M22f', 'MacBook Pro', 'Xiaomi note 12 pro'];
I opened AlertDialog box when tap on ElevatedButton like this:
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Alert Dialog Box"),
content: SizedBox(
// height: 250,
width: 150,
child: ListView.builder(
shrinkWrap: true,
itemCount: listDevices.length,
itemBuilder: (_, int index) => deviceList(listDevices[index]),
),
),
),
);
//onTap function define here
},
child: const Text('Send Message'),
),
I don't changes your deviceList widget
And i clearly show AlertDialogBox with ListView. I hope it's help you.
Happy Coding :}
Try to wrap ListView.builder in a SizedBox with a specific height and width.
AlertDialog(
title: const Text('Choose Device'),
content: SizedBox(
height: 300,
width: 300,
child: ListView.builder(
shrinkWrap: true,
itemCount: listDevices.length,
itemBuilder: (_, int index) =>
deviceList(listDevices[index]),
),
),
),

how to create horizontal listview builder in flutter?

hi I have Row in flutter and I want add some widget on row with listview.builder, listview sort all item vertically. but I want show them horizontally. in the image below you can see my code and the result.so how can i change the listview.builder to horizontal?
You need set height for your ListView if you want to use it horizontally, and set scrollDirection to Axis.horizontal:
Row(
children: [
IconButton(onPressed: () {}, icon: Icon(Icons.chevron_left)),
Expanded(
child: SizedBox(
height: 10,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 12),
height: 10,
width: 10,
color: Colors.red,
);
},
itemCount: 10,
),
)),
IconButton(onPressed: () {}, icon: Icon(Icons.chevron_right)),
],
),
There is a property in listview scrollDirection you need to set for horizontal scrolling
scrollDirection: Axis.horizontal,
Widget build(BuildContext context) {
Widget horizontalList = new Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 200.0,
child: new ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Container(width: 160.0, color: Colors.red,),
Container(width: 160.0, color: Colors.orange,),
Container(width: 160.0, color: Colors.pink,),
Container(width: 160.0, color: Colors.yellow,),
],
)
);
);
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: horizontalList,
),
);

how to only scroll the children inside scrollview that has another scrollview

I have ListView and the children that has pdf document wrap inside singleChildScrollView want to only scroll the singleChildScrollView and not the listView
after expansion
Below is my code
Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 15, top: 10),
width: size.width,
child: Text("Title should be Pinned",
textAlign: TextAlign.end,
),
),
SizedBox(
height: 20,
),
Theme(
data: ThemeData(
accentColor: Colors.black,
primaryColor: Colors.white24,
),
child: Expanded(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (context, i) => ExpansionTile(
title: Center(
child: Text(
"After Expansion this should be pinned and only children should be scrollable"
),
),
children: [
Container(
child: SingleChildScrollView( //should be scrollable on expanded and not the title of ExpansionTile
child: SfPdfViewer.network(
'$url',
),
),
),
],
),
),
),
)
],
),
warp your widget in a container and give it a height
use the following parameters inside the scroll widget
shrinkwrap: false,
primary: true
physics: NeverScrollableScrollPhysics():
Add This Line to Your Widget then it will work

How to include a RaisedButton in a Container

I want a Raisedbutton that appears after the Container.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Galeria clase 1'),
),
body: Container(padding: EdgeInsets.all(8), child: galeria(array)),
floatingActionButton: FloatingActionButton(
onPressed: _optionsDialogBox,
tooltip: 'Pick Image',
child: Icon(Icons.add_outlined),
),
);
}enter code here
I tried this but there is the next error -> RenderBox was not laid out: RenderFlex#02bd4 relayoutBoundary=up1
body: Column(children: <Widget>[
(Container(
padding: EdgeInsets.all(8),
child: galeria(
array,
),
)),
RaisedButton(
child: Text("Guardar red"),
onPressed: () {
safeNeuralNetwork();
},
),
]),
galeria Function
Widget galeria(List<Uint8List> array) {
return StaggeredGridView.countBuilder(
crossAxisCount: 2,
itemCount: array.length,
itemBuilder: (BuildContext context, int index) {
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.deepPurple,
child: Column(
children: <Widget>[Image.memory(array[index])],
),
),
);
},
// staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
);
}
StaggeredGridView (or nearly any ScrollView) widget can't work with unbounded (infinite) height like in your case. You can't have a Column widget and a ScrollView inside it without using Expanded or something like that. Why? Because you need something to limit the maximum height of that widget. In this case we will use an Expanded widget. So go and modify your code like this:
body: Column(
children: <Widget>[
Expanded(
child: Padding(
child: galeria(array),
padding: EdgeInsets.all(8.0),
),
),
RaisedButton(
onPressed: () {
safeNeuralNetwork();
},
child: Text("Guardar red"),
),
],
),
With this we basically said: "Hey, go and render this column but please, stick a RaisedButton at the bottom and use the remaining space for the StaggeredGridView.".