I have a widget which is built recursively and i need to find its height for another widget in the same row containing the recursively called widget.
I tried getting the height of the parent using LayoutBuilder but it doesnt work because of the expanded widget.
I have tried to use this method to find the height using key before the widget is build but it doesnt work for the recursive case.
CODE
class MyWidget extends StatefulWidget {
const MyWidget({
Key key,
}) : super(key: key);
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
TextEditingController _textEditingController;
GlobalKey _keyRed = GlobalKey();
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
_textEditingController = TextEditingController();
super.initState();
}
_afterLayout(_) {
_getSizes();
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
_getSizes() {
final RenderBox renderBoxRed = _keyRed.currentContext.findRenderObject();
final sizeRed = renderBoxRed.size;
print("SIZE of Red: $sizeRed");
return sizeRed.height;
}
@override
Widget build(BuildContext context) {
// condition for recursion to stop avoided here for brevity
return Container(
child: Row(
children: [
getLeadingWidget(),
Expanded(
key: _keyRed,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
getTextField(note),
Column(
children:
getChildren()
)
],
),
),
],
),
);
}
getLeadingWidget() {
return LayoutBuilder(
builder: (context, constraints) => Container(
height: constraints.constrainHeight(),
child: Container(...)
));
}
getTextField(NotesModel note) {
return TextFormField(
keyboardType: TextInputType.multiline,
maxLines: null,
controller: _textEditingController..text = 'TEST',
);
}
getChildrenNotes(int noteId) {
List<Widget> childrenNotes = [];
for (...) {
// widget called recursively
childrenNotes.add(MyWidget());
}
return childrenNotes;
}
}
Diagram with relevant widgets:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…