Flutter: Minimum height on horizontal list view

后端 未结 7 1078
鱼传尺愫
鱼传尺愫 2020-12-02 08:54

I\'m trying to create a horizontal scrolling list of items in Flutter, and I want that list to only take up the necessary height based on its children. By design “List

7条回答
  •  萌比男神i
    2020-12-02 09:07

    Wrapping the widget by Column with mainAxisSize: MainAxisSize.min worked for me. You can try to change the height here.

    var data = [
          {'name': 'Shopping', 'icon': Icons.local_shipping},
          {'name': 'Service', 'icon': Icons.room_service},
          {'name': 'Hotel', 'icon': Icons.hotel},
          {'name': 'More', 'icon': Icons.more}
    ];
    
    new Container(
            constraints: new BoxConstraints(
              minHeight: 40.0,
              maxHeight: 60.0,
            ),
            color: Colors.transparent,
            child: new ListView(
              scrollDirection: Axis.horizontal,
              children: data
                  .map((e) => Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          new Container(
                            width: 40,
                            height: 50, // try to change this.
                            color: Colors.transparent,
                            margin: EdgeInsets.only(right: 20, left: 4),
                            child: ClipOval(
                              child: Container(
                                padding: EdgeInsets.all(4),
                                color: Colors.white,
                                child: Icon(
                                  e["icon"],
                                  color: Colors.grey,
                                  size: 30,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ))
                  .toList(),
            ));
    

提交回复
热议问题