bloc.dart 835 Bytes
import 'package:bloc/bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

import 'event.dart';
import 'state.dart';

class GamesBloc extends Bloc<GamesEvent, GamesState> {

  late MethodChannel _methodChannel;

  GamesBloc() : super(GamesState().init()) {
    on<InitEvent>(_init);
    on<GotoGamePageEvent>(_gotoGamePage);
  }

  void _init(InitEvent event, Emitter<GamesState> emit) async {
    emit(state.clone());
  }

  void _gotoGamePage(GotoGamePageEvent event, Emitter<GamesState> emit) async {
    try {
      _methodChannel = const MethodChannel('wow_english/game_method_channel');
      await _methodChannel.invokeMethod('openGamePage', { "gameId": event.gameId });
    } on PlatformException catch (e) {
      debugPrint("Failed to go to native page: '${e.message}'.");
    }
  }
}