import 'package:flutter_bloc/flutter_bloc.dart'; import 'account_event.dart'; import 'account_state.dart'; class AccountBloc extends Bloc { AccountBloc({ AccountStatus initialStatus = AccountStatus.undefined, String? accountId, }) : super(AccountState(status: initialStatus, accountId: accountId)) { on( (event, emit) => emit( AccountState( status: event.status, accountId: event.status == AccountStatus.loggedOut ? null : state.accountId, ), ), ); on( (event, emit) => emit( AccountState( status: AccountStatus.loggedIn, accountId: event.accountId, freshLogin: event.freshLogin, ), ), ); } void setStatus(AccountStatus status) => add(AccountStatusChanged(status)); /// [accountId] became the active account (login, switch or takeover after a /// sign-out); null means no account is left. void activated(String? accountId, {bool freshLogin = false}) => add( accountId == null ? const AccountStatusChanged(AccountStatus.loggedOut) : AccountActivated(accountId, freshLogin: freshLogin), ); }