简体   繁体   中英

Unable to scroll ListView in Flutter

I am new to flutter and am currently working on a project that is in need of a scrollable Column. So I used ListView. The CollapseTile is a refactored widget.

Here is the code:

Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
          children: <Widget>[
            ListView(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                children: <Widget>[
                  SizedBox(height: 20.0),
                  CollapseTile(
                    text: 'Skin',
                    children: <Widget>[
                      ListView.builder(
                          shrinkWrap: true,
                          physics: AlwaysScrollableScrollPhysics(),
                          itemCount: data['skin'].length,
                          itemBuilder: (BuildContext context, int index) {
                            print(_selectedSkin);
                            return CheckboxListTile(
                              title: Text(data['skin'][index]['name']),
                              value: _selectedSkin
                                  .contains(data['skin'][index]['name']),
                              secondary: Image(
                                image: AssetImage(
                                    'images/${skinImageName[index]}'),
                              ),
                              onChanged: (bool selected) {
                                _onSelected(
                                    selected, data['skin'][index]['name']);
                              },
                            );
                          })
                    ],
                  ),
                  CollapseTile(
                    text: 'Teeth',
                    children: <Widget>[],
                  ),
                  CollapseTile(
                    text: 'Mouth',
                    children: <Widget>[],
                  ),
                  CollapseTile(
                    text: 'Eye',
                    children: <Widget>[],
                  ),
                  CollapseTile(
                    text: 'Hair',
                    children: <Widget>[],
                  ),
                  CollapseTile(
                    text: 'Nails',
                    children: <Widget>[],
                  ),
                ],
              ),
            ),
          ],
        );
  }

However, the ListView does not scroll and RenderFlex is overflowed.

Add SingleChildScrollView & add physiscs as NeverScrollableScrollPhysics inside both ListView .

 Scaffold(
  body: SingleChildScrollView( 
    child: Column(
      children: <Widget>[
    ListView(
    scrollDirection: Axis.vertical,
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      children: <Widget>[
        SizedBox(height: 20.0),
        CollapseTile(
          text: 'Skin',
          children: <Widget>[
            ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemCount: data['skin'].length,
                itemBuilder: (BuildContext context, int index) {
                  print(_selectedSkin);
                  return CheckboxListTile(
                    title: Text(data['skin'][index]['name']),
                    value: _selectedSkin
                        .contains(data['skin'][index]['name']),
                    secondary: Image(
                      image: AssetImage(
                          'images/${skinImageName[index]}'),
                    ),
                    onChanged: (bool selected) {
                      _onSelected(
                          selected, data['skin'][index]['name']);
                    },
                  );
                })
          ],
        ),
        CollapseTile(
          text: 'Teeth',
          children: <Widget>[],
        ),
        CollapseTile(
          text: 'Mouth',
          children: <Widget>[],
        ),
        CollapseTile(
          text: 'Eye',
          children: <Widget>[],
        ),
        CollapseTile(
          text: 'Hair',
          children: <Widget>[],
        ),
        CollapseTile(
          text: 'Nails',
          children: <Widget>[],
        ),
      ],
    ),
),
  ),
],
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM