2

in this sample code i want to put nested ListView inside SingleChildScrollView, but i get this error:

RenderBox was not laid out: RenderRepaintBoundary#8de00 relayoutBoundary=up1 NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1694 pos 12: 'hasSize'

my code:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'test',
      home: Scaffold(
        appBar: AppBar(
          title: Text("Scrollview Demo"),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              Container(
                height: 50.0,
                color: Colors.green,
                child: Center(
                  child: Text('message'),
                ),
              ),
              Expanded(
                child: ListView.builder(
                  itemCount: 30,
                  itemBuilder: (context, index) {
                    return ListTile(title: Text("Index : $index"));
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

3 Answers 3

3

In ListView you can set

shrinkWrap: true

so that ListView only occupies the space it needed.

To disable the scrolling on ListView so it uses that of SingleChildScrollView you can set the

physics: NeverScrollableScrollPhysics().

You need to remove the Expanded which set the child to take the available screen in case of here is infinite.

Here:

SingleChildScrollView(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            height: 50.0,
            color: Colors.green,
            child: Center(
              child: Text('message'),
            ),
          ),
          Flexible(
            child: ListView.builder(
              itemCount: 30,
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return ListTile(title: Text("Index : $index"));
              },
            ),
          ),
        ],
      ),
    )
Sign up to request clarification or add additional context in comments.

1 Comment

can you try it with flexible widget instead of expanded and setting the mainAxisSize of column to min.
1

Instead of using SingleChildScrollView then use Column as the child just use ListView.

    return Scaffold(
      body: ListView(
        children: <Widget>[
          Container(
            height: 104,
            child: Card(
              clipBehavior: Clip.antiAliasWithSaveLayer,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8.0),
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[],
              ),
            ),
          ),
          ListView(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
          ),
        ],
      ),
    );

Comments

1

Ones I was in the same situation and did like that:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'test',
      home: Scaffold(
        appBar: AppBar(
          title: Text("Scrollview Demo"),
        ),
        body: ListView.builder(
          itemCount: 30 + 1,
          itemBuilder: (context, index) {
            if (index == 0) {
              return Container(
                height: 50.0,
                color: Colors.green,
                child: Center(
                  child: Text('message'),
                ),
              );
            }
            return ListTile(title: Text('Index : ${index -1}'));
          },
        ),
      ),
    );
  }
}

Comments

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.