ListView.builder inside SliverList SliverChildBuilderDelegate not rendering Images on first load - flutter

my images (from api) don't render at first load unless I scroll down and back to top again
here is my code structure
CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildBuilderDelegate(_, index) {
return Column(children:
[
// SLIVERBUILDER INDEX
Text(somedata[_index]),
// MY IMAGE BUILDER
Container(child: ListView.builder(itemBuilder: (_, int index){
return Image.network(arrayOfImages[index]);
}))
]
);
}),
]
)

Try these:
Wrap a column with a container or any parent that could give its child constraints.
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container( // warp with container
height: 450,
child: Column(children: [
// SLIVERBUILDER INDEX
Text('title'),
Wrap ListView.builder with Expanded and add itemCount Property. (In your case 'arrayOfImage.length')
// MY IMAGE BUILDER
Expanded( // add this widget
child: ListView.builder(
itemCount: arrayOfImage.length, // add this line

Related

Create a page view with 2 different scrollable list with different index

I want to create a screen that can display a scrollable list.
Based on a condition, I want to be able to display another list.
My program works however the index is the same for the 2 lists so when I scroll a list, the second list is scroll too
My code :
return PageView.builder(
scrollDirection: Axis.vertical,
itemCount: category[state.indexView].length,
itemBuilder: category[state.indexView][0].containsKey('video')
? (context2, index2) {
return Stack(
children: [
VideoWidget(
videoUrl: category[state.indexView][index2]['video'],
asset: true,
videoFile: File(''),
),
if (category[state.indexView][index2].containsKey('image'))
Center(child: Image(image: AssetImage(category[state.indexView][index2]['image']))),
_postContent(d_height)
],
);
}
: (context, index) {
return Stack(
children: [
if (category[state.indexView][index].containsKey('image'))
Center(child: Image(image: AssetImage(category[state.indexView][index]['image']))),
_postContent(d_height)
],
);
});
You can create second page in pageview similar to first page when you want to jump on second page then pass that index to another page and use it as current index in listview.

how to Add some design at top after that listview with dynamic size list and then below some some design for advertisement and comment in flutter

Scaffold(
appBar: AppBar(
title: Text("Design test..."),
),
body: Container(
margin:EdgeInsets.fromLTRB(0, MediaQuery.of(context).padding.top, 0, 0
),
child: Column(
children: [
Container(//for Some Kind of design at top),
Column(
children: [
ListView.builder(
itemCount: listLength.length,
itemBuilder: (BuildContext buildContext, int index) {
return ListTile(title: Text("hello world")
);
}),
//i want to add some design here after the list view for advertisement and comment
],
),
],
),
),
);
my listview.builder() item length is dynamic so i want to expand my list view as much as it requied and when it ends i need some design just like youtube privious design where on the top video player after that video list and at the end comment part.thank you.
Yes you can achieve this, Refer to this code.
use the build method like this.
#override
Widget build(BuildContext context) {
return Scaffold(
body : ListView(
//Base Listview, it can be scrollable if content is large also
children:[
Container(
//Sample widget you can use your own
child:Text("Here is some design At Top")
),
ListView.builder(
//use shrinkWrap:true and physics:NeverScrollableScrollPhysics()
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 10, //it can be dynamic and null checks can be appield,
itemBuilder: (context, index){
return Text("ListItem $index");
},
),
Container(
//Sample widget you can use your own
child: Text("Some Design for Ads")
),
Container(
//Sample widget you can use your own
child: Text("Some Design for Commentes")
)
]
)
);
}
Mark answer as right if it helped !!

Flutter: unexpected space at the top of ListView

I have the following source code:
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
controller: scrollController,
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(context, cardIndex) {
return Container(
color: Colors.white,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Main Course',
style: kRestaurantMenuTypeStyle,
),
ListView.builder(
itemCount: menuCards[cardIndex].menuItems.length,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
itemBuilder: (context, itemIndex) {
return RestaurantMenuItem(
menuItem: menuCards[cardIndex].menuItems[itemIndex],
);
},
),
],
),
);
},
childCount: menuCards.length,
),
),
],
),
);
}
Unfortunately, the ListView.builder() creates this extra space on top automatically. This is shown in the image below. That is the big white space between the 'Main Course' and 'Pancit Malabon' texts.
I don't understand why ListView does that. How do I remove the space?
To avoid this behaviour of listview, override padding property with a zero [padding]
return ListView.builder(
padding: EdgeInsets.zero,
itemCount: data.items.length,
itemBuilder: (context, index) {}
);
Looking at your screenshot, the ListView scrolls close to the top of the screen and by default, ListView adds a padding to avoid obstructing the system UI. So a zero padding would remove the extra space.
By default, ListView will automatically pad the list's scrollable
extremities to avoid partial obstructions indicated by MediaQuery's
padding. To avoid this behavior, override with a zero padding
property.
Source : ListView
I solved the issue by adding a padding to my list view like so:
ListView.builder(
padding: EdgeInsets.only(top: 0.0),
...
),
I don't understand why the solution works. If someone can explain the bug, I can accept theirs as the correct answer.
You can wrap your listview with MediaQuery.removePadding
MediaQuery.removePadding(
context: context,
removeTop: true,
child: ListView(...),
)

How to show the listview in a listview

I got a problem when I am trying to show the listview in a listview. Right now it shows me the error "Vertical viewport was given unbounded height.". I try to add a container and set up the height to the second listview and it works. But I don't want to restrict the height of the second listview.
return new Scaffold(
body: new Column(
children: <Widget> [
new Expanded(
child: ListView(
children: <Widget> [
new Container(), // something else
new ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => new MenuCard(menuStore[index]),
itemCount: menuStore.length,
padding: new EdgeInsets.symmetric(vertical: 16.0)
)
]
)
)
]
)
);
You may want to set the ListView property shrinkWrap to true:
/// Whether the extent of the scroll view in the [scrollDirection] should be
/// determined by the contents being viewed.
///
/// If the scroll view does not shrink wrap, then the scroll view will expand
/// to the maximum allowed size in the [scrollDirection]. If the scroll view
/// has unbounded constraints in the [scrollDirection], then [shrinkWrap] must
/// be true.
...
/// Defaults to false.
final bool shrinkWrap;
In order to fix the Given Error in your code you need to shrinkWrap: true, in ListView.builder and ListView.
return new Scaffold(
body: new Column(
children: <Widget> [
new Expanded(
child: ListView(
shrinkWrap:ture,
children: <Widget> [
new Container(), // something else
new ListView.builder(
shrinkWrap:ture,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) => new MenuCard(menuStore[index]),
itemCount: menuStore.length,
padding: new EdgeInsets.symmetric(vertical: 16.0)
)
]
)
)
]
)
);
Try out this answer here, Instead of GridView you can use ListView, also remove the crossAxisCount property from GridView.
https://stackoverflow.com/a/53852317/3811162

Flutter listview is not visible

Widget servicesListview() {
return Container(
decoration: new BoxDecoration(color: const Color(0xFFEAEAEA)),
child: Column(
children: <Widget>[
ListView.builder(
scrollDirection: Axis.vertical,
itemCount: menServicesList.length,
itemBuilder: (BuildContext context, int index) {
Text(menServicesList[index].name);
}),
],
));
}
I am implementing listview in my flutter project whenever i call this method list is not visible.The page becomes blank,help me to solve this
I have a same problem.
The ListView.builder don't work inside a Column, this necessary use the Expanded.
The Expanded widget allows you to expand and fill the space with the relative widget.
See this:
child: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
.......
)
)
]
)
Wrap your ListView inside of a Expanded or a Container widget, if you use a Container, you'll need to set a height/width.
This happens because ListView doesn't have a height/width property to edit.
EDIT
Don't forget to return your Text inside the itemBuilder property.
You're missing the return statement in your itemBuilder
itemBuilder: (BuildContext context, int index) {
return Text(menServicesList[index].name);
}),
or
itemBuilder: (BuildContext context, int index) => Text(menServicesList[index].name)),
If you remove Column widget from the ListView, the list will definitely appear
and if you want to give a decoration property to each element of a ListView.
Widget servicesListview() {
return ListView.builder(
scrollDirection: Axis.vertical,
itemCount: menServicesList.length,
itemBuilder: (BuildContext context, int index) {
Container(
decoration: new BoxDecoration(color: const Color(0xFFEAEAEA)),
child: Text(menServicesList[index].name)
);
})
}