本文介绍了Flutter ListView收缩包装-嵌套ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ListView内有一个ListView,而内部ListView不知道它应该有多高,所以我必须给它指定特定的高度,例如SizedBox。但是问题是我实际上希望内部ListView收缩包装,以便它不会滚动/占用父ListView中的不必要空间。



请先谢谢

解决方案

这听起来像是

  import'dart:async'; 
导入的 package:flutter / material.dart;

void main(){
runApp(new MaterialApp(
home:new Scaffold(
body:new CustomScrollView(
slivers:[
new SliverToBoxAdapter(
子代:new Container(高度:100.0,颜色:Colors.blueAccent),
),
new SliverList(
委托人: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),
),
],
),
),
));
}


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

解决方案

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

这篇关于Flutter ListView收缩包装-嵌套ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:24