Skip to content

Commit 9352e41

Browse files
Merge pull request #56 from mrsumanbiswas/54-bug-uer-search-bug
search is enhanced and min length is 3 now
2 parents 6da725b + 32b7db5 commit 9352e41

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

mobile/lib/controllers/chat.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ class ChatController extends ChangeNotifier {
8888
}
8989

9090
Future<void> queryUsers(String term) async {
91-
if (term.trim().isEmpty) {
91+
final String cleanTerm = term.trim();
92+
93+
if (cleanTerm.isEmpty) {
94+
contactSearchResults.clear();
95+
notifyListeners();
96+
return;
97+
}
98+
99+
if (cleanTerm.length < 3) {
92100
contactSearchResults.clear();
93101
notifyListeners();
94102
return;
@@ -97,7 +105,7 @@ class ChatController extends ChangeNotifier {
97105
notifyListeners();
98106

99107
try {
100-
final res = await _api.searchUsers(term);
108+
final res = await _api.searchUsers(cleanTerm);
101109
if (res.data == null) {
102110
contactSearchResults = [];
103111
} else if (res.data is List) {

mobile/lib/pages/new_chat_page.dart

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class _NewChatPageState extends State<NewChatPage> {
4949
autofocus: true,
5050
style: const TextStyle(color: Colors.black87),
5151
decoration: const InputDecoration(
52-
hintText: "Enter exact username...",
52+
hintText: "Enter minimum 3 characters...",
5353
hintStyle: TextStyle(color: Colors.black38),
5454
enabledBorder: UnderlineInputBorder(
5555
borderSide: BorderSide(color: Colors.black26),
@@ -73,13 +73,22 @@ class _NewChatPageState extends State<NewChatPage> {
7373
),
7474
onPressed: () {
7575
final text = inputController.text.trim();
76-
if (text.isNotEmpty) {
76+
if (text.length >= 3) {
7777
context.read<ChatController>().queryUsers(text);
7878
setState(() {
7979
_searchController.text = text;
8080
});
81+
Navigator.pop(context);
82+
} else {
83+
ScaffoldMessenger.of(context).showSnackBar(
84+
const SnackBar(
85+
content: Text(
86+
"Search query must be at least 3 characters.",
87+
),
88+
backgroundColor: Colors.redAccent,
89+
),
90+
);
8191
}
82-
Navigator.pop(context);
8392
},
8493
child: const Text("Search", style: TextStyle(color: Colors.white)),
8594
),
@@ -115,7 +124,9 @@ class _NewChatPageState extends State<NewChatPage> {
115124
@override
116125
Widget build(BuildContext context) {
117126
final chatState = context.watch<ChatController>();
118-
final bool isSearching = _searchController.text.trim().isNotEmpty;
127+
final String searchInput = _searchController.text.trim();
128+
final bool isSearching = searchInput.isNotEmpty;
129+
final bool hasValidQueryLength = searchInput.length >= 3;
119130

120131
return Scaffold(
121132
backgroundColor: Colors.white,
@@ -172,7 +183,17 @@ class _NewChatPageState extends State<NewChatPage> {
172183
),
173184
Expanded(
174185
child: isSearching
175-
? (chatState.isSearchLoading
186+
? (!hasValidQueryLength
187+
? const Center(
188+
child: Text(
189+
"Type at least 3 characters to search...",
190+
style: TextStyle(
191+
color: Colors.black45,
192+
fontSize: 14,
193+
),
194+
),
195+
)
196+
: chatState.isSearchLoading
176197
? const Center(
177198
child: CircularProgressIndicator(
178199
color: Color(0xFF1890FF),

0 commit comments

Comments
 (0)