Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Stack size to sibling

Tags:

flutter

Is there a way to size a stack child automatically to its largest sibling? I.e. if I have a Stack with a ListTile and a Container on top, how do I make sure the Container covers the entire ListTile?

Example:

new Stack(children: <Widget>[       new ListTile(           leading: new AssetImage('foo.jpg'),           title: new Text('Bar'),           subtitle: new Text('yipeee'),           isThreeLine: true,       ),       new Container(color: Colors.grey, child: new Text('foo'))  ], ) 

I tried to make it work with Row, Column and Expanded but am running into problems with unbounded constraints.

Is there a way to size the Container (or any other widget such as a GestureDetector) to its largest sibling in the stack?

like image 870
TommyF Avatar asked Jul 01 '18 13:07

TommyF


People also ask

What is Stack fit in flutter?

StackFit fit. How to size the non-positioned children in the stack. The constraints passed into the Stack from its parent are either loosened (StackFit. loose) or tightened to their biggest size (StackFit. expand).

Can a container have multiple child in flutter?

The container in Flutter is a parent widget that can contain multiple child widgets and manage them efficiently through width, height, padding, background color, etc. It is a widget that combines common painting, positioning, and sizing of the child widgets.

What is stack overflow in flutter?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.


1 Answers

I had the same issue and finally managed to solve it using this answer:

Inside your Stack, you should wrap your background widget in a Positioned.fill.

return new Stack(   children: <Widget>[     new Positioned.fill(       child: background,     ),     foreground,   ], ); 

-- Mary, https://stackoverflow.com/a/45745479

Applying that to your question results in the following:

Stack(   children: <Widget>[     ListTile(       leading: AssetImage('foo.jpg'),       title: Text('Bar'),       subtitle: Text('yipeee'),       isThreeLine: true,     ),     Positioned.fill(       child: Container(color: Colors.grey, child: Text('foo')),     ),   ], ), 
like image 137
nesbocaj Avatar answered Sep 21 '22 20:09

nesbocaj