Untitled
unknown
dart
a year ago
3.3 kB
4
Indexable
Never
class CastSample extends StatefulWidget { static const _iconSize = 50.0; @override _CastSampleState createState() => _CastSampleState(); } class _CastSampleState extends State<CastSample> { late ChromeCastController _controller; AppState _state = AppState.idle; bool _playing = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Plugin example app'), actions: <Widget>[ ChromeCastButton( size: CastSample._iconSize, color: Colors.white, onButtonCreated: _onButtonCreated, onSessionStarted: _onSessionStarted, onSessionEnded: () => setState(() => _state = AppState.idle), onRequestCompleted: _onRequestCompleted, // onRequestFailed: () => _onRequestFailed, ), ], ), body: Center( child: Column( children: [ _handleState(), ], )), ); } Widget _handleState() { switch (_state) { case AppState.idle: return Text('ChromeCast not connected'); case AppState.connected: return Text('No media loaded'); case AppState.mediaLoaded: return _mediaControls(); case AppState.error: return Text('An error has occurred'); default: return Container(); } } Widget _mediaControls() { return Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ _RoundIconButton( icon: Icons.replay_10, onPressed: () => _controller.seek(relative: true, interval: -10.0), ), _RoundIconButton( icon: _playing ? Icons.pause : Icons.play_arrow, onPressed: _playPause), _RoundIconButton( icon: Icons.forward_10, onPressed: () => _controller.seek(relative: true, interval: 10.0), ) ], ); } Future<void> _playPause() async { final bool? playing = await _controller.isPlaying(); if (playing!) { await _controller.pause(); } else { await _controller.play(); } setState(() => _playing = !playing); } Future<void> _onButtonCreated(ChromeCastController controller) async { _controller = controller; await _controller.addSessionListener(); } Future<void> _onSessionStarted() async { setState(() => _state = AppState.connected); await _controller.loadMedia( 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', title: 'Tamreeni', ); } Future<void> _onRequestCompleted() async { final playing = await _controller.isPlaying(); setState(() { _state = AppState.mediaLoaded; _playing = playing!; }); } _onRequestFailed(String error) async { setState(() => _state = AppState.error); debugPrint(error); } } class _RoundIconButton extends StatelessWidget { final IconData icon; final VoidCallback onPressed; const _RoundIconButton({required this.icon, required this.onPressed}); @override Widget build(BuildContext context) { return ElevatedButton( onPressed: onPressed, child: Icon(icon, color: Colors.white)); } } enum AppState { idle, connected, mediaLoaded, error }