{"id":1124,"date":"2023-10-12T12:15:38","date_gmt":"2023-10-12T12:15:38","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=1124"},"modified":"2023-10-12T12:15:40","modified_gmt":"2023-10-12T12:15:40","slug":"adding-a-search-or-filter-feature-to-your-flutter-app","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/adding-a-search-or-filter-feature-to-your-flutter-app\/","title":{"rendered":"Adding a Search or Filter Feature to Your Flutter App"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-15.png\" alt=\"\" class=\"wp-image-1126\" width=\"408\" height=\"229\"\/><\/figure>\n\n\n\n<p>Filtering and searching are essential functionalities in many mobile apps. we&#8217;ll explore how to implement a search or filter feature in a Flutter app.<\/p>\n\n\n\n<p>Let&#8217;s create a new Flutter project to implement the search or filter feature. Open your terminal and run the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flutter create search_filter_app\r\ncd search_filter_app\r\n<\/code><\/pre>\n\n\n\n<p>This will create a new Flutter project named &#8220;search_filter_app&#8221; and change your working directory to the project folder.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Creating the User Interface<\/h4>\n\n\n\n<p>To keep things simple, we will create a list of items with a search bar at the top. Follow these steps to create the user interface:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open the <code>lib\/main.dart<\/code> file.<\/li>\n\n\n\n<li>Replace the contents with the following code:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:flutter\/material.dart';\r\n\r\nvoid main() {\r\n  runApp(SearchFilterApp());\r\n}\r\n\r\nclass SearchFilterApp extends StatelessWidget {\r\n  @override\r\n  Widget build(BuildContext context) {\r\n    return MaterialApp(\r\n      title: 'Search and Filter',\r\n      home: SearchFilterScreen(),\r\n    );\r\n  }\r\n}\r\n\r\nclass SearchFilterScreen extends StatefulWidget {\r\n  @override\r\n  _SearchFilterScreenState createState() => _SearchFilterScreenState();\r\n}\r\n\r\nclass _SearchFilterScreenState extends State&lt;SearchFilterScreen> {\r\n  final List&lt;String> items = List.generate(50, (index) => 'Item $index');\r\n  List&lt;String> filteredItems = &#91;];\r\n\r\n  void filterItems(String query) {\r\n    filteredItems = items\r\n        .where((item) => item.toLowerCase().contains(query.toLowerCase()))\r\n        .toList();\r\n    setState(() {});\r\n  }\r\n\r\n  @override\r\n  Widget build(BuildContext context) {\r\n    return Scaffold(\r\n      appBar: AppBar(\r\n        title: Text('Search and Filter'),\r\n      ),\r\n      body: Column(\r\n        children: &lt;Widget>&#91;\r\n          Padding(\r\n            padding: const EdgeInsets.all(16.0),\r\n            child: TextField(\r\n              onChanged: filterItems,\r\n              decoration: InputDecoration(\r\n                labelText: 'Search',\r\n                border: OutlineInputBorder(\r\n                  borderRadius: BorderRadius.circular(12),\r\n                ),\r\n              ),\r\n            ),\r\n          ),\r\n          Expanded(\r\n            child: ListView.builder(\r\n              itemCount: filteredItems.isNotEmpty ? filteredItems.length : items.length,\r\n              itemBuilder: (context, index) {\r\n                return ListTile(\r\n                  title: Text(filteredItems.isNotEmpty ? filteredItems&#91;index] : items&#91;index]),\r\n                );\r\n              },\r\n            ),\r\n          ),\r\n        ],\r\n      ),\r\n    );\r\n  }\r\n}\r\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"133\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-14.png\" alt=\"\" class=\"wp-image-1125\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-14.png 591w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/10\/image-14-300x68.png 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this code, we create a simple Flutter app with a search bar and a list of items. The <code>filterItems<\/code> method is used to filter the items based on the user&#8217;s input. The filtered items are displayed in a <code>ListView<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Filtering or Searching Data<\/h4>\n\n\n\n<p>In the code above, the <code>filterItems<\/code> method is responsible for filtering the items based on the search query. It uses the <code>where<\/code> method to filter the items and converts them into a list. The <code>setState<\/code> method is called to trigger a rebuild of the UI with the updated filtered items.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Displaying Filtered Results<\/h4>\n\n\n\n<p>The filtered results are displayed in a <code>ListView.builder<\/code>. The <code>itemCount<\/code> property is set based on whether there are filtered items. If there are filtered items, the count is set to the number of filtered items; otherwise, it&#8217;s set to the total number of items.<\/p>\n\n\n\n<p>The filtered items or the original items are displayed as <code>ListTile<\/code> widgets in the list.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Styling the Search Bar<\/h4>\n\n\n\n<p>In the code, we&#8217;ve styled the search bar by providing it with rounded borders. We set the <code>borderRadius<\/code> property to achieve the rounded appearance. Adjust the <code>borderRadius<\/code> value to control the roundness of the corners.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Testing and Running the App<\/h4>\n\n\n\n<p>To test the app, open your terminal and navigate to your project directory. Then, run the app with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flutter run\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Filtering and searching are essential functionalities in many mobile apps. we&#8217;ll explore how to implement a search or filter feature in a Flutter app. Let&#8217;s create a&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[425,426,428,427],"class_list":["post-1124","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-adding-a-search-or-filter-feature-to-your-flutter-app","tag-filter-feature","tag-flutter-app","tag-search"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=1124"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"predecessor-version":[{"id":1127,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/1124\/revisions\/1127"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}