Skip to content

Commit

Permalink
Replace BLoC with Cubit
Browse files Browse the repository at this point in the history
  • Loading branch information
vkmaxcooldude committed Apr 1, 2024
1 parent f91ed2d commit cf9bf3c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 30 deletions.
17 changes: 0 additions & 17 deletions examples/mirai_gallery/lib/app/home/bloc/home_event.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'home_event.dart';
part 'home_state.dart';

class HomeBloc extends Bloc<HomeEvent, HomeState> {
class HomeBloc extends Cubit<HomeState> {
HomeBloc() : super(const HomeLoading()) {
on<_Initialize>(_onInitializeEvent);
on<UserInputEvent>(_onUserInputEvent);
controller = TextEditingController()
..addListener(_textEditingControllerListener);
add(const _Initialize());
_initialize();
}

late final TextEditingController controller;

final List<Map<String, dynamic>> _allItems = [];

void _onInitializeEvent(_Initialize event, Emitter<HomeState> emit) async {
void _initialize() async {
final items = await _loadAndParseJsonFromAsset();
if (items.isNotEmpty) {
_allItems.addAll(items);
}
emit(HomeLoaded(items: items));
}

void _onUserInputEvent(UserInputEvent event, Emitter<HomeState> emit) {
if (event.keyword.isNotEmpty) {
void _onUserInput({required String keyword}) {
if (keyword.isNotEmpty) {
List<Map<String, dynamic>> matchedItems = [];

final String keyword = event.keyword.toLowerCase();
keyword = keyword.toLowerCase();
for (final Map<String, dynamic> item in _allItems) {
var text = item["title"]?["data"];
if (text != null && text is String) {
Expand Down Expand Up @@ -60,9 +57,9 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
}
}

// Add `UserInputEvent` with the latest keyword
// Listen to text editing controller for the latest keyword
void _textEditingControllerListener() {
add(UserInputEvent(keyword: controller.text));
_onUserInput(keyword: controller.text);
}

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of 'home_bloc.dart';
part of 'home_cubit.dart';

@immutable
sealed class HomeState {
Expand Down
2 changes: 1 addition & 1 deletion examples/mirai_gallery/lib/app/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mirai/mirai.dart';

import 'bloc/home_bloc.dart';
import 'cubit/home_cubit.dart';

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
Expand Down

2 comments on commit cf9bf3c

@Chennailions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir I have a one doubt
I am prakash I am from Chennai
I am creat one action parser (Image_upload) but how to configure in json file

@divyanshub024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please create an issue?

Please sign in to comment.