From f0426b0246d90ec581698c89539b527809bd1218 Mon Sep 17 00:00:00 2001 From: Florian Griffon Date: Thu, 31 Oct 2024 16:15:00 +0100 Subject: [PATCH] feat: searchbar permanent --- dialer/lib/features/home/home_page.dart | 68 +++++++++++++++++-------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/dialer/lib/features/home/home_page.dart b/dialer/lib/features/home/home_page.dart index 26d2f7f..09ded0c 100644 --- a/dialer/lib/features/home/home_page.dart +++ b/dialer/lib/features/home/home_page.dart @@ -3,17 +3,43 @@ import 'package:dialer/features/contacts/contact_page.dart'; import 'package:dialer/features/favorites/favorites_page.dart'; import 'package:dialer/features/history/history_page.dart'; import 'package:dialer/features/composition/composition.dart'; +import 'package:flutter_contacts/flutter_contacts.dart'; class _MyHomePageState extends State with SingleTickerProviderStateMixin { late TabController _tabController; - final SearchController _searchController = SearchController(); + List _allContacts = []; + List _contactSuggestions = []; @override void initState() { super.initState(); _tabController = TabController(length: 3, vsync: this, initialIndex: 1); _tabController.addListener(_handleTabIndex); + _fetchContacts(); + } + + void _fetchContacts() async { + if (await FlutterContacts.requestPermission()) { + _allContacts = await FlutterContacts.getContacts(withProperties: true); + setState(() {}); + } + } + + void _onSearchChanged(String query) { + print("Search query: $query"); + + setState(() { + if (query.isEmpty) { + _contactSuggestions = List.from(_allContacts); + } else { + _contactSuggestions = _allContacts.where((contact) { + return contact.displayName + .toLowerCase() + .contains(query.toLowerCase()); + }).toList(); + } + }); } @override @@ -27,10 +53,6 @@ class _MyHomePageState extends State setState(() {}); } - void _onSearchChanged(String query) { - // Use this method to manage search actions if needed - } - @override Widget build(BuildContext context) { return Scaffold( @@ -40,15 +62,14 @@ class _MyHomePageState extends State // Persistent Search Bar Padding( padding: const EdgeInsets.only( - top: 24.0, // Outside top padding - bottom: 10.0, // Outside bottom padding - left: 16.0, // Outside left padding - right: 16.0, // Outside right padding + top: 24.0, + bottom: 10.0, + left: 16.0, + right: 16.0, ), child: Container( decoration: BoxDecoration( - color: const Color.fromARGB( - 255, 30, 30, 30), // Background of the SearchBar + color: const Color.fromARGB(255, 30, 30, 30), borderRadius: BorderRadius.circular(12.0), border: Border( top: BorderSide(color: Colors.grey.shade800, width: 1), @@ -63,15 +84,15 @@ class _MyHomePageState extends State controller: controller, padding: MaterialStateProperty.all( EdgeInsets.only( - top: 10.0, // Inside top padding - bottom: 10.0, // Inside bottom padding - left: 16.0, // Inside left padding - right: 16.0, // Inside right padding + top: 6.0, + bottom: 6.0, + left: 16.0, + right: 16.0, ), ), - onChanged: _onSearchChanged, onTap: () { controller.openView(); + _onSearchChanged(''); }, backgroundColor: MaterialStateProperty.all( const Color.fromARGB(255, 30, 30, 30)), @@ -91,18 +112,21 @@ class _MyHomePageState extends State ), ); }, + viewOnChanged: (query) { + _onSearchChanged(query); + }, suggestionsBuilder: (BuildContext context, SearchController controller) { - return List.generate(5, (int index) { - final String item = 'Suggestion $index'; + return _contactSuggestions.map((contact) { return ListTile( - title: Text(item), + key: ValueKey(contact.id), + title: Text(contact.displayName, + style: const TextStyle(color: Colors.white)), onTap: () { - // Close the search view and select suggestion - controller.closeView(item); + controller.closeView(contact.displayName); }, ); - }); + }).toList(); }, ), ),