Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stack widgets overlapping each other in flutter

Tags:

flutter

dart

I need to stack widgets like this:

coins

I wrote the code below. However the coins are coming one after another with some default padding. How can I get something like the image above?

Row(   children: <Widget>[     Icon(       Icons.monetization_on, size: 36.0,        color: const Color.fromRGBO(218, 165, 32, 1.0),     ),     Icon(       Icons.monetization_on, size: 36.0,       color: const Color.fromRGBO(218, 165, 32, 1.0),     ),   ], ), 
like image 321
user983327 Avatar asked Apr 15 '18 02:04

user983327


People also ask

Which Flutter layout type can be used to overlay multiple widgets on top of each other?

A Stack widget allows us to make multiple widgets overlay each other.

How do you implement stacks in Flutter?

In the first Container, the text is 'One', and the alignment is set to Alignment. topRight, which puts the Text widget in the top-right corner. In the second Container, the text is 'Two', and the alignment is set to Alignment. bottom left, which put the child which is the Text in the bottom-left corner.


1 Answers

You can use a Stack with Positioned to achieve this:

enter image description here

class StackExample extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new Scaffold(       appBar: new AppBar(),       body:  new Container(         padding: const EdgeInsets.all(8.0),           height: 500.0,           width: 500.0,           // alignment: FractionalOffset.center,           child: new Stack(             //alignment:new Alignment(x, y)             children: <Widget>[               new Icon(Icons.monetization_on, size: 36.0, color: const Color.fromRGBO(218, 165, 32, 1.0)),               new Positioned(                 left: 20.0,                 child: new Icon(Icons.monetization_on, size: 36.0, color: const Color.fromRGBO(218, 165, 32, 1.0)),               ),               new Positioned(                 left:40.0,                 child: new Icon(Icons.monetization_on, size: 36.0, color: const Color.fromRGBO(218, 165, 32, 1.0)),               )              ],           ),         ),       )     ;   } } 

And this how you get some nice shadow drop so the icon stands out more:

enter image description here

class StackExample extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new Scaffold(       appBar: new AppBar(),       body:  new Container(         padding: const EdgeInsets.all(8.0),           height: 500.0,           width: 500.0,           // alignment: FractionalOffset.center,           child: new Stack(             //alignment:new Alignment(x, y)             children: <Widget>[               new Container(                 decoration: new BoxDecoration(                   borderRadius: BorderRadius.circular(25.0),                   boxShadow: [                     new BoxShadow(                       blurRadius: 5.0,                       offset: const Offset(3.0, 0.0),                       color: Colors.grey,                     )                   ]                 ),                 child: new Icon(Icons.monetization_on, size: 36.0, color: const Color.fromRGBO(218, 165, 32, 1.0))),               new Positioned(                 left: 20.0,                 child: new Container(                   decoration: new BoxDecoration(                   borderRadius: BorderRadius.circular(25.0),                   boxShadow: [                     new BoxShadow(                       blurRadius: 5.0,                       offset: const Offset(3.0, 0.0),                       color: Colors.grey,                     )                   ]                 ),                   child: new Icon(Icons.monetization_on, size: 36.0, color: const Color.fromRGBO(218, 165, 32, 1.0))),               ),               new Positioned(                 left:40.0,                 child: new Container(                   decoration: new BoxDecoration(                   borderRadius: BorderRadius.circular(25.0),                   boxShadow: [                     new BoxShadow(                       blurRadius: 5.0,                       offset: const Offset(3.0, 0.0),                       color: Colors.grey,                     )                   ]                 )                   ,child: new Icon(Icons.monetization_on, size: 36.0, color: const Color.fromRGBO(218, 165, 32, 1.0))),               )              ],           ),         ),       )     ;   } } 
like image 118
Shady Aziza Avatar answered Oct 03 '22 05:10

Shady Aziza