Stack in Flutter

18-Sep-2024

Learn how to position widgets on top of each other using the Stack widget

Stack widget is used to overlay multiple widgets on top of each other.
Widgets added to a Stack can be positioned relative to the edges of the Stack or relative to each other

There Are Some Common Attributes for Stack

alignment
Alignment of the children in the stack
Positioned
Positioned widget or using other positioning widgets like


Here's a simple example of using a Stack in Flutter:

File open lib->main.dart file −



import 'package:flutter/material.dart';

void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Stack Example'),
),
body: MyApp(),
),
)
);
}

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: [
Container(
height: 200,
width: 200,
color: Colors.blue,
alignment: Alignment.center,
),
const Positioned(
top: 50,
left: 50,
child: Text("Micro ",
style: TextStyle(
fontSize: 15,
color: Colors.amberAccent
),
)
),

const Positioned(
top: 30,
right: 50,
child: Text("Tutorial",
style: TextStyle(
fontSize: 15,
color: Colors.white
)
)
)
],
);

}
}




Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments