[text] AddFormBloc.dart

Viewer

copydownloadembedprintName: AddFormBloc.dart
  1. import 'dart:ffi';
  2.  
  3. import 'package:client/additemform/models/item_category.dart';
  4. import 'package:client/additemform/models/item_condition.dart';
  5. import 'package:client/additemform/models/item_description.dart';
  6. import 'package:client/additemform/models/item_image.dart';
  7. import 'package:client/additemform/models/item_location.dart';
  8. import 'package:client/additemform/models/item_title.dart';
  9. import 'package:equatable/equatable.dart';
  10. import 'package:flutter/cupertino.dart';
  11. import 'package:flutter/services.dart';
  12. import 'package:formz/formz.dart';
  13. import 'package:bloc/bloc.dart';
  14. import 'package:user_repository/user_repository.dart';
  15. part 'additem_state.dart';
  16. part 'additem_event.dart';
  17.  
  18. class AddItemFormBloc extends Bloc<AddItemFormEvent,AddItemFormState>{
  19.   AddItemFormBloc({
  20.     required UserRepository userRepository,
  21.   }) : _userRepository= userRepository,
  22.   super(const AddItemFormState()){ 
  23.     on<AddItemFormTitleChanged>(_onTitleChanged);
  24.     on<AddItemFormDescriptionChanged>(_onDescriptionChanged);
  25.     on<AddItemLocationChanged>(_onLocationChanged);
  26.     on<AddItemConditionChanged>(_onConditionChanged);
  27.     on<AddItemCategoryChanged>(_onCategoryChanged);
  28.     on<AddItemFormSubmitted>(_onSubmitted);
  29.     on<AddItemImageChanged>(_onImageChanged);
  30.  
  31.   }
  32.   final UserRepository _userRepository;
  33.   void _onTitleChanged(
  34.     AddItemFormTitleChanged event,
  35.     Emitter<AddItemFormState> emit,
  36.   ){
  37.     final title= ItemTitle.dirty(event.title);
  38.     emit(
  39.       state.copywith(
  40.         title: title,
  41.         isValid: Formz.validate([title,state.description,state.location,state.condition,state.category,state.imageId]),
  42.       ),
  43.     );
  44.   }
  45.   void _onDescriptionChanged(
  46.     AddItemFormDescriptionChanged event,
  47.     Emitter<AddItemFormState> emit,
  48.   ){
  49.     final description= ItemDescription.dirty(event.description);
  50.     emit(
  51.       state.copywith(
  52.         description: description,
  53.         isValid: Formz.validate([description,state.title,state.location,state.condition,state.category,state.imageId]),
  54.       ),
  55.     );
  56.   }
  57.   void _onLocationChanged(
  58.     AddItemLocationChanged event,
  59.     Emitter <AddItemFormState> emit
  60.   ){
  61.     final location= ItemLocation.dirty(event.location);
  62.     emit(
  63.        state.copywith(
  64.         location: location,
  65.         isValid: Formz.validate([location,state.title,state.description,state.condition,state.category,state.imageId]),
  66.        ),
  67.     );
  68.   }
  69.   void _onConditionChanged(
  70.     AddItemConditionChanged event,
  71.     Emitter <AddItemFormState> emit
  72.   ){
  73.     final condition= ItemCondition.dirty(event.condition);
  74.     emit(
  75.       state.copywith(
  76.         condition: condition,
  77.         isValid: Formz.validate([condition,state.title,state.description,state.location,state.category,state.imageId])
  78.       )
  79.     );
  80.   }
  81.   void _onCategoryChanged(
  82.     AddItemCategoryChanged event,
  83.     Emitter <AddItemFormState> emit
  84.   ){
  85.     final category= ItemCategory.dirty(event.category);
  86.     emit(
  87.       state.copywith(
  88.         category: category,
  89.         isValid: Formz.validate([category,state.title,state.description,state.location,state.condition,state.imageId])
  90.       )
  91.     );
  92.   }
  93.   void _onImageChanged(
  94.     AddItemImageChanged event,
  95.     Emitter<AddItemFormState>emit,
  96.  
  97.   ){
  98.     final imageId= ItemImage.dirty(event.imageId);
  99.     emit(
  100.       state.copywith(
  101.         imageId: imageId,
  102.         isValid: Formz.validate([imageId,state.title,state.description,state.location,state.category,state.condition])
  103.       )
  104.     );
  105.   }
  106.  
  107.   Future<void> _onSubmitted(
  108.     AddItemFormSubmitted event,
  109.     Emitter<AddItemFormState> emit,
  110.   ) async{
  111.     if (state.isValid==true){
  112.       emit(state.copywith(status: FormzSubmissionStatus.inProgress));
  113.       try{
  114.  
  115.         await _userRepository.createItem(
  116.           title: state.title.value, 
  117.           description: state.description.value,
  118.           location: state.location.value,
  119.           category: state.category.value, 
  120.           condition: state.condition.value,
  121.           imageId: state.imageId.value,
  122.         ); 
  123.         emit(state.copywith(status:FormzSubmissionStatus.success));
  124.       }catch(_){
  125.         emit(state.copywith(status:FormzSubmissionStatus.failure));
  126.       }
  127.     }else{
  128.       print("EROORR!");
  129.     }
  130.   }
  131. }

Editor

You can edit this paste and save as new:


File Description
  • AddFormBloc.dart
  • Paste Code
  • 24 Apr-2024
  • 4.22 Kb
You can Share it: