import 'package:bloc/bloc.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:wow_english/common/extension/string_extension.dart'; import 'event.dart'; import 'game_entity.dart'; import 'state.dart'; class GamesBloc extends Bloc { late MethodChannel _methodChannel; //手动初始化4个GameEntity对象 final List _games = [ GameEntity() ..id = 1 ..imageName = 'game_food_1'.assetPng ..name = 'Food', GameEntity() ..id = 2 ..imageName = 'game_halloween_2'.assetPng ..name = 'Halloween', GameEntity() ..id = 3 ..imageName = 'game_toy_3'.assetPng ..name = 'Toy', GameEntity() ..id = 4 ..imageName = 'game_animal_4'.assetPng ..name = 'Animal' ]; List get listData => _games; GamesBloc() : super(GamesState().init()) { on(_init); on(_gotoGamePage); } void _init(InitEvent event, Emitter emit) async { emit(state.clone()); } void _gotoGamePage(GotoGamePageEvent event, Emitter 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}'."); } } }