我能找到的最相似的问题是'how to create a row of scrollable text boxes or widgets in flutter inside a ListView?',但他们的解决方案只是为ListView的Container分配一个高度:
import 'package:flutter/material.dart';
// Define a 'main' function to run when our app starts
void main() {
var app = new MaterialApp(
home: new SafeArea(
left: false,
top: true,
right: false,
bottom: false,
child: new Scaffold(
body: new Container(
color: Colors.green,
height: 50.0,
child: new ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
new Icon(Icons.check_circle, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.add, size: 50.0),
new Icon(Icons.close, size: 50.0),
],
),
),
appBar: AppBar(
title: new Text("Hello")
),
),
),
);
runApp(app);
}
根据Julien Louage,除非添加子项,否则Container会扩展以填充其父项,在这种情况下,它将缩小以适应子项的维度 . 如果您要删除Container 's height property, it will go back to filling its parent, as if it didn' t包含一个孩子 .
合理的结论是ListView导致了这个问题; ListView希望尽可能大,并且没有明显的方法来改变它 .
像Row这样的其他小部件有缩小的方法来包含子节点,但ListView却没有?要查看此示例以及Container的大小调整约定,请删除一些图标以避免像素溢出并进行以下更改:
//height: 50.0,
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
//child: new ListView(
// scrollDirection: Axis.horizontal,
那么如何才能使ListView缩小以包含子项(如Row可以与CrossAxisAlignment.center一样)而不设置Container的高度?应该有办法!