Flutter Stack

 

Flutter Stack

The stack is a widget in Flutter that contains a list of widgets and positions them on top of the other. In other words, the stack allows developers to overlap multiple widgets into a single screen and renders them from bottom to top. Hence, the first widget is the bottommost item, and the last widget is the topmost item

Key Points Related to Stack Widget

The following are the key points of the Flutter stack widget:

  • The child widget in a stack can be either positioned or non-positioned.
  • Positioned items are wrapped in Positioned widget and must have a one non-null property
  • The non-positioned child widgets are aligned itself. It displays on the screen based on the stack's alignment. The default position of the children is in the top left corner.
  • We can use the alignment attribute to change the alignment of the widgets.
  • Stack places the children widgets in order with the first child being at the bottom and the last child being at the top. If we want to reorder the children's widget, it is required to rebuild the stack in the new order. By default, the first widget of each stack has the maximum size compared to other widgets.

How to use a stack widget in Flutter?

The below example helps to understand the use of stack widget quickly that contains three containers of shrinking size:

  1. Stack(  
  2.   children: <Widget>[  
  3.     // Max Size  
  4.     Container(  
  5.       color: Colors.green,  
  6.     ),  
  7.     Container(  
  8.       color: Colors.blue,  
  9.     ),  
  10.     Container(  
  11.       color: Colors.yellow,  
  12.     )  
  13.   ],  
  14. ),  

It will give the following output:

Flutter Stack

Properties of the Stack Widget

The following are the properties used with the stack widget:

alignment: It determines how the children widgets are positioned in the stack. It can be top, bottom, center, center-right, etc.

  1. Stack(  
  2.   alignment: Alignment.topCenter, // Center of Top  
  3.   children: <Widget>[ ]  
  4. )  

textDirection: It determines the text direction. It can draw the text either ltr (left to right) or rtl (right to the left).

  1. Stack(  
  2.   textDirection: TextDirection.rtl, // Right to Left  
  3.   children: <Widget>[ ]  
  4. )  

fit: It will control the size of non-positioned children widgets in the stack. It has three types: loose, expand and passthrough. The loose used to set the child widget small, the expand attribute makes the child widget as large as possible, and the passthrough set the child widget depending on its parent widget.

  1. Stack(  
  2.   fit: StackFit.passthrough,  
  3.   children: <Widget>[ ]  
  4. )  

overflow: It controls the children widgets, whether visible or clipped, when it's content overflowing outside the stack.

  1. Stack(  
  2.   overflow: Overflow.clip, // Clip the Content  
  3.   children: <Widget>[ ]  
  4. )  

clipBehavior: It determines whether the content will be clipped or not.

Positioned

It is not the stack parameter but can be used in the stack to locate the children widgets. The following are the constructor of the positioned stack:

  1. const Positioned({  
  2. Key key,  
  3. this.left,  
  4. this.top,  
  5. this.right,  
  6. this.bottom,  
  7. this.width,  
  8. this.height,  
  9. @required Widget child,  
  10. })   

Stack Widget Example

The below code explains how to use the stack widget in Flutter. In this code, we are going to try most of the essential attributes of the stack widget.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. /// This Widget is the main application widget.  
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       home: MyStackWidget(),  
  11.     );  
  12.   }  
  13. }  
  14.   
  15. class MyStackWidget extends StatelessWidget {  
  16.   @override  
  17.   Widget build(BuildContext context) {  
  18.     return MaterialApp(  
  19.       home: Scaffold(  
  20.         appBar: AppBar(  
  21.           title: Text("Flutter Stack Widget Example"),  
  22.         ),  
  23.         body: Center(  
  24.           child: Stack(  
  25.             fit: StackFit.passthrough,  
  26.             overflow: Overflow.visible,  
  27.             children: <Widget>[  
  28.               // Max Size Widget  
  29.               Container(  
  30.                 height: 300,  
  31.                 width: 400,  
  32.                 color: Colors.green,  
  33.                 child: Center(  
  34.                   child: Text(  
  35.                     'Top Widget: Green',  
  36.                     style: TextStyle(color: Colors.white, fontSize: 20),  
  37.                   ),  
  38.                 ),  
  39.               ),  
  40.               Positioned(  
  41.                 top: 30,  
  42.                 right: 20,  
  43.                 child: Container(  
  44.                   height: 100,  
  45.                   width: 150,  
  46.                   color: Colors.blue,  
  47.                   child: Center(  
  48.                     child: Text(  
  49.                       'Middle Widget',  
  50.                       style: TextStyle(color: Colors.white, fontSize: 20),  
  51.                     ),  
  52.                   ),  
  53.                 ),  
  54.               ),  
  55.               Positioned(  
  56.                 top: 30,  
  57.                 left: 20,  
  58.                 child: Container(  
  59.                   height: 100,  
  60.                   width: 150,  
  61.                   color: Colors.orange,  
  62.                   child: Center(  
  63.                     child: Text(  
  64.                       'Bottom Widget',  
  65.                       style: TextStyle(color: Colors.white, fontSize: 20),  
  66.                     ),  
  67.                   ),  
  68.                 )  
  69.               ),  
  70.             ],  
  71.           ),  
  72.         )  
  73.       ),  
  74.     );  
  75.   }  
  76. }  

Output:

When we run the app, we should get the UI of the screen similar to the below screenshot:

Flutter Stack

Flutter IndexedStack

It is another stack widget in Flutter that displayed only one element at one time by specifying its index. See the below code snippet:

  1. IndexedStack(  
  2.   index: 1,  
  3.   children: <Widget>[  
  4.     Container(  
  5.       color: Colors.green,  
  6.     ),  
  7.     Container(  
  8.       color: Colors.blue,  
  9.     ),  
  10.     Container(  
  11.       color: Colors.yellow,  
  12.     )  
  13.   ],  
  14. )  

IndexedStack takes children like a usual stack, but it will display only one child at a time. Therefore, it is not a stack. We use it for easily switching between one child to another child according to our needs.

IndexedStack Widget Example

The below code explains how to use indexed stack widget in Flutter:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. /// This Widget is the main application widget.  
  6. class MyApp extends StatelessWidget {  
  7.   @override  
  8.   Widget build(BuildContext context) {  
  9.     return MaterialApp(  
  10.       home: MyStackWidget(),  
  11.     );  
  12.   }  
  13. }  
  14.   
  15. class MyStackWidget extends StatelessWidget {  
  16.   @override  
  17.   Widget build(BuildContext context) {  
  18.     return MaterialApp(  
  19.       home: Scaffold(  
  20.         appBar: AppBar(  
  21.           title: Text("Flutter Stack Widget Example"),  
  22.         ),  
  23.         body: Center(  
  24.           child: IndexedStack(  
  25.             index: 0,  
  26.             children: <Widget>[  
  27.               Container(  
  28.                 height: 300,  
  29.                 width: 400,  
  30.                 color: Colors.green,  
  31.                 child: Center(  
  32.                   child: Text(  
  33.                     'First Widget',  
  34.                     style: TextStyle(color: Colors.white, fontSize: 20),  
  35.                   ),  
  36.                 ),  
  37.               ),  
  38.               Container(  
  39.                 height: 250,  
  40.                 width: 250,  
  41.                 color: Colors.blue,  
  42.                 child: Center(  
  43.                   child: Text(  
  44.                     'Second Widget',  
  45.                     style: TextStyle(color: Colors.white, fontSize: 20),  
  46.                   ),  
  47.                 ),  
  48.               ),  
  49.             ],  
  50.           ),  
  51.         )  
  52.       ),  
  53.     );  
  54.   }  
  55. }  

Output:

When we run the app, we should get the UI of the screen similar to the below screenshot:

Flutter Stack

Is it possible to wrap stack inside stack in Flutter?

Yes, it is possible to wrap stack inside stack in Flutter. We can do this by wrapping the second stack inside the container with height and width property.

See the below code to understand it more clearly:

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() => runApp(MyApp());  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       home: MyStackWidget(),  
  10.     );  
  11.   }  
  12. }  
  13.   
  14. class MyStackWidget extends StatelessWidget {  
  15.   @override  
  16.   Widget build(BuildContext context) {  
  17.     return MaterialApp(  
  18.       home: Scaffold(  
  19.         appBar: AppBar(  
  20.           title: Text("Flutter Stack Widget Example"),  
  21.         ),  
  22.         body: Center(  
  23.           child: Stack(  
  24.             children: [  
  25.               Positioned(  
  26.                 top: 100,  
  27.                 child: Text(  
  28.                     "Stack#1",  
  29.                     style: TextStyle(color: Colors.black, fontSize: 20)  
  30.                 ),  
  31.               ),  
  32.               Positioned(  
  33.                 top: 150.0,  
  34.                 child: Container(  
  35.                   height: 220,  
  36.                   width: 220,  
  37.                   color: Colors.green,  
  38.                   child: Stack(  
  39.                     children: [  
  40.                       Positioned(  
  41.                         top:160,  
  42.                         child: Text(  
  43.                           "Stack Inside Stack#1",  
  44.                           style: TextStyle(color: Colors.white, fontSize: 20)  
  45.                         ),  
  46.                       )  
  47.                     ],  
  48.                   ),  
  49.                 ),  
  50.               )  
  51.             ],  
  52.           ),  
  53.         )  
  54.       ),  
  55.     );  
  56.   }  
  57. }  

When we run the app, we should get the UI of the screen similar to the below screenshot:

Flutter Stack


Post a Comment

Previous Post Next Post