简体   繁体   中英

Flutter How to Display a List of Widgets in a Container

So in my app, I am building a list of widgets, which is basically just a container with text in it. I am doing this for a variety of reasons, but basically here is where I am having the problem.

I can view this list in a ListView.Builder, but I do not want to view them that way. I want to be able to view them in a container. Then wrap down the container as items get added.

Below is a mock up of what I am trying to do.

在此处输入图片说明

Right now my page looks like this.

body: Column(children: <Widget>[
          Expanded(
            flex: 3,
            child: Container(
                width: MediaQuery.of(context).size.width*0.8,
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: ListView.builder(
                          itemCount: hashList.length,
                          itemBuilder: (context, index){
                            return Container(
                              child: hashList[index],
                            );
                          }),
                    )
                  ],

                ),
                ),
          ),
          Expanded(
            flex: 6,
            child: Container(
              color: Colors.black,
              child: ListView.builder(
                itemCount: snapshot.length,
                itemBuilder: (context, index) {
                  return Tag(
                    callback: (list) => setState(() => hashList = list),
                    tag: snapshot[index].data["title"],
                    list: hashList,
                    id: index,
                  );
                },
              )
    ))]),

And the Tag Widget adds to the list with the following.

void _handleOnPressed() {
    setState(() {
      isSelected = !isSelected;
      isSelected
          ? _animationController.forward()
          : _animationController.reverse();
      isSelected ? widget.list.add(
          Container(
            padding: EdgeInsets.all(10),
            height: 45,
            child: Text(widget.tag, style: TextStyle(color: Color(0xffff9900), fontSize: 20, fontFamily: 'Dokyo'),),
              decoration: BoxDecoration(
            border: Border.all(color: Color(0xffff9900),),
            borderRadius: BorderRadius.circular(10))
          )): widget.list.removeAt(widget.id);
    });
  }
}

Adding the list to a listview, I can do, but it is not the desired affect.

I have no clue how to do what I am looking for and I am completely lost.

Try and add shrinkWrap to the ListView Builder

child: ListView.builder(shrinkWrap:true, <-- this
       ....           # then continue your logic

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