Skip to content

Commit

Permalink
fix: added separate event for exchange rate change
Browse files Browse the repository at this point in the history
  • Loading branch information
lumpsoid committed Jun 17, 2024
1 parent 7c436d9 commit c1c2a66
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
15 changes: 14 additions & 1 deletion lib/form/bloc/form_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BillFormBloc extends Bloc<FormEvent, BillFormState> {
on<FormTagsChanged>(_onTagsChanged);
on<FormDateChanged>(_onDateChanged);
on<FormPriceChanged>(_onPriceChanged);
on<FormExchangeRateChanged>(_onExchangeRateChanged);
on<FormCurrencyChanged>(_onCurrencyChanged);
}
final BillRepository _billRepository;
Expand Down Expand Up @@ -99,7 +100,19 @@ class BillFormBloc extends Bloc<FormEvent, BillFormState> {
) async {
if (state is BillFormLoaded) {
final stateLoaded = state as BillFormLoaded;
emit(stateLoaded.copyWith(price: event.price));
final price = double.tryParse(event.price) ?? 0.0;
emit(stateLoaded.copyWith(price: price));
}
}

Future<void> _onExchangeRateChanged(
FormExchangeRateChanged event,
Emitter<BillFormState> emit,
) async {
if (state is BillFormLoaded) {
final stateLoaded = state as BillFormLoaded;
final rate = double.tryParse(event.rate) ?? 0.0;
emit(stateLoaded.copyWith(exchangeRate: rate));
}
}

Expand Down
13 changes: 11 additions & 2 deletions lib/form/bloc/form_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,23 @@ final class FormDateChanged extends FormEvent {
}

final class FormPriceChanged extends FormEvent {
final double price;

const FormPriceChanged(this.price);

final String price;

@override
List<Object> get props => [price];
}

final class FormExchangeRateChanged extends FormEvent {
const FormExchangeRateChanged(this.rate);

final String rate;

@override
List<Object> get props => [rate];
}

final class FormCurrencyChanged extends FormEvent {
final String currency;

Expand Down
9 changes: 5 additions & 4 deletions lib/form/view/form_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,8 @@ class _PriceField extends StatelessWidget {
decoration: const InputDecoration(labelText: 'Price'),
keyboardType: TextInputType.number,
validator: (value) => value!.isEmpty ? 'Price is required' : null,
onChanged: (value) => context
.read<BillFormBloc>()
.add(FormPriceChanged(double.parse(value!))),
onChanged: (value) =>
context.read<BillFormBloc>().add(FormPriceChanged(value)),
initialValue: '',
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[0-9,.]')),
Expand Down Expand Up @@ -253,9 +252,11 @@ class _ExchageRateField extends StatelessWidget {
return TextFormField(
decoration: const InputDecoration(labelText: 'Exchange Rate'),
keyboardType: TextInputType.number,
validator: (value) =>
value!.isEmpty ? 'Exchange rate is required' : null,
onChanged: (value) => context
.read<BillFormBloc>()
.add(FormPriceChanged(double.parse(value))),
.add(FormExchangeRateChanged(value)),
initialValue: state.exchangeRate.toString(),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
Expand Down

0 comments on commit c1c2a66

Please sign in to comment.