scrol is not working when i used ListView.builder() in SingleChildScrollView . i have to use ListView.builder() and SingleChildScrollView . do i have a alternative for ListView.builder() and SingleChildScrollView. my app design is irregular , i know and i will fixed .
class ListelemeEkrani extends StatefulWidget {
const ListelemeEkrani({super.key});
@override
State<ListelemeEkrani> createState() => _ListelemeEkraniState();
}
class _ListelemeEkraniState extends State<ListelemeEkrani> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [liste()],
),
),
),
);
}
ListView liste() {
return ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 200,
width: MediaQuery.of(context).size.width,
color: Colors.red,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Align(
alignment: Alignment.topLeft,
child: Text(
"yerin adı",
style: Theme.of(context).textTheme.headline5,
),
),
Text("buraya detay yazısı gelecek" * 2),
Expanded(
child: Image.asset("assets/manzara.jpg",
width: MediaQuery.of(context).size.width,
fit: BoxFit.contain),
),
],
),
),
),
);
},
itemCount: listeSonucu.values.length,
);
}
}
enum listeSonucu { bir, iki, uc, dort, bes, alti, yedi }
2
Answers
add scroll physics as NeverScrollableScrollPhysics() to your listview.
It’s not possible to directly add a
ListView.builder
inside aSingleChildScrollView
because both widgets are designed to be scrollable, which can lead to conflicts when used together.When you use a
ListView.builder
, it creates a scrollable widget that can display a large number of items. On the other hand, aSingleChildScrollView
creates a scrollable widget that can contain a single child widget.If you try to nest a
ListView.builder
inside aSingleChildScrollView
, you will end up with two widgets that are competing for control over the scrolling behavior. This can lead to unexpected results, such as theListView.builder
scrolling independently of theSingleChildScrollView
, or theSingleChildScrollView
preventing theListView.builder
from scrolling.To solve this issue we have two answers :
You can consider using a different widget such as a
CustomScrollView
which is designed to work with multiplescrollable widgets. Or you can wrap the
ListView.builder
with aSizedBox
orContainer
widget that sets a fixed height, so thatthe
ListView.builder
doesn’t try to scroll beyond its boundaries.Use
NeverScrollableScrollPhysics
in sublistHere’s an example of how to use
NeverScrollableScrollPhysics
to disable scrolling for a ListView widget: