Skip to content

Instantly share code, notes, and snippets.

@felangel
Created February 6, 2020 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/6088a16ec171a0a97841b646c4785d3d to your computer and use it in GitHub Desktop.
Save felangel/6088a16ec171a0a97841b646c4785d3d to your computer and use it in GitHub Desktop.
GameBloc Refactor
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_cards/models/models.dart';
part 'game_event.dart';
part 'game_state.dart';
class GameBloc extends Bloc<GameEvent, GameState> {
@override
GameState get initialState => GameLoadInProgress();
@override
Stream<GameState> mapEventToState(
GameEvent event,
) async* {
if (event is GameStarted) {
yield* _mapGameStartedToState();
} else if (event is GameCardSwiped) {
yield* _mapGameCardSwipedToState(event);
} else if (event is GameCardsRequested) {
yield* _mapGameCardsRequestedToState();
}
}
Stream<GameState> _mapGameStartedToState() async* {
await Future.delayed(const Duration(seconds: 1));
final cards = List.generate(3, (i) => GameCard(id: i));
yield GameLoadSuccess(cards: cards);
}
Stream<GameState> _mapGameCardSwipedToState(GameCardSwiped event) async* {
final currentState = state;
if (currentState is GameLoadSuccess) {
if (event.action == GameCardAction.select) {
// do something
}
if (event.action == GameCardAction.delete) {
// do something
}
final index = currentState.cards.indexWhere(
(card) => card.id == event.id,
);
// If only three cards remaining...
if (currentState.cards.length - index < 2) {
add(GameCardsRequested());
}
}
}
Stream<GameState> _mapGameCardsRequestedToState() async* {
final currentState = state;
if (currentState is GameLoadSuccess) {
await Future.delayed(const Duration(seconds: 1));
final oldCards = currentState.cards;
final newCards =
List.generate(3, (i) => GameCard(id: oldCards.length + i));
yield GameLoadSuccess(
cards: List.from(oldCards)..addAll(newCards),
);
}
}
}
part of 'game_bloc.dart';
enum GameCardAction { select, delete }
abstract class GameEvent extends Equatable {
const GameEvent();
@override
List<Object> get props => [];
}
class GameStarted extends GameEvent {}
class GameCardSwiped extends GameEvent {
final int id;
final GameCardAction action;
const GameCardSwiped({this.id, this.action});
@override
List<Object> get props => [id, action];
@override
String toString() =>
'GameCardSwiped { id: $id, action: ${describeEnum(action)} }';
}
class GameCardsRequested extends GameEvent {}
part of 'game_bloc.dart';
abstract class GameState extends Equatable {
@override
List<Object> get props => [];
}
class GameLoadInProgress extends GameState {}
class GameLoadSuccess extends GameState {
final List<GameCard> cards;
GameLoadSuccess({@required this.cards});
@override
List<Object> get props => [cards];
}
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_cards/bloc_delegate.dart';
import 'package:flutter_cards/tinder_swiper.dart';
import 'bloc/game_bloc.dart';
void main() {
BlocSupervisor.delegate = MyBlocDelegate();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(
title: Text('Game'),
),
body: BlocProvider(
create: (context) => GameBloc()..add(GameStarted()),
child: Column(children: [GamePage()]),
),
),
);
}
}
class GamePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<GameBloc, GameState>(
builder: (context, state) {
if (state is GameLoadSuccess) {
final cards = state.cards;
return TinderSwipe(
cards: cards,
onSelected: (index) {
context.bloc<GameBloc>().add(GameCardSwiped(id: cards[index].id, action: GameCardAction.select));
},
onDeleted: (index) {
context.bloc<GameBloc>().add(GameCardSwiped(id: cards[index].id, action: GameCardAction.delete));
},
);
}
return Center(child: CircularProgressIndicator());
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment