[英]Flutter - rendering problem with ListView.builder
I am encountering a "RenderBox was not laid out" error with a custom Flutter widget that includes a 'ListView.builder' element, and I can't see what the problem is or how to fix it.我在使用包含“ListView.builder”元素的自定义 Flutter 小部件时遇到“RenderBox 未布局”错误,我看不出问题出在哪里或如何解决。
See the bottom section of the sample code shown below.请参阅下面显示的示例代码的底部。 If I comment out the 'ListView.builder' code block and replace it with the 'Text('Has members') then I don't get the error, hence it must be something to do with the ListView.builder.
如果我注释掉 'ListView.builder' 代码块并将其替换为 'Text('Has members') 那么我不会收到错误,因此它一定与 ListView.builder 有关。
Any suggestions?有什么建议么?
class GroupMembershipSelector extends FormField<List<GroupMember>> {
final BuildContext? parentContext;
GroupMembershipSelector(
{this.parentContext,
FormFieldSetter<List<GroupMember>>? onSaved,
FormFieldValidator<List<GroupMember>>? validator,
List<GroupMember>? initialValue,
AutovalidateMode autoValidate = AutovalidateMode.disabled})
: super(
onSaved: onSaved,
validator: validator,
initialValue: initialValue,
autovalidateMode: autoValidate,
builder: (FormFieldState<List<GroupMember>> state) {
return Container(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextButton.icon(
label: Text('Add to group'),
icon: Icon(Icons.add),
onPressed: () {
showModalGroupMemberForm(parentContext!, null)
.then((aNewMember) {
if (aNewMember != null) {
if (state.value == null) {
List<GroupMember> aList = [];
aList.add(aNewMember);
state.didChange(aList);
} else {
List<GroupMember> anExisting = state.value!;
anExisting.add(aNewMember);
state.didChange(anExisting);
}
}
});
},
),
Flexible(
child: ( (state.value == null) || (state.value!.length == 0))
? Text('No members')
// : Text('Has members'),
: ListView.builder(
itemCount: state.value!.length,
itemBuilder: (context, index) {
return Flexible(
child: ListTile(
title: Text(state.value![index].email)));
}),
),
],
),
);
}) {}
}
Remove Flexible
widget from ListTile
.从
ListTile
中删除Flexible
的小部件。 It will be这将是
Flexible(
child: ListView.builder(
itemBuilder: (context, index) {
return ListTile();
},
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.