utd_audio_room_kit 4.0.3
utd_audio_room_kit: ^4.0.3 copied to clipboard
Real-time live audio room for Flutter: seat management, real-time chat, mic/speaker controls, speak requests, moderation, and minimize/PiP.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:utd_audio_room_kit/utd_audio_room_kit.dart';
/// A voice room that uses EVERY slot. The slots are places where your app draws; each one is handed the member's data, and
/// `attributes` is whatever YOUR app published for that member (here: `level`, `tint` and `title`, arbitrary keys the kit never
/// interprets). Replace the values with your own.
void main() => runApp(const MaterialApp(home: ExampleRoom()));
Color _tint(Map<String, String> attributes) => switch (attributes['tint']) {
'blue' => Colors.blue,
'green' => Colors.green,
_ => Colors.orange,
};
/// Stand-in for the call to YOUR server (see doc/upgrade-4.0-token-provider.md): it forwards the request to the engine with the
/// Server Secret and returns the engine's JSON.
class _YourServer {
Future<Map<String, dynamic>> mintToken(Map<String, dynamic> request) async => throw UnimplementedError('call your token endpoint');
}
final yourServer = _YourServer();
class ExampleRoom extends StatelessWidget {
const ExampleRoom({super.key});
@override
Widget build(BuildContext context) {
return UTDAudioRoom(
appId: 'YOUR_APP_ID',
// Your server mints every token (see doc/upgrade-4.0-token-provider.md).
tokenProvider: (request) async => UTDTokenResponse.fromJson(await yourServer.mintToken(request.toJson())),
userId: 'user-1',
userName: 'Sam',
roomId: 'room-1',
roomOwnerId: 'user-1',
config: UTDAudioRoomConfig(
// What your app publishes about this member; every other member sees it in their slots.
userInRoomAttributes: const {'level': '7', 'tint': 'blue', 'title': 'Captain'},
// SEAT: the container wraps the whole seat (the kit's own seat is the third argument).
seatContainerBuilder: (context, seat, child) => Container(
decoration: seat.member == null
? null
: BoxDecoration(border: Border.all(color: _tint(seat.member!.attributes), width: 1.5), borderRadius: BorderRadius.circular(12)),
child: child,
),
// SEAT: a layer drawn over the seat (here the member's level) and one under it (a glow while speaking).
seatFrontBuilder: (context, seat) => seat.member == null
? null
: Align(
alignment: Alignment.bottomRight,
child: Text('Lv ${seat.member!.attributes['level'] ?? '-'}', style: const TextStyle(color: Colors.white, fontSize: 10)),
),
seatBackBuilder: (context, seat) => (seat.member?.isSpeaking ?? false)
? DecoratedBox(decoration: BoxDecoration(shape: BoxShape.circle, color: _tint(seat.member!.attributes).withValues(alpha: 0.25)))
: null,
// SEAT: the avatar (it receives the same attributes).
avatarBuilder: (userId, size, attributes, isMuted, seatIndex, userName) => CircleAvatar(
radius: size * 0.35,
backgroundColor: _tint(attributes),
child: Text(userName.isEmpty ? '?' : userName[0]),
),
// MESSAGE: one chat line, with the sender's attributes (null keeps the default bubble).
messageBuilder: (context, line) => Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Text.rich(TextSpan(children: [
TextSpan(
text: '${line.sender?.attributes['title'] ?? ''} ${line.message.senderName} ',
style: TextStyle(color: _tint(line.sender?.attributes ?? const {}), fontWeight: FontWeight.w600),
),
TextSpan(text: line.message.text, style: const TextStyle(color: Colors.white)),
])),
),
// MEMBER: one row of the member list. `actions` are the moderation buttons the default row would show.
memberBuilder: (context, row) => ListTile(
onTap: row.onTap,
leading: CircleAvatar(backgroundColor: _tint(row.member.attributes), child: Text(row.member.name[0])),
title: Text(row.member.name),
subtitle: Text('Lv ${row.member.attributes['level'] ?? '-'}${row.member.isSeated ? ' · seat ${row.member.seatIndex! + 1}' : ''}'),
trailing: Row(mainAxisSize: MainAxisSize.min, children: row.actions),
),
// PAGE: a layer under the whole page, one over it, and the free area between the seats and the messages.
pageBackBuilder: (context, page) => const DecoratedBox(
decoration: BoxDecoration(gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [Color(0xFF1B1B2F), Color(0xFF0E0E18)])),
),
pageFrontBuilder: (context, page) => Align(
alignment: Alignment.topRight,
child: SafeArea(
child: Padding(
padding: const EdgeInsets.all(8),
child: Text('${page.seatedCount}/${page.seatCount} on stage · ${page.memberCount} here', style: const TextStyle(color: Colors.white70, fontSize: 11)),
),
),
),
emptyAreaBuilder: (context, page) => Padding(
padding: const EdgeInsets.all(8),
child: Text('Welcome, ${page.local?.name ?? 'guest'} (${page.local?.attributes['title'] ?? 'member'})', style: const TextStyle(color: Colors.white54)),
),
),
);
}
}