flutter add widgets on top and bottom to a listview

后端 未结 4 1228
渐次进展
渐次进展 2021-02-05 23:37

Trying to add some widgets to before of a listview.... I searched and found to use expanded like:

  return Scaffold(
    appBar: AppBar(
      title: Text(\'Test         


        
相关标签:
4条回答
  • 2021-02-06 00:04
    body: Container(
      width: MediaQuery.of(context).width,
      height: MediaQuery.of(context).height,
      child: ListView(
         children: Widget[
           Container(
             width: MediaQuery.of(context).width,
             height: 50,
             alignment: AlignmentDirectional.center
             child: Text("HEADER")
          ),
          for(var i =0;i<providerApp.domains.length;i++)
          Container(
             width: MediaQuery.of(context).width,
             height: 50,
             alignment: AlignmentDirectional.center
             child: Text("Child $i")
          ),
          Container(
             width: MediaQuery.of(context).width,
             height: 50,
             alignment: AlignmentDirectional.center
             child: Text("Footer")
          )
        ]
      )
    )
    
    0 讨论(0)
  • 2021-02-06 00:10

    You can achieve this by using listview inside list view, below is sample code please check

     body: ListView(
        children: <Widget>[
          Container(
            height: 40,
            color: Colors.deepOrange,
            child: Center(
              child: Text(
                'Header',
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
          ),
          ListView.builder(
            physics: ScrollPhysics(),
            shrinkWrap: true,
            itemCount: 50,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                color: Colors.lime,
                height: 60,
                child: Center(
                  child: Text(
                    'Child $index',
                    style: TextStyle(color: Colors.black, fontSize: 16),
                  ),
                ),
              );
            },
          ),
          Container(
            height: 40,
            color: Colors.deepOrange,
            child: Center(
              child: Text(
                'Footer',
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
          ),
        ],
      ),
    
    0 讨论(0)
  • 2021-02-06 00:17

    Try this.

    body : Container(
          constraints: BoxConstraints.expand(),
          padding: const EdgeInsets.all(10.0),
          child : ListView.builder(
             itemCount : providerApp.domains.length + 1,
             itemBuilder : (BuildContext context, int index){
                 Widget result;
                 if(index == 0){
                   result = Text("Header");
                 }else{
                   result = Container(..Your list item code here.);
                 }
                 return result;
             },
          ),
        )
    

    PS: If you want add widget to bottom of list, condition is provider.domain.length + 1.

    I hope to help you.

    0 讨论(0)
  • 2021-02-06 00:22

    You should be returning ListTile not Container now and your tiles will be titled according to the ListTile title: property field where "providerapp.domains" is the list of strings.

    Container(
                child: Column(children: <Widget>[
          Text('header'),
          ListView.builder(
            itemCount: providerApp.domains.length,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                title: Text('${(providerApp.domains)[index]}'),
              );
            },
          ),
        ]),
    )
    
    0 讨论(0)
提交回复
热议问题