diff --git a/mobile/lib/controllers/chat.dart b/mobile/lib/controllers/chat.dart index 93276f8..7cf1379 100644 --- a/mobile/lib/controllers/chat.dart +++ b/mobile/lib/controllers/chat.dart @@ -88,7 +88,15 @@ class ChatController extends ChangeNotifier { } Future queryUsers(String term) async { - if (term.trim().isEmpty) { + final String cleanTerm = term.trim(); + + if (cleanTerm.isEmpty) { + contactSearchResults.clear(); + notifyListeners(); + return; + } + + if (cleanTerm.length < 3) { contactSearchResults.clear(); notifyListeners(); return; @@ -97,7 +105,7 @@ class ChatController extends ChangeNotifier { notifyListeners(); try { - final res = await _api.searchUsers(term); + final res = await _api.searchUsers(cleanTerm); if (res.data == null) { contactSearchResults = []; } else if (res.data is List) { diff --git a/mobile/lib/pages/new_chat_page.dart b/mobile/lib/pages/new_chat_page.dart index f51f5c0..82ba109 100644 --- a/mobile/lib/pages/new_chat_page.dart +++ b/mobile/lib/pages/new_chat_page.dart @@ -49,7 +49,7 @@ class _NewChatPageState extends State { autofocus: true, style: const TextStyle(color: Colors.black87), decoration: const InputDecoration( - hintText: "Enter exact username...", + hintText: "Enter minimum 3 characters...", hintStyle: TextStyle(color: Colors.black38), enabledBorder: UnderlineInputBorder( borderSide: BorderSide(color: Colors.black26), @@ -73,13 +73,22 @@ class _NewChatPageState extends State { ), onPressed: () { final text = inputController.text.trim(); - if (text.isNotEmpty) { + if (text.length >= 3) { context.read().queryUsers(text); setState(() { _searchController.text = text; }); + Navigator.pop(context); + } else { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text( + "Search query must be at least 3 characters.", + ), + backgroundColor: Colors.redAccent, + ), + ); } - Navigator.pop(context); }, child: const Text("Search", style: TextStyle(color: Colors.white)), ), @@ -115,7 +124,9 @@ class _NewChatPageState extends State { @override Widget build(BuildContext context) { final chatState = context.watch(); - final bool isSearching = _searchController.text.trim().isNotEmpty; + final String searchInput = _searchController.text.trim(); + final bool isSearching = searchInput.isNotEmpty; + final bool hasValidQueryLength = searchInput.length >= 3; return Scaffold( backgroundColor: Colors.white, @@ -172,7 +183,17 @@ class _NewChatPageState extends State { ), Expanded( child: isSearching - ? (chatState.isSearchLoading + ? (!hasValidQueryLength + ? const Center( + child: Text( + "Type at least 3 characters to search...", + style: TextStyle( + color: Colors.black45, + fontSize: 14, + ), + ), + ) + : chatState.isSearchLoading ? const Center( child: CircularProgressIndicator( color: Color(0xFF1890FF),