Audio Message Player
Loading preview
Starting the Flutter app
Chat-style audio message bubbles with play/pause and seek functionality
import 'package:flutter/material.dart';
class AudioMessagePlayer extends StatefulWidget {
const AudioMessagePlayer({super.key});
@override
State<AudioMessagePlayer> createState() => _AudioMessagePlayerState();
}
class _AudioMessagePlayerState extends State<AudioMessagePlayer> {
// Only one message plays at a time.
int? _playingIndex;
// One playback position per message, indexed like _messages.
final List<double> _progress = [0.3, 0.3, 0.3];
final List<Map<String, dynamic>> _messages = [
{
'sender': 'Sarah',
'avatar': 'S',
'audioDuration': '0:15',
'isMe': false,
'timestamp': '2:30 PM',
},
{
'sender': 'You',
'avatar': 'Y',
'audioDuration': '0:23',
'isMe': true,
'timestamp': '2:32 PM',
},
{
'sender': 'Mike',
'avatar': 'M',
'audioDuration': '0:45',
'isMe': false,
'timestamp': '2:35 PM',
},
];
@override
Widget build(BuildContext context) {
final isDark = Theme.of(context).brightness == Brightness.dark;
return Scaffold(
appBar: AppBar(
title: const Text('Audio Messages'),
backgroundColor: isDark ? Colors.grey[900] : Colors.blue[700],
foregroundColor: Colors.white,
),
body: ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: _messages.length,
itemBuilder: (context, index) {
final message = _messages[index];
return _buildAudioMessage(
context,
index: index,
sender: message['sender'],
avatar: message['avatar'],
audioDuration: message['audioDuration'],
isMe: message['isMe'],
timestamp: message['timestamp'],
isDark: isDark,
);
},
),
);
}
Widget _buildAudioMessage(
BuildContext context, {
required int index,
required String sender,
required String avatar,
required String audioDuration,
required bool isMe,
required String timestamp,
required bool isDark,
}) {
final isPlaying = _playingIndex == index;
final metaColor = isMe
? Colors.white70
: (isDark ? Colors.white54 : Colors.grey[600]);
return Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Row(
mainAxisAlignment: isMe
? MainAxisAlignment.end
: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (!isMe) ...[
CircleAvatar(
radius: 18,
backgroundColor: Colors.blue[400],
child: Text(avatar, style: const TextStyle(color: Colors.white)),
),
const SizedBox(width: 8),
],
Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.65,
),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: isMe
? (isDark ? Colors.blue[700] : Colors.blue[500])
: (isDark ? Colors.grey[800] : Colors.grey[200]),
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(20),
topRight: const Radius.circular(20),
bottomLeft: Radius.circular(isMe ? 20 : 4),
bottomRight: Radius.circular(isMe ? 4 : 20),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
sender,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: isMe
? Colors.white
: (isDark ? Colors.white : Colors.grey[800]),
),
),
const SizedBox(height: 4),
Row(
mainAxisSize: MainAxisSize.min,
children: [
GestureDetector(
onTap: () {
setState(() {
_playingIndex = isPlaying ? null : index;
});
},
child: Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: isMe
? Colors.white.withValues(alpha: 0.3)
: (isDark ? Colors.white24 : Colors.blue[100]),
shape: BoxShape.circle,
),
child: Icon(
isPlaying ? Icons.pause : Icons.play_arrow,
color: isMe ? Colors.white : Colors.blue[700],
size: 24,
),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: 4,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 6,
),
),
child: Slider(
value: _progress[index],
onChanged: (value) {
setState(() => _progress[index] = value);
},
activeColor: isMe
? Colors.white
: Colors.blue[700],
inactiveColor: isMe
? Colors.white30
: Colors.blue[200],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
audioDuration,
style: TextStyle(
fontSize: 11,
color: metaColor,
),
),
Text(
timestamp,
style: TextStyle(
fontSize: 11,
color: metaColor,
),
),
],
),
],
),
),
],
),
],
),
),
if (isMe) ...[
const SizedBox(width: 8),
CircleAvatar(
radius: 18,
backgroundColor: Colors.purple[400],
child: Text(avatar, style: const TextStyle(color: Colors.white)),
),
],
],
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
No audio plays. Each bubble has its own position in _progress, and _playingIndex tracks the one playing message, so starting another pauses the first. Nothing advances _progress on its own, though each Slider moves its own bubble. _buildAudioMessage shows the sender above the player, with the duration and timestamp below the slider.
Adapting it
- Drive
_progressfrom a player such as just_audio, and seek from theSlider’sonChanged. - Turn the
_messagesmaps into a typed model with a URL, and readaudioDurationfrom the file.