Video Message Player
Loading preview
Starting the Flutter app
Video message bubbles with inline player and full-screen modal
import 'package:flutter/material.dart';
class VideoMessagePlayer extends StatefulWidget {
const VideoMessagePlayer({super.key});
@override
State<VideoMessagePlayer> createState() => _VideoMessagePlayerState();
}
class _VideoMessagePlayerState extends State<VideoMessagePlayer> {
// One playback position per message, indexed like _messages.
final List<double> _progress = [0.25, 0.25];
final List<Map<String, dynamic>> _messages = [
{
'sender': 'Sarah',
'avatar': 'S',
'thumbnail': 'Video',
'duration': '0:15',
'isMe': false,
'timestamp': '2:30 PM',
},
{
'sender': 'You',
'avatar': 'Y',
'thumbnail': 'Video',
'duration': '0:32',
'isMe': true,
'timestamp': '2:32 PM',
},
];
int _parseSeconds(String duration) {
final parts = duration.split(':');
return int.parse(parts[0]) * 60 + int.parse(parts[1]);
}
String _formatSeconds(int seconds) {
final remainder = (seconds % 60).toString().padLeft(2, '0');
return '${seconds ~/ 60}:$remainder';
}
@override
Widget build(BuildContext context) {
final isDark = Theme.of(context).brightness == Brightness.dark;
return Scaffold(
appBar: AppBar(
title: const Text('Video 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 _buildVideoMessage(
context,
index: index,
avatar: message['avatar'],
duration: message['duration'],
isMe: message['isMe'],
isDark: isDark,
);
},
),
);
}
Widget _buildVideoMessage(
BuildContext context, {
required int index,
required String avatar,
required String duration,
required bool isMe,
required bool isDark,
}) {
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),
],
GestureDetector(
onTap: () => _showVideoPlayer(context, index, isDark),
child: Container(
width: 200,
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.65,
),
decoration: BoxDecoration(
color: isDark ? Colors.grey[800] : Colors.grey[900],
borderRadius: BorderRadius.circular(16),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Stack(
children: [
Container(
height: 120,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: isMe
? [Colors.purple[400]!, Colors.purple[700]!]
: [Colors.blue[400]!, Colors.blue[700]!],
),
borderRadius: const BorderRadius.vertical(
top: Radius.circular(16),
),
),
child: Center(
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.3),
shape: BoxShape.circle,
),
child: const Icon(
Icons.play_arrow,
color: Colors.white,
size: 32,
),
),
),
),
Positioned(
bottom: 8,
right: 8,
child: Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: Colors.black54,
borderRadius: BorderRadius.circular(4),
),
child: Text(
duration,
style: const TextStyle(
color: Colors.white,
fontSize: 12,
),
),
),
),
],
),
Padding(
padding: const EdgeInsets.all(8),
child: Row(
children: [
Expanded(
child: SliderTheme(
data: SliderTheme.of(context).copyWith(
trackHeight: 3,
thumbShape: const RoundSliderThumbShape(
enabledThumbRadius: 5,
),
),
child: Slider(
value: _progress[index],
onChanged: (value) {
setState(() => _progress[index] = value);
},
activeColor: isMe ? Colors.white : Colors.white,
inactiveColor: Colors.white30,
),
),
),
],
),
),
],
),
),
),
if (isMe) ...[
const SizedBox(width: 8),
CircleAvatar(
radius: 18,
backgroundColor: Colors.purple[400],
child: Text(avatar, style: const TextStyle(color: Colors.white)),
),
],
],
),
);
}
void _showVideoPlayer(BuildContext context, int index, bool isDark) {
final totalSeconds = _parseSeconds(_messages[index]['duration']);
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.7,
decoration: BoxDecoration(
color: isDark ? Colors.grey[900] : Colors.black,
borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
),
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 12),
width: 40,
height: 4,
decoration: BoxDecoration(
color: Colors.white24,
borderRadius: BorderRadius.circular(2),
),
),
Expanded(
child: Stack(
alignment: Alignment.center,
children: [
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.blue[700]!, Colors.purple[700]!],
),
),
child: const Center(
child: Icon(
Icons.play_circle_fill,
size: 80,
color: Colors.white70,
),
),
),
Positioned(
bottom: 20,
left: 16,
right: 16,
child: Column(
children: [
// The sheet is its own route, so the page's setState
// can't rebuild it. StatefulBuilder updates this part.
StatefulBuilder(
builder: (context, setSheetState) => Column(
children: [
Slider(
value: _progress[index],
onChanged: (value) {
setState(() => _progress[index] = value);
setSheetState(() {});
},
activeColor: Colors.white,
inactiveColor: Colors.white30,
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
_formatSeconds(
(_progress[index] * totalSeconds).round(),
),
style: TextStyle(
color: Colors.white.withValues(alpha: 0.8),
fontSize: 12,
),
),
Text(
_formatSeconds(totalSeconds),
style: TextStyle(
color: Colors.white.withValues(alpha: 0.8),
fontSize: 12,
),
),
],
),
],
),
),
],
),
),
],
),
),
],
),
),
);
}
}
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
Thumbnails are gradients and nothing plays. Each bubble owns a position in _progress, and tapping one calls _showVideoPlayer with its index, opening a showModalBottomSheet with a play icon, slider and time labels. Those labels come from the message’s duration, through _parseSeconds and _formatSeconds. A StatefulBuilder rebuilds the sheet’s controls while the page’s setState keeps the bubble behind in sync.
Adapting it
- Give
_showVideoPlayeraVideoPlayerControllerfrom video_player, disposed on close, and drive_progressfrom it. - The
thumbnailkey in_messagesis never read, so store a real image URL there and show it in_buildVideoMessage.