[英]Flutter - How to have a non-scrollable max height ListView.builder?
My problems with ListView.builder
in my specific scenario:我在特定情况下使用
ListView.builder
的问题:
ListView.builder
so that whatever is inside that can be infinitely scrollable.ListView.builder
一个高度,这样里面的任何东西都可以无限滚动。shrinkWrap
property is only helpful if you are planning to give ListView.builder
a predetermined height. shrinkWrap
属性仅在您计划为ListView.builder
提供预定高度时才有用。 What I require:我需要什么:
A ListView.builder
that acts just like a Column
which will display all the widgets so that there is no scrolling, so it does not require a height but will be like an Expanded
widget.一个
ListView.builder
就像一个Column
一样,它会显示所有的小部件,这样就不会滚动,所以它不需要高度,但就像一个Expanded
小部件。
I want to use ListView.builder
as my widgets are generated through Lists and Dictionaries , but if it's not necessary to use it, please give me an alternative where I can use my Lists and Dictionaries to generate widgets on demand and update the state.我想使用
ListView.builder
因为我的小部件是通过Lists 和 Dictionaries生成的,但是如果没有必要使用它,请给我一个替代方案,我可以使用我的 Lists 和 Dictionaries 按需生成小部件并更新 state。
Example:例子:
List<Map> data = [{'data': 'here'}];
// ListView.builder solution:
ListView.builder(
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (context, index) {
if (condition A) {
return Text(data[index]['data']);
} else if (condition B) {
return Text(data[index]['data']);
} else {
return Container();
}
}
);
// This solution requires ListView.builder to have its own height
// constraints, so setting a height will cause it to be scrollable if
// the widgets take up space after those constraints are exceeded.
// NeverScrollablePhysics() is not the point here.
// Column solution:
// A widget and for loop is required to build before the build method is called:
List<Widget> widgetList = [];
for (var dataSet in data) {
// If conditions here:
widgetList.add(dataSet['data'];
}
return Column(
children: widgetList,
);
// Problem: Widgets such as FilterChip which use state changes don't
// get updated as these come from a prebuilt Widget variable. Tapping
// on them does nothing.
ListView widget contains a property named "Physics" which can accept "NeverScrollableScrollPhysics()" and that will disable scrolling in your ListView widget. ListView 小部件包含一个名为“Physics”的属性,它可以接受“NeverScrollableScrollPhysics()”,这将禁用您的 ListView 小部件中的滚动。 here is an example,
这是一个例子,
return Scaffold(
body: ListView.builder(
physics: const NeverScrollableScrollPhysics(),
itemCount: 10,
itemBuilder: (context, index) => Container(),
),
);
I'm not entirely sure if I understood it right, but if-conditions and loops are certainly also possible in Columns
.我不完全确定我是否理解正确,但 if-conditions 和循环在
Columns
中当然也是可能的。 Your example of你的例子
ListView.builder(
shrinkWrap: true,
itemCount: data.length,
itemBuilder: (context, index) {
if (condition A) {
return Text(data[index]['data']);
} else if (condition B) {
return Text(data[index]['data']);
} else {
return Container();
}
}
);
can be rewritten as a Column
like可以重写为
Column
之类的
Column(children: [
for (final d in data)
if (condition A)
Text(d['data'])
else if (condition B)
Text(d['data'])
else
Container()
]);
But I'm not sure if it helps solve your problem.但我不确定它是否有助于解决您的问题。
EDIT: For your follow-up question, maybe this is something that you want?编辑:对于您的后续问题,也许这是您想要的?
Column(children: [
for (final d in data.map((e) => e['data']))
if (conditionA)
Text(d)
else if (conditionB)
Text(d)
else
Container()
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.