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 = 'pic_module_game'.assetPng ..name = '游戏1', GameEntity() ..id = 2 ..imageName = 'pic_module_game'.assetPng ..name = '游戏2', GameEntity() ..id = 3 ..imageName = 'pic_module_game'.assetPng ..name = '游戏3', GameEntity() ..id = 4 ..imageName = 'pic_module_game'.assetPng ..name = '游戏4' ]; 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}'."); } } }