1

I have made a list of widgets

List<Widget> my_list = [Text("Hello World"),Text("Hello Flutter")];

And I want to make a Widget which takes argument as list of widgets only in its constructor, and I have to do something using this widgets in which I need the size of each widget. Below is my Widget

class MyCustomWidget extends StatefulWidget{
List<Widget> widget_list;
MyCustomWidget(this.widget_list);
@override
_MyCustomWidgetState createState() => _MyCustomWidgetState();
}

class _MyCustomWidgetState extends State<MyCustomWidget>{
List<double> widget_height; 
@override
  Widget build(BuildContext context) {
  return null;
  }
}

In this list(widget_height) I have to save the height of each widget that is in the list(widget_list).

How to do this. Is there any method like widget_list[0].getHeight()?

Edit: I can't use global key. Refer comment section for more detail. And I need all this data before rendering the object. So maybe we can override initState() method to get all this data betfore rendering.

Maybe I an not correct in the last part about rendering.

Abhishek Patil
  • 502
  • 1
  • 4
  • 12
  • Does this answer your question? [How to get a height of a Widget?](https://stackoverflow.com/questions/49307677/how-to-get-a-height-of-a-widget) – tomerpacific Dec 01 '19 at 10:31
  • let me clear some more things, I am designing a flutter package which return a widget by taking a list of widget as argument in constructor. So the widget in _list of widget_ should not be restricted to give a global key to it. It just have to give a normal widget. All other things should be handled by *MyCustomWidget* . So I can't use a global key for widgets. – Abhishek Patil Dec 01 '19 at 10:35
  • I only have a widget instance and I dont know anything about it. I have to extract it's height using that instance. – Abhishek Patil Dec 01 '19 at 10:36
  • This is not possible you cannot obtain the size of a widget as widgets don't have a size. Instead you need to obtain the Renderobject associated to the widget, then obtain its size. – Rémi Rousselet Dec 01 '19 at 16:01
  • You may as well write a Renderobject yourself. – Rémi Rousselet Dec 01 '19 at 16:02
  • If you explain more about the end goal maybe we will be able to give you help. Your request is a bit vague and honestly sounds counter to the "Flutter way" of doing things. If you explain the end goal in a question and give good detail then a suggestion of how get to the end can be made. – Kent Dec 01 '19 at 23:31
  • I wanted to make a dart package which takes input as a list of widgets, and the goal of the package is to return two variables named *index* and *progress*. the _index_ will show which widget is in the middle of the screen and _progress_ will show how much percentage of this widget is above the middle line. So the developer can make animation in the background or anything he wants to do using _index_ and _progress_ variable. The main aim is to animate the background according to 'which widget is on the screen'(_index_) and 'how much porting is above the middle line'(_progress_) – Abhishek Patil Dec 02 '19 at 07:39

0 Answers0