Last active
August 29, 2022 07:28
-
-
Save themobilecoder/cc646d4333ea284220d53c7419abaebf to your computer and use it in GitHub Desktop.
Simple Bloc Pattern examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends StatelessWidget { | |
final MusicPlayer musicPlayer = DummyMusicPlayer(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Music Player', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: BlocProvider( | |
create: (BuildContext context) => MusicPlayerBloc(DummyMusicPlayer()), | |
child: MainScreen(), | |
)); | |
} | |
} | |
class MainScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: BlocBuilder<MusicPlayerBloc, MusicPlayerState>(builder: (_, state) { | |
if (state is MusicPlayerPlaying) { | |
return Text('Playing: ${state.musicTitle}'); | |
} else if (state is MusicPlayerPaused) { | |
return Text('Paused'); | |
} else { | |
return Text('Music Player'); | |
} | |
}), | |
), | |
body: Center( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
BlocBuilder<MusicPlayerBloc, MusicPlayerState>(builder: (context, state) { | |
if (state is MusicPlayerIdle || state is MusicPlayerPaused) { | |
return SimpleButton( | |
icon: Icons.play_arrow, | |
onPressed: () { | |
BlocProvider.of<MusicPlayerBloc>(context).add(PlayEvent(musicTitle: 'Sandstorm')); | |
}); | |
} else { | |
return SimpleButton( | |
icon: Icons.pause, | |
onPressed: () { | |
BlocProvider.of<MusicPlayerBloc>(context).add(PauseEvent()); | |
}); | |
} | |
}), | |
BlocBuilder<MusicPlayerBloc, MusicPlayerState>(builder: (_, state) { | |
if (state is MusicPlayerPlaying || state is MusicPlayerPaused) { | |
return SimpleButton( | |
icon: Icons.stop, | |
onPressed: () { | |
BlocProvider.of<MusicPlayerBloc>(context).add(StopEvent()); | |
}); | |
} else { | |
return Container( | |
width: 0, | |
); | |
} | |
}) | |
], | |
), | |
), | |
); | |
} | |
} | |
class SimpleButton extends StatelessWidget { | |
final Function onPressed; | |
final IconData icon; | |
SimpleButton({@required this.onPressed, @required this.icon}); | |
@override | |
Widget build(BuildContext context) { | |
return IconButton( | |
onPressed: onPressed, | |
icon: Icon(icon), | |
iconSize: 46, | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppBar( | |
title: BlocBuilder<MusicPlayerBloc, MusicPlayerState>(builder: (_, state) { | |
if (state is MusicPlayerPlaying) { | |
return Text('Playing: ${state.musicTitle}'); | |
} else if (state is MusicPlayerPaused) { | |
return Text('Paused'); | |
} else { | |
return Text('Music Player'); | |
} | |
}), | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Music Player', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: BlocProvider( | |
create: (BuildContext context) => MusicPlayerBloc(DummyMusicPlayer()), | |
child: MainScreen(), | |
)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MusicPlayerBloc extends Bloc<MusicPlayerEvent, MusicPlayerState> { | |
final MusicPlayer _musicPlayer; | |
MusicPlayerBloc(this._musicPlayer) : assert(_musicPlayer != null); | |
@override | |
MusicPlayerState get initialState => MusicPlayerIdle(); | |
@override | |
Stream<MusicPlayerState> mapEventToState(MusicPlayerEvent event) async* { | |
if (event is PlayEvent) { | |
final musicTitle = event.musicTitle; | |
_musicPlayer.play(musicTitle); | |
yield MusicPlayerPlaying(musicTitle: musicTitle); | |
} else if (event is PauseEvent) { | |
_musicPlayer.pause(); | |
yield MusicPlayerPaused(); | |
} else if (event is StopEvent) { | |
_musicPlayer.stop(); | |
yield MusicPlayerIdle(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MusicPlayerBloc extends Bloc<MusicPlayerEvent, MusicPlayerState> { | |
@override | |
MusicPlayerState get initialState => MusicPlayerIdle(); | |
@override | |
Stream<MusicPlayerState> mapEventToState(MusicPlayerEvent event) async* { | |
//TODO: Handle Events | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@immutable | |
abstract class MusicPlayerEvent { | |
const MusicPlayerEvent(); | |
} | |
class PlayEvent extends MusicPlayerEvent { | |
final String musicTitle; | |
const PlayEvent({@required this.musicTitle}) : assert(musicTitle != null); | |
} | |
class PauseEvent extends MusicPlayerEvent {} | |
class StopEvent extends MusicPlayerEvent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@immutable | |
abstract class MusicPlayerState { | |
const MusicPlayerState(); | |
} | |
class MusicPlayerIdle extends MusicPlayerState {} | |
class MusicPlayerPlaying extends MusicPlayerState { | |
final String musicTitle; | |
const MusicPlayerPlaying({@required this.musicTitle}) : assert(musicTitle != null); | |
} | |
class MusicPlayerPaused extends MusicPlayerState {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment