Skip to content

Commit d39354a

Browse files
committed
search by title and content on change text on search box
1 parent fa9da13 commit d39354a

File tree

6 files changed

+79
-45
lines changed

6 files changed

+79
-45
lines changed

lib/core/db/db_helper.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ class DatabaseHelper {
4343
});
4444
}
4545

46-
static Future<List<PostModel>> findPosts(String title) async {
46+
static Future<List<PostModel>> findPosts(String value) async {
47+
String searchString1 = '$value%';
48+
String searchString2 = '$value%';
4749
final db = await DatabaseHelper.db();
48-
final List<Map<String, dynamic>> maps = await db.query('post', where: 'title = ?', whereArgs: [title]);
50+
final List<Map<String, dynamic>> maps = await db.query(
51+
'post',
52+
where: 'title LIKE ? OR content LIKE ?',
53+
whereArgs: [searchString1, searchString2],
54+
);
4955
return List.generate(maps.length, (i) {
5056
return PostModel(
5157
id: maps[i]['id'],

lib/features/home/presentation/bloc/post_bloc.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PostBloc extends Bloc<PostEvent, PostState> {
4040
selectedPosts.add(post);
4141
}
4242
}
43-
emit(PostLoadedSuccessState(postList));
43+
emit(PostLoadedSuccessState(postList, false));
4444
});
4545
}
4646

@@ -49,15 +49,15 @@ class PostBloc extends Bloc<PostEvent, PostState> {
4949
FutureOr<void> postDeleteButtonClickedEvent(PostDeleteButtonClickedEvent event, Emitter<PostState> emit) async {
5050
await DatabaseHelper.deletePost(event.post.id!);
5151
List<PostModel> postList = await DatabaseHelper.getPosts();
52-
emit(PostLoadedSuccessState(postList));
52+
emit(PostLoadedSuccessState(postList, false));
5353
}
5454

5555
FutureOr<void> postDeleteAllButtonClickedEvent(PostDeleteAllButtonClickedEvent event, Emitter<PostState> emit) async {
5656
for (var element in selectedPosts) {
5757
await DatabaseHelper.deletePost(element.id!);
5858
}
5959
List<PostModel> postList = await DatabaseHelper.getPosts();
60-
emit(PostLoadedSuccessState(postList));
60+
emit(PostLoadedSuccessState(postList, false));
6161
}
6262

6363
FutureOr<void> postAddButtonClickedEvent(PostAddButtonClickedEvent event, Emitter<PostState> emit) {
@@ -82,12 +82,28 @@ class PostBloc extends Bloc<PostEvent, PostState> {
8282
postList!.fold((failure) {
8383
// emit(Error(message: _mapFailureToMessage(failure)));
8484
}, (post) {
85-
emit(PostLoadedSuccessState(post));
85+
emit(PostLoadedSuccessState(post, false));
8686
});
8787
}
8888

8989
FutureOr<void> postSearchIconClickedEvent(PostSearchIconClickedEvent event, Emitter<PostState> emit) async {
90-
List<PostModel> postList = await DatabaseHelper.findPosts(event.value);
91-
emit(PostLoadedSuccessState(postList));
90+
if (event.isSearch) {
91+
List<PostModel> postList = await DatabaseHelper.findPosts(event.value);
92+
emit(PostLoadedSuccessState(postList, event.isSearch));
93+
} else {
94+
emit(PostLoadingState());
95+
final postModelList = await getPosts(NoParams());
96+
97+
postModelList!.fold((failure) {
98+
// emit(Error(message: _mapFailureToMessage(failure)));
99+
}, (postList) {
100+
for (var post in postList) {
101+
if (post.isSelected == 1) {
102+
selectedPosts.add(post);
103+
}
104+
}
105+
emit(PostLoadedSuccessState(postList, false));
106+
});
107+
}
92108
}
93109
}

lib/features/home/presentation/bloc/post_event.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class PostDeleteAllButtonClickedEvent extends PostEvent {}
1515

1616
class PostSearchIconClickedEvent extends PostEvent {
1717
final String value;
18-
PostSearchIconClickedEvent(this.value);
18+
bool isSearch = false;
19+
PostSearchIconClickedEvent(this.value, this.isSearch);
1920
}
2021

2122
class PostAddButtonClickedEvent extends PostEvent {}

lib/features/home/presentation/bloc/post_state.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import '../../../../core/entities/post.dart';
32

43
abstract class PostState {}
@@ -10,10 +9,11 @@ class PostInitialState extends PostState {}
109
class PostLoadingState extends PostState {}
1110

1211
class PostLoadedSuccessState extends PostState {
12+
bool isSearch;
1313
final List<Post> postList;
14-
PostLoadedSuccessState(this.postList);
14+
PostLoadedSuccessState(this.postList, this.isSearch);
1515
PostLoadedSuccessState copyWith({List<Post>? postList}) {
16-
return PostLoadedSuccessState(postList ?? this.postList);
16+
return PostLoadedSuccessState(postList ?? this.postList, isSearch);
1717
}
1818
}
1919

@@ -34,3 +34,8 @@ class PostItemDeletedActionState extends PostActionState {}
3434
class PostItemSelectedActionState extends PostActionState {}
3535

3636
class PostItemsDeletedActionState extends PostActionState {}
37+
38+
class PostSearchIconClickedState extends PostActionState {
39+
bool isSearch;
40+
PostSearchIconClickedState(this.isSearch);
41+
}

lib/features/home/presentation/ui/post.dart

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_bloc/flutter_bloc.dart';
4+
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
45
import 'package:my_blog_bloc/resources/colour_manager.dart';
56
import 'package:my_blog_bloc/resources/strings_manager.dart';
67
import '../../../../core/db/db_helper.dart';
@@ -97,26 +98,31 @@ class _HomeScreenState extends State<HomeScreen> {
9798
},
9899
),
99100
appBar: AppBar(
101+
iconTheme: IconThemeData(color: ColorManager.primary),
102+
elevation: 0,
103+
backgroundColor: ColorManager.white,
100104
// title: Obx(() => menuListController.isSearch.value
101105
// ?
102-
title: TextField(
103-
autofocus: true,
104-
controller: searchMenuController,
105-
style: TextStyle(color: ColorManager.primary),
106-
decoration: InputDecoration(
107-
fillColor: ColorManager.primary,
108-
hintText: 'Search',
109-
focusColor: ColorManager.primary,
110-
hintStyle: TextStyle(
111-
color: ColorManager.primary,
112-
),
113-
),
114-
onChanged: (value) => postBloc.add(PostSearchIconClickedEvent(value)),
115-
),
116-
// : Text(
117-
// 'Menus',
118-
// style: TextStyle(color: ColorManager.primary, fontWeight: FontWeight.bold),
119-
// )),
106+
107+
title: state.isSearch
108+
? TextField(
109+
autofocus: true,
110+
controller: searchMenuController,
111+
style: TextStyle(color: ColorManager.primary),
112+
decoration: InputDecoration(
113+
fillColor: ColorManager.primary,
114+
hintText: 'Search',
115+
focusColor: ColorManager.primary,
116+
hintStyle: TextStyle(
117+
color: ColorManager.primary,
118+
),
119+
),
120+
onChanged: (value) => postBloc.add(PostSearchIconClickedEvent(value, true)),
121+
)
122+
: Text(
123+
'Posts',
124+
style: TextStyle(color: ColorManager.primary, fontWeight: FontWeight.bold),
125+
),
120126
centerTitle: true,
121127
// title: Row(
122128
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
@@ -135,19 +141,19 @@ class _HomeScreenState extends State<HomeScreen> {
135141
// )
136142
// ],
137143
// ),
138-
// actions: [
139-
// IconButton(
140-
// onPressed: () {
141-
// menuListController.isSearch.value = !menuListController.isSearch.value;
142-
// searchMenuController.clear();
143-
// },
144-
// icon: FaIcon(
145-
// menuListController.isSearch.isFalse ? FontAwesomeIcons.magnifyingGlass : FontAwesomeIcons.xmark,
146-
// color: ColorManager.primary,
147-
// size: 20,
148-
// ),
149-
// ),
150-
//],
144+
actions: [
145+
IconButton(
146+
onPressed: () {
147+
postBloc.add(PostSearchIconClickedEvent("", !state.isSearch));
148+
searchMenuController.clear();
149+
},
150+
icon: FaIcon(
151+
state.isSearch == false ? FontAwesomeIcons.magnifyingGlass : FontAwesomeIcons.xmark,
152+
color: ColorManager.primary,
153+
size: 20,
154+
),
155+
),
156+
],
151157
),
152158
body: ListView.builder(
153159
itemCount: successState.postList.length,

lib/resources/colour_manager.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22

33
class ColorManager {
4-
static Color primary = HexColor.fromHex("#ED9728");
4+
static Color primary = HexColor.fromHex("#00304f");
55
static Color darkGrey = HexColor.fromHex("#525252");
66
static Color grey = HexColor.fromHex("#737477");
77
static Color lightGrey = HexColor.fromHex("#9E9E9E");
@@ -23,4 +23,4 @@ extension HexColor on Color {
2323
}
2424
return Color(int.parse(hexColorString, radix: 16));
2525
}
26-
}
26+
}

0 commit comments

Comments
 (0)