Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

flutter - How to get the height of widget built recursively?

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: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...