|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:flutter_unit/blocs/category/category_bloc.dart'; |
| 4 | +import 'package:flutter_unit/blocs/category/category_event.dart'; |
| 5 | +import 'package:flutter_unit/repositories/itf/category_repository.dart'; |
| 6 | + |
| 7 | +import 'category_widget_event.dart'; |
| 8 | +import 'category_widget_state.dart'; |
| 9 | + |
| 10 | +/// create by 张风捷特烈 on 2020-04-21 |
| 11 | +/// contact me by email [email protected] |
| 12 | +/// 说明: |
| 13 | +
|
| 14 | +class CategoryWidgetBloc |
| 15 | + extends Bloc<CategoryWidgetEvent, CategoryWidgetState> { |
| 16 | + final CategoryBloc categoryBloc; |
| 17 | + |
| 18 | + CategoryWidgetBloc({@required this.categoryBloc}) { |
| 19 | + print('CategoryBloc'); |
| 20 | + } |
| 21 | + |
| 22 | + CategoryRepository get repository => categoryBloc.repository; |
| 23 | + |
| 24 | + @override |
| 25 | + CategoryWidgetState get initialState => CategoryWidgetEmptyState(); //初始状态 |
| 26 | + |
| 27 | + @override |
| 28 | + Stream<CategoryWidgetState> mapEventToState( |
| 29 | + CategoryWidgetEvent event) async* { |
| 30 | + if (event is EventLoadCategoryWidget) { |
| 31 | + final widgets = |
| 32 | + await repository.loadCategoryWidgets(categoryId: event.categoryId); |
| 33 | + yield widgets.isNotEmpty |
| 34 | + ? CategoryWidgetLoadedState(widgets) |
| 35 | + : CategoryWidgetEmptyState(); |
| 36 | + categoryBloc.add(EventLoadCategory()); |
| 37 | + } |
| 38 | + |
| 39 | + if (event is EventToggleCategoryWidget) { |
| 40 | + await repository.toggleCategory(event.categoryId, event.widgetId); |
| 41 | + add(EventLoadCategoryWidget(event.categoryId)); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments