- import 'dart:ffi';
- import 'package:client/additemform/models/item_category.dart';
- import 'package:client/additemform/models/item_condition.dart';
- import 'package:client/additemform/models/item_description.dart';
- import 'package:client/additemform/models/item_image.dart';
- import 'package:client/additemform/models/item_location.dart';
- import 'package:client/additemform/models/item_title.dart';
- import 'package:equatable/equatable.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/services.dart';
- import 'package:formz/formz.dart';
- import 'package:bloc/bloc.dart';
- import 'package:user_repository/user_repository.dart';
- part 'additem_state.dart';
- part 'additem_event.dart';
- class AddItemFormBloc extends Bloc<AddItemFormEvent,AddItemFormState>{
- AddItemFormBloc({
- required UserRepository userRepository,
- }) : _userRepository= userRepository,
- super(const AddItemFormState()){
- on<AddItemFormTitleChanged>(_onTitleChanged);
- on<AddItemFormDescriptionChanged>(_onDescriptionChanged);
- on<AddItemLocationChanged>(_onLocationChanged);
- on<AddItemConditionChanged>(_onConditionChanged);
- on<AddItemCategoryChanged>(_onCategoryChanged);
- on<AddItemFormSubmitted>(_onSubmitted);
- on<AddItemImageChanged>(_onImageChanged);
- }
- final UserRepository _userRepository;
- void _onTitleChanged(
- AddItemFormTitleChanged event,
- Emitter<AddItemFormState> emit,
- ){
- final title= ItemTitle.dirty(event.title);
- emit(
- state.copywith(
- title: title,
- isValid: Formz.validate([title,state.description,state.location,state.condition,state.category,state.imageId]),
- ),
- );
- }
- void _onDescriptionChanged(
- AddItemFormDescriptionChanged event,
- Emitter<AddItemFormState> emit,
- ){
- final description= ItemDescription.dirty(event.description);
- emit(
- state.copywith(
- description: description,
- isValid: Formz.validate([description,state.title,state.location,state.condition,state.category,state.imageId]),
- ),
- );
- }
- void _onLocationChanged(
- AddItemLocationChanged event,
- Emitter <AddItemFormState> emit
- ){
- final location= ItemLocation.dirty(event.location);
- emit(
- state.copywith(
- location: location,
- isValid: Formz.validate([location,state.title,state.description,state.condition,state.category,state.imageId]),
- ),
- );
- }
- void _onConditionChanged(
- AddItemConditionChanged event,
- Emitter <AddItemFormState> emit
- ){
- final condition= ItemCondition.dirty(event.condition);
- emit(
- state.copywith(
- condition: condition,
- isValid: Formz.validate([condition,state.title,state.description,state.location,state.category,state.imageId])
- )
- );
- }
- void _onCategoryChanged(
- AddItemCategoryChanged event,
- Emitter <AddItemFormState> emit
- ){
- final category= ItemCategory.dirty(event.category);
- emit(
- state.copywith(
- category: category,
- isValid: Formz.validate([category,state.title,state.description,state.location,state.condition,state.imageId])
- )
- );
- }
- void _onImageChanged(
- AddItemImageChanged event,
- Emitter<AddItemFormState>emit,
- ){
- final imageId= ItemImage.dirty(event.imageId);
- emit(
- state.copywith(
- imageId: imageId,
- isValid: Formz.validate([imageId,state.title,state.description,state.location,state.category,state.condition])
- )
- );
- }
- Future<void> _onSubmitted(
- AddItemFormSubmitted event,
- Emitter<AddItemFormState> emit,
- ) async{
- if (state.isValid==true){
- emit(state.copywith(status: FormzSubmissionStatus.inProgress));
- try{
- await _userRepository.createItem(
- title: state.title.value,
- description: state.description.value,
- location: state.location.value,
- category: state.category.value,
- condition: state.condition.value,
- imageId: state.imageId.value,
- );
- emit(state.copywith(status:FormzSubmissionStatus.success));
- }catch(_){
- emit(state.copywith(status:FormzSubmissionStatus.failure));
- }
- }else{
- print("EROORR!");
- }
- }
- }
[text] AddFormBloc.dart
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: