wt1.1a flutter_bloc

This commit is contained in:
Bossanyi Tibor
2020-08-17 12:38:47 +02:00
parent 8363d7772f
commit ac1c6be8f3
90 changed files with 4281 additions and 2854 deletions
+46
View File
@@ -0,0 +1,46 @@
import 'dart:async';
import 'package:aitrainer_app/model/auth.dart';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
part 'account_event.dart';
part 'account_state.dart';
class AccountBloc extends Bloc<AccountEvent, AccountState> {
final CustomerRepository customerRepository;
bool loggedIn = false;
AccountBloc({this.customerRepository}) : super(AccountInitial()) {
if ( Auth().userLoggedIn != null ) {
customerRepository.customer = Auth().userLoggedIn;
loggedIn = true;
}
}
@override
Stream<AccountState> mapEventToState(
AccountEvent event,
) async* {
try {
if (event is AccountChangeCustomer) {
// route to Customer Change page
yield AccountReady();
} else if (event is AccountLogin) {
//route to Login Page
} else if (event is AccountLogInFinished) {
customerRepository.customer = event.customer;
yield AccountLoggedIn();
} else if (event is AccountLogout) {
await Auth().logout();
customerRepository.customer = null;
loggedIn = false;
yield AccountLoggedOut();
}
} on Exception catch(e) {
yield AccountError(message: e.toString());
}
}
}
+46
View File
@@ -0,0 +1,46 @@
part of 'account_bloc.dart';
@immutable
abstract class AccountEvent extends Equatable {
const AccountEvent();
@override
List<Object> get props => [];
}
class AccountChangeCustomer extends AccountEvent {
final Customer customer;
const AccountChangeCustomer({this.customer});
@override
List<Object> get props => [customer];
}
class AccountLogout extends AccountEvent {
final Customer customer;
const AccountLogout({this.customer});
@override
List<Object> get props => [customer];
}
class AccountLogin extends AccountEvent {
final Customer customer;
const AccountLogin({this.customer});
@override
List<Object> get props => [customer];
}
class AccountLogInFinished extends AccountEvent {
final Customer customer;
const AccountLogInFinished({this.customer});
@override
List<Object> get props => [customer];
}
+36
View File
@@ -0,0 +1,36 @@
part of 'account_bloc.dart';
@immutable
abstract class AccountState extends Equatable {
const AccountState();
@override
List<Object> get props => [];
}
class AccountInitial extends AccountState {
const AccountInitial();
}
class AccountLoading extends AccountState {
const AccountLoading();
}
class AccountReady extends AccountState {
const AccountReady();
}
class AccountLoggedOut extends AccountState {
const AccountLoggedOut();
}
class AccountLoggedIn extends AccountState {
const AccountLoggedIn();
}
class AccountError extends AccountState {
final String message;
const AccountError({this.message});
@override
List<Object> get props => [message];
}