DolDurma
DolDurma

Reputation: 17340

implementing nested ListView inside SingleChildScrollView

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"));
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: 2

Views: 3226

Answers (3)

Kherel
Kherel

Reputation: 16225

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}'));
          },
        ),
      ),
    );
  }
}

Upvotes: 1

Ujjwal Mainali
Ujjwal Mainali

Reputation: 388

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"));
              },
            ),
          ),
        ],
      ),
    )

Upvotes: 3

Federick Jonathan
Federick Jonathan

Reputation: 2864

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(),
          ),
        ],
      ),
    );

Upvotes: 1

Related Questions