ListView.builder with scrollDirection horizontal got error - flutter

I have this code:
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => addProductClass()),
);
},
child: const Icon(
Icons.add,
color: Colors.black,
),
),
body: Column(children: [
FutureBuilder(
future: getDocId(),
builder: (context, snapshot) {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: dataIDs.length,
itemBuilder: (BuildContext context, int index) {
return GetProduct(
documentId: dataIDs[index],
);
},
);
}),
Text("text")
]));
}
And i want to use scrollDirection: Axis.horizontal for listview. When I insert this value, I got the error:
Horizontal viewport was given unbounded height
Viewports expand in the cross axis to fill their container and '
'constrain their children to match their extent in the cross axis. '
'In this case, a horizontal viewport was given an unlimited amount of '
'vertical space in which to expand
How can I resolve this?

Easiest way is to provide a fixed height, either using a SizedBox or a ConstrainedBox with a maxHeight set.

Try to set shrinkwrap to true for listview builder
here is an example of listview builder in horizontal mode :
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.horizontal,
itemCount: 10,
itemBuilder: (BuildContext context, int index){
return Container( margin: EdgeInsets.all(5), height:10, width:20, color:black);
}
),
),
hope it will help !

Related

Flutter : Showing long widget list using ListView.builder() and ListTile

I have a Widget list with millions of widgets and some of them are wider than screen.
I need to use ListView.builder() and ListTile to save time while running like this:
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
controller: scrollController,
itemCount: widgetList.length,
itemBuilder: (context, i) =>
ListTile(
title: widgetList[i]
)
);
But when I run it, those ListTile that their title is wider than screen will overflow on the right because ListView.builder() not wide enough.
If I assign a big width to ListView.builder() like this will works fine but will remain a lot of blank even if all widgets in list are short:
ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: [
Container(
width: 2000,
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
controller: scrollController,
itemCount: widgetList.length,
itemBuilder: (context, i) =>
ListTile(
title: widgetList[i]
)
))]);
Any idea to improve this function?
---------------update-----------
My widgetList contains Column/Row etc.
But let's start with only one Text().
Example:
ListView.builder(
restorationId: 'sampleItemListView',
itemCount: 1000000,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz",
maxLines: 1,
),
);
},
)
The text is too long but I don't want it change line .
What can I do to read full text?
Use like below, do not specify the hight and width for the widgets
#override
Widget build(BuildContext context) {
var items = List<SampleItem>.generate(
1000,
(i) => const SampleItem(
'范植勝',
'Showing long widget list using ListView.builder() and ListTile',
Icons.favorite,
));
return Scaffold(
body: ListView.builder(
restorationId: 'sampleItemListView',
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
final item = items[index];
return ListTile(
title: Text(item.name),
subtitle: Text(item.decsription),
leading: Icon(item.icons),
onTap: () {
Navigator.restorablePushNamed(
context,
SampleItemDetailsView.routeName,
);
});
},
),
);
}

RenderFlex children have non-zero flex but incoming height constraints are unbounded: Nested ListView

I am trying to build a Nested listview but getting "RenderFlex children have non-zero flex but incoming height constraints are unbounded" error with below code.
Layers are like this...
Each item of a horizontal ListView has a Text widget and a ListView widget.
At the second level, each item of vertical ListView contains again a Text widget and a ListView.
At the third level, each item of the ListView contains a Text widget.
-Horizontal ListView
- Person's Name
- ListView
- Relation Name
- ListView
- Person's Name
Thanks in advance.
person.relations is a Map<String, List<Person>>
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Relationship Explorer"),
),
body: SafeArea(
child: BlocBuilder<RelationCubit, CubitState>(
bloc: _cubit,
builder: (_, state) {
if (state is RelationSuccessState) {
return ListView.builder(
scrollDirection: Axis.horizontal,
itemBuilder: (_, outerIndex) =>
_relationTreeView(context, outerIndex),
itemCount: _cubit.people.length,
);
} else {
return WaitWidget();
}
},
),
),
);
}
Widget _relationTreeView(BuildContext context, int outerIndex) {
var person = _cubit.people[outerIndex];
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(person.displayName ?? ''),
Expanded(
child: Container(
width: MediaQuery.of(context).size.width,
child: ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: person.relations?.length,
itemBuilder: (_, index) {
var persons = person.relations?[index];
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(person.relations!.keys.elementAt(index)),
Expanded(
child: Container(
width: MediaQuery.of(context).size.width,
child: ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: persons.length,
itemBuilder: (_, index) {
var innerPerson = persons[index];
return Text(innerPerson.displayName ?? '');
},
),
),
)
],
);
},
),
),
),
],
);
}
Wrap the list view with a container and give a height.

How to create horizontal Listview in listview builder flutter

I am using an API to build a movies application, I had a problem with making theListView to be from left to right instead of up and dowm.I am trying to create a horizontal list view but it displayed not as expected. You can see how it looks in the Image bellow. It works with me usually but it my first time to do that inside a listview builder. How to fix that?
Note: I want the full screen to be vertical and this part only horizontal.
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child:
FutureBuilder(
future: getData(),
builder: (BuildContext context,AsyncSnapshot snapshot){
if(snapshot.data == null){
return Container(
child: Center(
child:CircularProgressIndicator() ,),);
}
else{
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context,int i){
if(snapshot.data==null){
return Center(
child: CircularProgressIndicator(),
);
}
else{
return Container(
height: 250,
child: Card(
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Image(image: NetworkImage(snapshot.data[i].poster))
],
),
),
);
}}
);
}
}
,),
),
);
}
}
Image
You can use scrollDirection: Axis.horizontal,
inside the Listview.Builder to make it scroll horizontally and add a width for the container that's being returned.
Try below code hope its helpful to you . declare your ListView.builder() like below refer Listview here
Container(
width: double.infinity,
height: 180,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: 10,
itemBuilder: (BuildContext context, int i) {
return Container(
height: 50,
child: Card(
child: Image.network(
'https://tech.pelmorex.com/wp-content/uploads/2020/10/flutter.png',
),
),
);
},
),
),
Your result screen->

Fix Renderbox Error with Horizontal Listview

I am trying to change my ListView to be Shown horizontally instead of veritcally by default. However, when I have done this, I face the following an error. The error occurs at both the Widgets returned by the _buildListItem function and disappears when the scrolldirection is changed from vertical back to horizontal.
RenderBox was not laid out: RenderPointerListener#98473 relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
The code of the ListView Builder is
Widget _buildListItem (BuildContext context,DocumentSnapshot doc,int index)
{
if(index ==0)
{
return ListTile(title:Container( height:200,width: 100,child: Card(shape: new RoundedRectangleBorder(),child:Column(children:<Widget>[new Flexible(child:Text(widget.prayerName,style: TextStyle(fontSize: 32,fontWeight: FontWeight.bold))),Text(widget.blurb,style: TextStyle(fontSize: 28))]))));
}
else {
return RaisedButton(child: Text("a"),);
}
}
Here is the body of my build widget where my list view is declared
body: StreamBuilder(
stream: Firestore.instance.collection('quranKhwani').document(widget.docID).collection("juz").orderBy("juzNumber").snapshots(),
builder: (context,snapshot)
{
if(!snapshot.hasData)return const Text("Loading..");
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context,index)=>
_buildListItem(context,snapshot.data.documents[index],index),
itemCount: snapshot.data.documents.length,
);
}
)
));
Container(
height: _height //add height
child: StreamBuilder(
builder: (context, snapshot) {
return ListView.builder(
itemBuilder: (context, index) {},
scrollDirection: Axis.horizontal,
);
},
),
)
You have to give height to the StreamBuilder like:
body: Container(
height: 300, // Height you want
child:
StreamBuilder(
stream: Firestore.instance.collection('quranKhwani').document(widget.docID).collection("juz").orderBy("juzNumber").snapshots(),
builder: (context,snapshot)
{
if(!snapshot.hasData)return const Text("Loading..");
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: (context,index)=>
_buildListItem(context,snapshot.data.documents[index],index),
itemCount: snapshot.data.documents.length,
);
}
),
),
),

Flutter - Evenly spaced padding at the bottom of each ListView item

I am trying to add 70.0px at the bottom of each of my Container item in my ListView.
I have been looking at a lot of answers from StackOverflow and trying them in many different ways, and adding padding inside ListView doesn't work somehow. However, I still couldn't tackle it.
Can someone please give me some advice please?
class PortfolioRow extends StatelessWidget {
#override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
return Container(
margin: const EdgeInsets.only(bottom: 70.0),
child: StreamBuilder(
stream: Firestore.instance
.collection('portfolio')
.where('category')
.snapshots(),
builder: (context, snapshot) {
return ListView.builder(
physics: ScrollPhysics(),
shrinkWrap: true,
itemExtent: 450.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) => portfolioContainer(
context, snapshot.data.documents[index]));
}));
...
}
Widget portfolioContainer(
BuildContext context, DocumentSnapshot documentSnapshot) {
return Align(
child: SizedBox(
height: 540,
width: 330,
child: Container( // if I add padding bottom here, there will be a pixel overflow in my container
child: Column(children: [
Container(
...
}
ListView has a named constructor for exactly that : ListView.separated(). The separatorBuilder parameter lets you specify a widget that will be added at the bottom of each item :
ListView.separated(
itemCount: 25,
separatorBuilder: (BuildContext context, int index) => Divider(),
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('item $index'),
);
},
)