The simplest and most reliable layout is a Row, which already aligns children horizontally, and you can control the vertical centering with crossAxisAlignment. Don’t use Stack unless you must.
✅ Recommended Layout
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center, // ensures vertical centering
children: [
Expanded(
child: YourMainWidget(),
),
GestureDetector(
onTap: () {
print('Icon tapped!');
},
child: Icon(Icons.more_vert, size: 24),
),
],
)
This ensures:
- The icon stays at the end
- Both widgets get their click events
- Everything stays vertically centered
If you MUST use a Stack (for overlay UI)
You can do:
Stack(
children: [
YourMainWidget(),
Positioned(
right: 0,
top: 0,
bottom: 0, // keeps the icon vertically centered
child: GestureDetector(
onTap: () => print('Icon tapped'),
child: Center(
child: Icon(Icons.more_vert),
),
),
),
],
)
This works because setting top:0 and bottom:0 forces the child to match the Stack height, so Center inside keeps the icon vertically centered.
