I have a list of items and based on a condition it should be displayed in Widget1() or Widget2(). It works fine up to there, the problem is when I get to the bottom of the list and want to scroll slowly up, the items rebuild and it jumps up and down the list.
Using the itemExent does solve the problem, but it's not the ideal solution. I need dynamic items.
Note: Widgets do not have a default size.
ListView.builder(
controller: scrollController,
cacheExtent: 10000,
addAutomaticKeepAlives: true,
dragStartBehavior: DragStartBehavior.start,
physics: AlwaysScrollableScrollPhysics(),
//itemExtent: 450.0,
itemBuilder: (context, index) {
if (index < list.length)
return MultiBlocProvider(
providers: [
/// some mandatory providers
],
child: some condition
? Widget1()
: Widget2());
return loading();
},
itemCount: list.length
);
Related
My question is very simple but I did not find a result.
In a ListView.builder (with horizontal scroll) for example if there are 5 elements the user can scroll them all at once. I would like that the user can scroll only 1 by 1.
In short I would like the animation of the list to stop at each element.
Example of my list :
Widget _list()
{
return ListView.builder(
physics: const ClampingScrollPhysics(),
scrollDirection: _horizontalDisplay? Axis.horizontal : Axis.vertical,
itemCount: data!.length,
itemBuilder: (context, index)
{
return widgetToDisplay(index);
}
);
}
The ListView.builder can't make it, you need to use carousel_slider package
In a page, my listView can be scrolled if I have at least 7 items or more. if there are only 3 items in my list then listView can not be scrolled.
I need to show Refresh Indicator (that circular indicator at the top of list) in my list even though I have less than 7 items (list can not be scrolled). How to do that?
Here is my code:
return RefreshIndicator(
onRefresh: () {},
child: ListView.builder(
physics: ClampingScrollPhysics(),
itemCount: controller.items.length + 1,
itemBuilder: (context, index) {
},
}
)
set the physics of the listView to AlwaysScrollableScrollPhysics()
return RefreshIndicator(
onRefresh: () {},
child: ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
itemCount: controller.items.length + 1,
itemBuilder: (context, index) {
},
}
)
You can do this if you don't wanna lose your clamping physics:
physics: ClampingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
As well as you can wrap your Listview, you can change this behavior so that the ListView only occupies the space it needs
return RefreshIndicator(
onRefresh: () {},
child: ListView.builder(
shrinkWrap: true,
physics: ScrollPhysics(),
itemCount: controller.items.length + 1,
itemBuilder: (context, index) {
},
}
)
I have nested ListView in a ListView.
So I want to disable scroll for child ListView programmatically.
How to do it.
Thanks,
You can prevent it by using physics: NeverScrollableScrollPhysics() for example
Expanded(
child: ListView.builder(
physics: NeverScrollableScrollPhysics(), <-----
scrollDirection: Axis.horizontal,
itemCount: status == false ? list.length : 5,
itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: list[i], child: ListItem()),
),
If you want to set it programatically, use a setState() to update the physics type like this:
ScrollPhysics physics = AlwaysScrollableScrollPhysics();
return Stack(
children: [
ListView(
physics: physics,
),
RaisedButton(
child: Text("Disable scrolling"),
onPressed: () {
setState((){
if(physics is AlwaysScrollableScrollPhysics){
physics = NeverScrollableScrollPhysics();
} else {
physics = AlwaysScrollableScrollPhysics();
}
});
},
),
]
);
You need to set this "physics" property value "NeverScrollableScrollPhysics" inside the nested listview.
example:
ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemBuilder: () {}
),
I have a page the has a list with refresh indicator. When I have many elements in the list (filling the whole view and more, i.e. I can scroll), the refresh indicator works.
However, when I have only one or two elements in the list (nothing to scroll), then the refresh indicator doesn't work. Is this the expected behaviour? Is there a way to get the indicator to work also for short lists?
I tried wrapping the list view (child of refresh indicator) with a scroll bar but the problem still persists.
Future<void> _onRefresh() async {
_getPeople();
}
#override
Widget build(BuildContext context) {
super.build(context);
return new Container(
child: _items.length == 0
? Center(child: CircularProgressIndicator())
: new RefreshIndicator(
onRefresh: _onRefresh,
child: Scrollbar(
child: new ListView.builder(
controller: controller,
itemBuilder: (context, index) {
return PeopleCard(
People: _items[index],
source: 2,
);
},
itemCount: _items.length,
),
)),
);
}
If the length of the items list is 1 or 2 or any amount that doesn't need scrolling, refresh indicator should also work.
Set the physics to AlwaysScrollableScrollPhysics just like the below code.
new RefreshIndicator(
key: _refreshIndicatorKey,
color: Colors.blue,
onRefresh: (){
setState(() {
_future = fetchPosts(http.Client());
_getAvalailableSlots();
});
},
child: ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
itemCount: data.length,
itemBuilder: (context, i) {
//do something
}
)
)
Case 1:
Use AlwaysScrollableScrollPhysics() in your Listview or SingleChildScrollView.
physics: const AlwaysScrollableScrollPhysics(),
Case 2:
If you want to use other scroll physics, use this way...
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),
Case 3:
Make sure you didn't make shrinkWrap, true. ShrinkWrap has to be false like default.
shrinkWrap: true. ❌
shrinkWrap: false. ✅
shrinkWrap: false,
Case 4:
If you have any custom widgets other than a listview, use stack and listview this way...
RefreshIndicator(
onRefresh: () async {
print('refreshing');
},
Stack(
children: [
YourWidget(),
ListView(
physics: const AlwaysScrollableScrollPhysics(),
),
],
),
),
To use with BouncingScrollPhysics:
RefreshIndicator(
onRefresh: () async {
},
child: ListView.builder(
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
itemBuilder: (context, index) {
return Text("${index}");
}),
To make RefreshIndicator always appear, set physics: const AlwaysScrollableScrollPhysics() for the scrollable child like below.
ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: <Widget>[widget.bodyWidget]
)
If you still have some errors, the below might help.
ListView widget needs to have height value to be "always" scrollable vertically. So wrap it with SizedBox or some other proper widget and set the height.
How to fix the height? Get the height of viewport using MediaQuery.of(context).size.height and, if needed, subtract the value of non-scrollable part.
If you use SingleChildScrollView and it's not working with AlwaysScrollableScrollPhysics(), it is because the child doesn't have height. fix the child's height or just use ListView.
To sum up, the following worked for me.
sizes.bottomNavigationBar and sizes.appBar are the constant variables I made to customize the appbar and navbar.
RefreshIndicator(
onRefresh: widget.onRefresh,
child: SizedBox(
height: MediaQuery.of(context).size.height -
sizes.bottomNavigationBar -
sizes.appBar,
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: <Widget>[widget.bodyWidget]
)),
)
If you're finding any reliability, Refresh indicator does not show up part of the official doc would help.
If you're using ListView inside of Column with Expanded.
Try Removing
shrinkWrap:true
I'd like to add an info box on top of the listview and instead of having it fixed, I'd like it to scroll along with the content while the page is scrolled down. Instead what I'm experiencing is that it stays always in fixed position no matter what I try to do.
My code:
return new Column(children: <Widget>[
new Text('I want to be scrollable as well with other content as well'), // I want widget or widgets here to be part of the scroll instead of them being fixed.
new Expanded(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return index >= state.posts.length
? BottomLoader()
: PostWidget(post: state.posts[index]);
},
itemCount: state.hasReachedMax
? state.posts.length
: state.posts.length + 1,
controller: _scrollController,
),
),
]);
Is this possible to achieve?
I think you could return your Text widget or any other widget at the 0 position ?
return new Column(children: <Widget>[
new Text('I want to be scrollable as well with other content as well'), // I want widget or widgets here to be part of the scroll instead of them being fixed.
new Expanded(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
if (index == 0) {
return Text('Your widget');
else {
return index >= state.posts.length
? BottomLoader()
: PostWidget(post: state.posts[index]);
}
},
itemCount: state.hasReachedMax
? state.posts.length
: state.posts.length + 1,
controller: _scrollController,
),
),
]);