row_bloc.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import 'package:flutter_bloc/flutter_bloc.dart';
  2. import 'package:freezed_annotation/freezed_annotation.dart';
  3. import 'package:dartz/dartz.dart';
  4. import 'dart:async';
  5. import 'data.dart';
  6. import 'row_service.dart';
  7. part 'row_bloc.freezed.dart';
  8. class RowBloc extends Bloc<RowEvent, RowState> {
  9. final RowService service;
  10. final GridRowData data;
  11. RowBloc({required this.data, required this.service}) : super(RowState.initial()) {
  12. on<RowEvent>(
  13. (event, emit) async {
  14. await event.map(
  15. initial: (_InitialRow value) async {},
  16. createRow: (_CreateRow value) {},
  17. highlightRow: (_HighlightRow value) {
  18. emit(state.copyWith(
  19. isHighlight: value.rowId.fold(() => false, (rowId) => rowId == data.row.id),
  20. ));
  21. },
  22. );
  23. },
  24. );
  25. }
  26. @override
  27. Future<void> close() async {
  28. return super.close();
  29. }
  30. }
  31. @freezed
  32. abstract class RowEvent with _$RowEvent {
  33. const factory RowEvent.initial() = _InitialRow;
  34. const factory RowEvent.createRow() = _CreateRow;
  35. const factory RowEvent.highlightRow(Option<String> rowId) = _HighlightRow;
  36. }
  37. @freezed
  38. abstract class RowState with _$RowState {
  39. const factory RowState({
  40. required bool isHighlight,
  41. }) = _RowState;
  42. factory RowState.initial() => const RowState(isHighlight: false);
  43. }