如何在Flutter中将ListView中的项目覆盖到整个屏幕

时间:2019-04-06 20:27:15

标签: flutter flutter-layout flutter-listview

我想要一个具有固定项目数的可滚动视图。第一项应覆盖父容器,然后用户可以向下滚动以查看其余项。

我尝试将Expanded添加到第一项,但出现白屏

  ListView(
    children: <Widget>[
      Expanded(child: MainInfo(),),
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),    
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),  
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),  
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),   
    ],
  ),

我应该使用ListView还是SingleChildScrollView(还不能与Expanded一起使用)?

2 个答案:

答案 0 :(得分:0)

Expanded不能在可滚动小部件中使用。您可以执行以下操作:

ListView(
    children: <Widget>[
      Container(height:MediaQuery.of(context).size.height,child: MainInfo(),),
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),    
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),  
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),  
      Divider(height: 2, color: Colors.black,),    
      MainInfo(),   
    ],
  ),

答案 1 :(得分:0)

我设法通过使用返回ListView的LayoutBuilder来实现这一目标

LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
    return ListView(
      children: <Widget>[
        Container(
          child: MainInfo(),
          height: constraints.maxHeight,
        ),
        Divider(
          height: 2,
          color: Colors.black,
        ),
        MainInfo(),
        Divider(
          height: 2,
          color: Colors.black,
        ),
        MainInfo(),
        Divider(
          height: 2,
          color: Colors.black,
        ),
        MainInfo(),
        Divider(
          height: 2,
          color: Colors.black,
        ),
        MainInfo(),
      ],
    );
  }),