Flutter ListView shrink wrap - nested ListView

2019-04-16 11:41发布

问题:

I have a ListView inside a ListView and the inner ListView doesn't know how height it should be so I have to give it a specific height with for example a SizedBox. However the problem is that I actually want the inner ListView to shrink wrap so that it wont scroll/take unnecessary space within the parent ListView.

Thanks in advance

回答1:

This sounds like a good use case for CustomScrollView.

import 'dart:async';
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new Scaffold(
      body: new CustomScrollView(
        slivers: [
          new SliverToBoxAdapter(
            child: new Container(height: 100.0, color: Colors.blueAccent),
          ),
          new SliverList(
            delegate: new SliverChildListDelegate(
              new List<Widget>.generate(10, (int index) {
                return new Text(
                  'Item $index',
                  style: new TextStyle(fontSize: 42.0),
                );
              }),
            ),
          ),
          new SliverToBoxAdapter(
            child: new Container(height: 100.0, color: Colors.tealAccent),
          ),
        ],
      ),
    ),
  ));
}


标签: dart flutter