skip to Main Content

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


  1. add scroll physics as NeverScrollableScrollPhysics() to your listview.

    ListView liste() {
        return ListView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
    
    Login or Signup to reply.
  2. It’s not possible to directly add a ListView.builder inside a SingleChildScrollView 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, a SingleChildScrollView creates a scrollable widget that can contain a single child widget.

    If you try to nest a ListView.builder inside a SingleChildScrollView, you will end up with two widgets that are competing for control over the scrolling behavior. This can lead to unexpected results, such as the ListView.builder scrolling independently of the SingleChildScrollView, or the SingleChildScrollView preventing the ListView.builder from scrolling.

    To solve this issue we have two answers :

    1. You can consider using a different widget such as a
      CustomScrollView which is designed to work with multiple
      scrollable widgets. Or you can wrap the ListView.builder with a
      SizedBox or Container widget that sets a fixed height, so that
      the ListView.builder doesn’t try to scroll beyond its boundaries.

    2. Use NeverScrollableScrollPhysics in sublist

    NeverScrollableScrollPhysics is a subclass of ScrollPhysics in the Flutter framework. It’s a special type of scroll physics that disables
    scrolling for a given scroll view or scrollable widget.

    When you apply NeverScrollableScrollPhysics to a scrollable widget, it
    prevents the widget from scrolling, regardless of whether the user
    attempts to scroll it by dragging or using a scroll controller.

    Here’s an example of how to use NeverScrollableScrollPhysics to disable scrolling for a ListView widget:

    ListView.builder(
      physics: NeverScrollableScrollPhysics(),
      itemCount: 10,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: Text('Item $index'),
        );
      },
    ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search