row_bloc.dart 1.4 KB

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