|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_dogfooding/core/model/environment.dart'; |
| 3 | +import 'package:flutter_dogfooding/core/repos/token_service.dart'; |
| 4 | +import 'package:flutter_dogfooding/screens/login_screen.dart'; |
| 5 | +import 'package:stream_video_flutter/stream_video_flutter.dart'; |
| 6 | + |
| 7 | +class LivestreamDemoScreen extends StatefulWidget { |
| 8 | + const LivestreamDemoScreen({ |
| 9 | + super.key, |
| 10 | + required this.callId, |
| 11 | + }); |
| 12 | + |
| 13 | + final String callId; |
| 14 | + |
| 15 | + @override |
| 16 | + State<LivestreamDemoScreen> createState() => _LivestreamDemoScreenState(); |
| 17 | +} |
| 18 | + |
| 19 | +class _LivestreamDemoScreenState extends State<LivestreamDemoScreen> { |
| 20 | + static const _tokenService = TokenService(); |
| 21 | + StreamVideo? _streamVideo; |
| 22 | + Call? _call; |
| 23 | + |
| 24 | + @override |
| 25 | + void initState() { |
| 26 | + super.initState(); |
| 27 | + _joinCall(); |
| 28 | + } |
| 29 | + |
| 30 | + Future<void> _joinCall() async { |
| 31 | + final userId = randomId(size: 6); |
| 32 | + |
| 33 | + final tokenResponse = await _tokenService.loadToken( |
| 34 | + userId: userId, |
| 35 | + environment: Environment.livestream, |
| 36 | + ); |
| 37 | + |
| 38 | + var streamVideo = StreamVideo.create( |
| 39 | + tokenResponse.apiKey, |
| 40 | + user: User.regular( |
| 41 | + userId: userId, |
| 42 | + ), |
| 43 | + tokenLoader: (userId) async { |
| 44 | + final token = await _tokenService.loadToken( |
| 45 | + userId: userId, |
| 46 | + environment: Environment.livestream, |
| 47 | + ); |
| 48 | + return token.token; |
| 49 | + }, |
| 50 | + ); |
| 51 | + |
| 52 | + final call = streamVideo.makeCall( |
| 53 | + callType: StreamCallType.liveStream(), |
| 54 | + id: widget.callId, |
| 55 | + ); |
| 56 | + final result = await call.getOrCreate(); |
| 57 | + |
| 58 | + if (!mounted) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + _streamVideo = streamVideo; |
| 63 | + |
| 64 | + result.fold(success: (data) { |
| 65 | + _call = call; |
| 66 | + if (mounted) { |
| 67 | + setState(() {}); |
| 68 | + } |
| 69 | + }, failure: (error) { |
| 70 | + ScaffoldMessenger.of(context).showSnackBar(SnackBar( |
| 71 | + content: Text(error.toString()), |
| 72 | + )); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + void dispose() { |
| 78 | + _call?.leave().then((value) { |
| 79 | + _streamVideo?.dispose(); |
| 80 | + }); |
| 81 | + super.dispose(); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + Widget build(BuildContext context) { |
| 86 | + final streamVideoTheme = StreamVideoTheme.of(context); |
| 87 | + final textTheme = streamVideoTheme.textTheme; |
| 88 | + |
| 89 | + return Scaffold( |
| 90 | + appBar: AppBar( |
| 91 | + elevation: 0, |
| 92 | + backgroundColor: Theme.of(context).scaffoldBackgroundColor, |
| 93 | + titleSpacing: 4, |
| 94 | + centerTitle: false, |
| 95 | + title: Text( |
| 96 | + 'Livestream Demo', |
| 97 | + style: textTheme.body, |
| 98 | + ), |
| 99 | + actions: [ |
| 100 | + IconButton( |
| 101 | + icon: const Icon( |
| 102 | + Icons.close, |
| 103 | + color: Colors.white, |
| 104 | + ), |
| 105 | + onPressed: () { |
| 106 | + Navigator.maybePop(context); |
| 107 | + }, |
| 108 | + ), |
| 109 | + ], |
| 110 | + ), |
| 111 | + body: switch (_call) { |
| 112 | + null => const Center(child: CircularProgressIndicator()), |
| 113 | + Call call => LivestreamPlayer(call: call), |
| 114 | + }, |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments