Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter cast RenderObject to RenderBox

I am trying to follow this tutorial creating a DropDown. But I can not copy his code because Flutter 2.0 is forbidding it because I can of these lines:

  void findDropdownData() {
    RenderBox renderBox = actionKey.currentContext!.findRenderObject()!;
    height = renderBox.size.height;
    width = renderBox.size.width;
    Offset? offset = renderBox.localToGlobal(Offset.zero);
    xPosition = offset!.dx;
    yPosition = offset.dy;
  }

As you can see I tried do add some ! and ? but it is still not working. The main issue is that findRenderObject returns RenderObject but I need it to be a RenderBox... Any idea what's wrong here? Can not figure it out..

like image 475
Chris Avatar asked Dec 17 '22 11:12

Chris


2 Answers

The solution was easier than I thought:

simply use as like this:

RenderBox renderBox =
    actionKey.currentContext!.findRenderObject()! as RenderBox;
like image 124
Chris Avatar answered Feb 01 '23 07:02

Chris


Sometimes actionkey may shows error, so simply use as

RenderBox renderBox = context.findRenderObject()! as RenderBox;
like image 40
19TUCS138 NITHINRAAJ J Avatar answered Feb 01 '23 06:02

19TUCS138 NITHINRAAJ J