Ever had your Flutter screen layout shift or break when your text changes length?
The Problem
In my music player screen, long song titles caused the UI to push down — shifting the seek bar & controls, or worse, throwing an overflow error.
Here’s how the screen was built without LayoutBuilder:
// Problematic layout
return Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
const SizedBox(height: 80),
AnimatedBeatCircle(size: 300, isPlaying: true),
// Title (may be 1 or 2 lines)
Text(
mediaItem.title,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 10),
// Seek bar + playback controls follow
...
],
),
);
Issue:
If the title is short → empty space.
If it’s long → seek bar/control buttons shift or overflow.
UI feels unstable between different songs.
The Fix: Use LayoutBuilder + Expanded
By wrapping the layout in a LayoutBuilder and explicitly splitting the screen into:
Top (flexible): Beat circle, title, artist
Bottom (fixed): Seek bar, playback controls
We pin the bottom UI and allow the top to adapt safely.
// Fixed layout
return LayoutBuilder(
builder: (context, constraints) {
return Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
AnimatedBeatCircle(size: 300, isPlaying: true),
const SizedBox(height: 20),
Text(
mediaItem.title,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
Text(mediaItem.artist ?? ''),
],
),
),
),
// Bottom stays pinned no matter what
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Slider(...),
Row(...), // duration labels
Row(...), // controls
],
),
),
],
);
},
);
Result:
- Layout is predictable
- Playback controls stay pinned
- UI looks great no matter how long the song title is!