bloc
This commit is contained in:
24
lib/view_model/LoginPage/Loginbloc.dart
Normal file
24
lib/view_model/LoginPage/Loginbloc.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'Loginobserver.dart';
|
||||
|
||||
class CounterBloc extends Bloc<CounterEvents, CounterStates> {
|
||||
int counter = 0;
|
||||
|
||||
CounterBloc() : super(UpdateState(0)) {
|
||||
on<NumberIncreaseEvent>(onNumberIncrease);
|
||||
on<NumberDecreaseEvent>(onNumberDecrease);
|
||||
}
|
||||
|
||||
void onNumberIncrease(
|
||||
NumberIncreaseEvent event, Emitter<CounterStates> emit) async {
|
||||
counter = counter + 1;
|
||||
emit(UpdateState(counter));
|
||||
}
|
||||
|
||||
void onNumberDecrease(
|
||||
NumberDecreaseEvent event, Emitter<CounterStates> emit) async {
|
||||
counter = counter - 1;
|
||||
emit(UpdateState(counter));
|
||||
}
|
||||
}
|
||||
14
lib/view_model/LoginPage/Loginobserver.dart
Normal file
14
lib/view_model/LoginPage/Loginobserver.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
class CounterEvents {}
|
||||
|
||||
class NumberIncreaseEvent extends CounterEvents {}
|
||||
|
||||
class NumberDecreaseEvent extends CounterEvents {}
|
||||
|
||||
class CounterStates {}
|
||||
|
||||
class InitialState extends CounterStates {}
|
||||
|
||||
class UpdateState extends CounterStates {
|
||||
final int counter;
|
||||
UpdateState(this.counter);
|
||||
}
|
||||
24
lib/view_model/SendOtp/sendOtpObserver.dart
Normal file
24
lib/view_model/SendOtp/sendOtpObserver.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
// Define events
|
||||
abstract class SendOtpEvent {
|
||||
const SendOtpEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class SendOtp extends SendOtpEvent {
|
||||
final Map<String, String> loginData;
|
||||
SendOtp(this.loginData){
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [loginData];
|
||||
|
||||
}
|
||||
|
||||
// Define states
|
||||
enum SendOtpState { initial, loading, success, failure }
|
||||
|
||||
33
lib/view_model/SendOtp/sendOtpbloc.dart
Normal file
33
lib/view_model/SendOtp/sendOtpbloc.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../../Utils/base_manager.dart';
|
||||
import '../../resources/routes/route_name.dart';
|
||||
import '../Login/send_otp_api.dart';
|
||||
import 'sendOtpObserver.dart';
|
||||
|
||||
class SendOtpBloc extends Bloc<SendOtpEvent, SendOtpState> {
|
||||
SendOtpBloc() : super(SendOtpState.initial) {
|
||||
on<SendOtp>(mapEventToState);
|
||||
}
|
||||
Future<void> mapEventToState(
|
||||
SendOtp event, Emitter<SendOtpState> emit) async {
|
||||
if (event is SendOtp) {
|
||||
emit(SendOtpState.loading);
|
||||
try {
|
||||
final loginData = event.loginData;
|
||||
var resp = await SendOtpAPI(loginData).sendOtpApi();
|
||||
|
||||
if (resp.status == ResponseStatus.SUCCESS) {
|
||||
emit(SendOtpState.success);
|
||||
} else {
|
||||
emit(SendOtpState.failure);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(SendOtpState.failure);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user