1

I have a ListView(2) nested inside another ListView(1).

Whenever I replace 2 with a container, the whole page scrolls fine but as soon as I enable both list views inside each other, both stop scrolling.

This is the main ListView implementation

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(20.0),
      child: ListView(
        primary: true, //does not have any effect
        physics: const AlwaysScrollableScrollPhysics(),
        children: [
          Container( ...etc.

And this is the second listview implementation inside the first listview:

Expanded(
    child: ListView(
      primary: false,
      padding: const EdgeInsets.only(top: 20.0),
      children: snapshot.map((data) => _buildListItem(context, data)).toList(),
    ),
  );

1 Answer 1

2

Expanded cannot be used in a ListView

Try this using SizedBox and setting the height to a desire number

If you want the ListView(2) to take the entire screen (and more) try this:

  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Container(
            color: Colors.pink,
            width: 400,
            height: 800
          ),
          SizedBox(
            height: MediaQuery.of(context).size.height,
            child: ListView(
              children: List.generate(300, (i) => Text(i.toString()))
            ),
          )
        ]
      )
    );
  }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Worked Perfectly. Expanded was the problem.
Hey @Josteve how do I make it so that the SizedBox takes as much height as required by the ListView? I am fetching 100 posts from db and users can scroll till the very end. I can't provide that in an static way.
Actually nesting listView inside another is not a good design. Use Slivers instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.