Skip to content

Instantly share code, notes, and snippets.

@themobilecoder
Last active August 29, 2022 07:28
Show Gist options
  • Save themobilecoder/cc646d4333ea284220d53c7419abaebf to your computer and use it in GitHub Desktop.
Save themobilecoder/cc646d4333ea284220d53c7419abaebf to your computer and use it in GitHub Desktop.
Simple Bloc Pattern examples
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,
);
}
}
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');
}
}),
);
@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 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();
}
}
}
class MusicPlayerBloc extends Bloc<MusicPlayerEvent, MusicPlayerState> {
@override
MusicPlayerState get initialState => MusicPlayerIdle();
@override
Stream<MusicPlayerState> mapEventToState(MusicPlayerEvent event) async* {
//TODO: Handle Events
}
}
@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 {}
@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 {}
SimpleButton(
icon: Icons.play_arrow,
onPressed: () {
BlocProvider.of<MusicPlayerBloc>(context).add(PlayEvent(musicTitle: 'Sandstorm'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment