踩坑报错如下:
The following assertion was thrown during performLayout():
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
This probably means that it is a render object that tries to be as big as possible, but it was put inside another render object that allows its children to pick their own size.
The nearest ancestor providing an unbounded height constraint is: RenderFlex#6fa90 relayoutBoundary=up1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
... needs compositing
... parentData: offset=Offset(0.0, 100.0); id=_ScaffoldSlot.body (can use size)
... constraints: BoxConstraints(0.0<=w<=414.0, 0.0<=h<=796.0)
... size: Size(414.0, 796.0)
... direction: vertical
... mainAxisAlignment: start
... mainAxisSize: max
... crossAxisAlignment: center
... verticalDirection: down
The constraints that applied to the RenderCustomMultiChildLayoutBox were: BoxConstraints(0.0<=w<=414.0, 0.0<=h<=Infinity)
The exact size it was given was: Size(414.0, Infinity)
See https://flutter.dev/docs/development/ui/layout/box-constraints for more information.
The relevant error-causing widget was:
Scaffold file:///Users/hello/Desktop/xzlcorp/AndroidStudioProjects/xzl-patient/lib/page/medic/medic_scan/medic_add_view.dart:109:16
When the exception was thrown, this was the stack:
#0 RenderBox.debugAssertDoesMeetConstraints.<anonymous closure> (package:flutter/src/rendering/box.dart:1967:9)
#1 RenderBox.debugAssertDoesMeetConstraints (package:flutter/src/rendering/box.dart:2035:6)
#2 RenderBox.size=.<anonymous closure> (package:flutter/src/rendering/box.dart:1752:7)
#3 RenderBox.size= (package:flutter/src/rendering/box.dart:1754:6)
#4 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:398:5)
布局如下
Widget build(BuildContext context) {
List<Suggestions> suggestions =
provider.getAllMedicSuggestions(medicNameTypeBean);
return Scaffold(
appBar: _titleAppBar(),
body: Column(
children: <Widget>[
_backMedicScanView(),
_medicSearchInputItem(),
_medicSelectNameType(suggestions)
],
),
);
}
定位到Column中第三个child报错,第三个child布局如下
Widget _medicSelectNameType(List<Suggestions> suggestions) {
return Container(
child: DefaultTabController(
length: suggestions.length,
initialIndex: 1,
child: Scaffold(
body: Column(
children: <Widget>[
TabBar(
indicatorSize: TabBarIndicatorSize.label,
unselectedLabelColor: AppColors.color_2c2f32,
labelColor: AppColors.color_3592de,
indicatorColor: AppColors.color_3592de,
labelStyle: TextStyle(height: 2),
tabs: _medicTabTitleList(suggestions)),
Expanded(
flex: 1,
child: Container(
width: double.infinity,
child: TabBarView(
children: suggestions.map((element) {
return _medicSelectDisplayView(element);
}).toList()),
))
],
)),
),
);
}
解决办法:把第三个child包在一个Expanded布局里,然后设置flex:1,将Column展开,就解决了这个问题。
如下:
Widget build(BuildContext context) {
List<Suggestions> suggestions =
provider.getAllMedicSuggestions(medicNameTypeBean);
return Scaffold(
appBar: _titleAppBar(),
body: Column(
children: <Widget>[
_backMedicScanView(),
_medicSearchInputItem(),
Expanded(
flex: 1,
child: _medicSelectNameType(suggestions),
)
],
),
);
}