英文:
flutter: force listview.builder fill all free space
问题
我尝试强制ListView.builder填充屏幕的所有可用空间(使其可滚动到屏幕的所有可用空间)。我的代码如下:
Column(
children: [
widget,
widget,
Expanded(
child: ListView.builder(
itemCount: medOrganizations.length,
padding: EdgeInsets.fromLTRB(35, 0, 35, 0),
itemBuilder: (BuildContext context, int index) {
return Container(
padding: EdgeInsets.all(15),
child: GestureDetector(
onTap: () async {
appointmentModel.clinicOid = medOrganizations[index].id!;
var medSpec = await _appointmentService.getSpecialities(appointmentModel.cityId!, appointmentModel.clinicOid!);
if (medSpec.statusCode == 200) {
medSpecialities = medSpec.result!;
stage = 2;
selectClinic = false;
selectSpecialist = true;
clinic = "${medOrganizations[index].nameFull}";
setState(() {});
}
},
child: Column(
children: [
Text(
"${medOrganizations[index].nameFull}",
style: GoogleFonts.roboto(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20.resizeFont(context),
),
),
SizedBox(height: 5),
Text(
"${medOrganizations[index].nameShort}, ${medOrganizations[index].adress}",
style: TextStyle(fontSize: 18.resizeFont(context)),
),
Divider(),
],
),
),
);
},
),
),
],
)
这个示例可以工作,但它不会填充整个屏幕。我尝试使用Flex
和Expanded
,但总是收到错误信息:"错误:RenderFlex的子项具有非零的flex,但传入的高度约束未被界定"。
英文:
i tried to force listview.builder fill all free space (make scrollable to all free space) in screen
my code :
Column(
children:[
widget,
widget,
SizedBox(
width: MediaQuery.of(context).size.width,
height:
MediaQuery.of(context).size.height,
child: ListView.builder(
itemCount: medOrganizations.length,
padding: EdgeInsets.fromLTRB(35, 0, 35, 0),
itemBuilder:
(BuildContext context, int index) {
return Container(
padding: EdgeInsets.all(15),
child: GestureDetector(
onTap: () async {
appointmentModel.clinicOid =
medOrganizations[index].id!;
var medSpec =
await _appointmentService
.getSpecialities(
appointmentModel
.cityId!,
appointmentModel
.clinicOid!);
if (medSpec.statusCode == 200) {
medSpecialities =
medSpec.result!;
stage = 2;
selectClinic = false;
selectSpecialist = true;
clinic =
"${medOrganizations[index].nameFull}";
setState(() {});
}
},
child: Column(
children: [
Text(
"${medOrganizations[index].nameFull}",
style: GoogleFonts.roboto(
color: Colors.black,
fontWeight:
FontWeight.bold,
fontSize: 20
.resizeFont(context)),
),
SizedBox(
height: 5,
),
Text(
"${medOrganizations[index].nameShort}, ${medOrganizations[index].adress}",
style: TextStyle(
fontSize: 18.resizeFont(
context))),
Divider(),
],
),
));
},
),
)])
this example, works but it doesn't fill all screen. I tried use Flex and Expanded, but always receive an error : "Error: RenderFlex children have non-zero flex but incoming height constraints are unbounded"
答案1
得分: 0
如果您的 column
沒有 scrollable
父部件,那麼您可以使用 Expanded
,它可以佔用屏幕的所有可用空間。
試試這段代碼:
Column(
children: [
widget,
widget,
Expanded( // <--------------------- 移除 Sized Box,用 Expanded 包裹
child: ListView.builder(
itemCount: medOrganizations.length,
padding: EdgeInsets.fromLTRB(35, 0, 35, 0),
itemBuilder: (BuildContext context, int index) {
return Container(
padding: EdgeInsets.all(15),
child: GestureDetector(
onTap: () async {
appointmentModel.clinicOid = medOrganizations[index].id!;
var medSpec = await _appointmentService.getSpecialities(
appointmentModel.cityId!,
appointmentModel.clinicOid!,
);
if (medSpec.statusCode == 200) {
medSpecialities = medSpec.result!;
stage = 2;
selectClinic = false;
selectSpecialist = true;
clinic = "${medOrganizations[index].nameFull}";
setState(() {});
}
},
child: Column(
children: [
Text(
"${medOrganizations[index].nameFull}",
style: GoogleFonts.roboto(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20.resizeFont(context),
),
),
SizedBox(
height: 5,
),
Text(
"${medOrganizations[index].nameShort}, ${medOrganizations[index].adress}",
style: TextStyle(
fontSize: 18.resizeFont(context),
),
),
Divider(),
],
),
),
);
},
),
),
],
)
英文:
If your column
has not scrollable
parent widget. Then you can use Expanded
. Which can take all free space of the screen.
Try this code:
Column(
children:[
widget,
widget,
Expanded( <--------------------- remove sized box and wrap with expanded
child: ListView.builder(
itemCount: medOrganizations.length,
padding: EdgeInsets.fromLTRB(35, 0, 35, 0),
itemBuilder:
(BuildContext context, int index) {
return Container(
padding: EdgeInsets.all(15),
child: GestureDetector(
onTap: () async {
appointmentModel.clinicOid =
medOrganizations[index].id!;
var medSpec =
await _appointmentService
.getSpecialities(
appointmentModel
.cityId!,
appointmentModel
.clinicOid!);
if (medSpec.statusCode == 200) {
medSpecialities =
medSpec.result!;
stage = 2;
selectClinic = false;
selectSpecialist = true;
clinic =
"${medOrganizations[index].nameFull}";
setState(() {});
}
},
child: Column(
children: [
Text(
"${medOrganizations[index].nameFull}",
style: GoogleFonts.roboto(
color: Colors.black,
fontWeight:
FontWeight.bold,
fontSize: 20
.resizeFont(context)),
),
SizedBox(
height: 5,
),
Text(
"${medOrganizations[index].nameShort}, ${medOrganizations[index].adress}",
style: TextStyle(
fontSize: 18.resizeFont(
context))),
Divider(),
],
),
));
},
),
)])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论