What if you could place buttons and images perfectly on any screen without endless trial and error?
Why Stack and Positioned in Flutter? - Purpose & Use Cases
Imagine you want to place a button exactly at the bottom right corner of your app screen, but also want an image behind it that fills the whole screen. Doing this by manually calculating positions for each element on every device size is like trying to fit puzzle pieces without seeing the picture.
Manually positioning UI elements by guessing coordinates is slow and frustrating. It often breaks on different screen sizes or orientations. You might spend hours adjusting numbers, only to find the layout looks wrong on another phone. This approach is error-prone and wastes time.
The Stack widget lets you place widgets on top of each other like layers. Using Positioned, you can easily fix a widget's position inside the stack without guessing numbers for the whole screen. This makes your layout flexible and responsive.
Container(width: 300, height: 600, child: Column(children: [Image(...), SizedBox(height: 550), Button()]))
Stack(children: [Image(...), Positioned(bottom: 20, right: 20, child: Button())])
You can create complex, layered layouts that adapt smoothly to different screen sizes without messy calculations.
Think of a chat app where you want the message list to fill the screen and a send button floating at the bottom right corner. Using Stack and Positioned makes this easy and reliable.
Stack lets you layer widgets on top of each other.
Positioned places widgets exactly where you want inside a Stack.
This approach saves time and works well on all screen sizes.
Practice
Stack widget do in Flutter?Solution
Step 1: Understand Stack widget purpose
The Stack widget allows placing widgets on top of each other in layers.Step 2: Compare with other layout widgets
Other widgets like Column or Row arrange widgets vertically or horizontally, not layered.Final Answer:
It layers widgets on top of each other. -> Option AQuick Check:
Stack = layering widgets [OK]
- Confusing Stack with Column or Row
- Thinking Stack scrolls content
- Assuming Stack arranges widgets side by side
Solution
Step 1: Check parameter order and values
Positioned requires named parameters; order doesn't matter but values must match position.Step 2: Verify correct top and left values
Positioned(top: 10, left: 20, child: Text('Hello'))correctly sets top: 10 and left: 20 with child widget.Final Answer:
Positioned(top: 10, left: 20, child: Text('Hello')) -> Option BQuick Check:
top=10 and left=20 in Positioned [OK]
- Swapping top and left values
- Placing child before position parameters
- Using bottom and right instead of top and left
Stack(
children: [
Container(width: 100, height: 100, color: Colors.red),
Positioned(top: 20, left: 20, child: Container(width: 50, height: 50, color: Colors.blue))
]
)Solution
Step 1: Understand Stack layering
The red container is the base layer sized 100x100 pixels.Step 2: Analyze Positioned child
The blue container is 50x50 pixels placed 20 pixels from top and left, overlapping the red.Final Answer:
A red square with a smaller blue square overlapping near the top-left corner. -> Option CQuick Check:
Stack layers with Positioned overlap [OK]
- Thinking widgets appear side by side
- Assuming blue is behind red
- Ignoring Positioned offsets
Stack(
children: [
Positioned(top: 10, child: Text('Hello')),
Positioned(left: 20, child: Text('World'))
]
)Solution
Step 1: Review Positioned requirements
Positioned widgets can use zero, one, or more positioning properties (top, left, bottom, right). Partial constraints are allowed (unspecified directions default to 0).Step 2: Check given Positioned widgets
The first uses top: 10 (Hello positioned 10px from top, left=0). The second uses left: 20 (World positioned 20px from left, top=0). No errors; valid code.Final Answer:
Positioned widgets can have only one property; this code is correct. -> Option DQuick Check:
Positioned allows partial properties [OK]
- Thinking Positioned requires at least two properties
- Believing partial positioning is invalid
- Confusing Positioned requirements
Solution
Step 1: Understand centering in Stack
Using Positioned.fill with Align centers the child relative to the Stack size.Step 2: Analyze each option
Stack( children: [ Container(width: 200, height: 200, color: Colors.yellow), Positioned.fill(child: Align(alignment: Alignment.center, child: Container(width: 50, height: 50, decoration: BoxDecoration(color: Colors.green, shape: BoxShape.circle)))) ] )uses Positioned.fill and Align with Alignment.center, ensuring the green circle stays centered regardless of screen size.Final Answer:
Stack with Positioned.fill and Align to center the circle. -> Option AQuick Check:
Use Positioned.fill + Align for responsive centering [OK]
- Hardcoding top and left offsets (Option A)
- Missing Positioned for precise control (Option B)
- Using bottom and right offsets incorrectly (Option D)
Ready to prove it?
Pass the test and this pattern moves unknown
