How to Add a Search Bar to the AppBar in Flutter

Add an animated search bar with suggestions to your Flutter AppBar using easy_search_bar, filter a list as the user types, and load results from an API.

T

Teammity Team

· 3 min read

Most apps with a list need search, and users expect it in the app bar. They tap the search icon, the title turns into a text field, suggestions appear, and the list filters as they type. Building that animation yourself takes a fair amount of code. The easy_search_bar package gives it to you as a drop-in replacement for AppBar.

By the end of this post you will have a searchable list with suggestions, async results from an API, and custom styling.

What we will use

  • easy_search_bar: an AppBar with an animated search field and a suggestions overlay.

Step 1: Install the package

Add it to your project:

flutter pub add easy_search_bar

This adds easy_search_bar: ^2.5.0 (the current version) to your pubspec.yaml.

Step 2: Replace AppBar with EasySearchBar

EasySearchBar is the app bar, so pass it straight to Scaffold.appBar. Do not put it inside an AppBar.

Here is a complete screen that filters a list of fruits:

lib/screens/fruit_search_screen.dart
import 'package:easy_search_bar/easy_search_bar.dart';
import 'package:flutter/material.dart';
 
class FruitSearchScreen extends StatefulWidget {
  const FruitSearchScreen({super.key});
 
  @override
  State<FruitSearchScreen> createState() => _FruitSearchScreenState();
}
 
class _FruitSearchScreenState extends State<FruitSearchScreen> {
  static const _fruits = [
    'Apple', 'Apricot', 'Banana', 'Blueberry', 'Cherry',
    'Grape', 'Mango', 'Orange', 'Peach', 'Pineapple',
  ];
 
  String _query = '';
 
  List<String> get _results => _fruits
      .where((f) => f.toLowerCase().contains(_query.toLowerCase()))
      .toList();
 
  @override
  Widget build(BuildContext context) {
    final results = _results;
    return Scaffold(
      appBar: EasySearchBar(
        title: const Text('Fruits'),
        onSearch: (value) => setState(() => _query = value),
        suggestions: _fruits,
        searchHintText: 'Search fruits',
        showClearSearchIcon: true,
      ),
      body: results.isEmpty
          ? const Center(child: Text('No fruits found'))
          : ListView.builder(
              itemCount: results.length,
              itemBuilder: (context, index) =>
                  ListTile(title: Text(results[index])),
            ),
    );
  }
}

How it works:

  • title is shown when search is closed. It is a Widget, so you can use an icon or a logo too.
  • onSearch is called on every change with the current text. When the user closes the search, it is called with an empty string, so the list resets automatically.
  • suggestions is the list shown in the dropdown overlay. Tapping a suggestion fills the search field.
  • showClearSearchIcon adds an ✕ button to clear the field.

Step 3: Load suggestions from an API

For real data, use asyncSuggestions instead of suggestions. It receives the typed text and returns a Future<List<String>>:

Future<List<String>> _fetchSuggestions(String value) async {
  final response = await http.get(
    Uri.parse('https://api.example.com/products?search=$value'),
  );
  // Parse the response into a List<String> of product names.
  return parseNames(response.body);
}
 
// ...
 
appBar: EasySearchBar(
  title: const Text('Products'),
  onSearch: (value) => setState(() => _query = value),
  asyncSuggestions: _fetchSuggestions,
  debounceDuration: const Duration(milliseconds: 500),
  onSuggestionTap: (product) {
    // Open the product page instead of just filling the field.
  },
),

debounceDuration waits until the user stops typing before calling your API (400 ms by default). Without it, typing "pineapple" would send nine requests.

onSuggestionTap replaces the default behaviour (filling the text field) with your own, which is handy when a suggestion should open a detail page.

Step 4: Style it to match your app

EasySearchBar has most of the styling options of a normal AppBar, plus some for the search field:

EasySearchBar(
  title: const Text('Contacts'),
  onSearch: onSearch,
  backgroundColor: Colors.indigo,
  foregroundColor: Colors.white,
  searchBackgroundColor: Colors.white,
  searchCursorColor: Colors.indigo,
  searchHintText: 'Name or phone',
  isFloating: true,
  actions: [
    IconButton(icon: const Icon(Icons.filter_list), onPressed: () {}),
  ],
)

isFloating: true gives you a rounded, floating bar instead of a full-width one. If you want the page content to scroll behind the floating bar, also set extendBodyBehindAppBar: true on the Scaffold and wrap it in a SafeArea.

Alternative: Flutter's built-in SearchAnchor

easy_search_bar has not had a release since 2023. It still works with current Flutter, but if you prefer no extra dependency, Material 3 includes SearchAnchor:

SearchAnchor.bar(
  barHintText: 'Search fruits',
  suggestionsBuilder: (context, controller) {
    final query = controller.text.toLowerCase();
    return fruits
        .where((f) => f.toLowerCase().contains(query))
        .map((f) => ListTile(
              title: Text(f),
              onTap: () => controller.closeView(f),
            ));
  },
)

It opens a full-screen search view on mobile, following Material 3 guidelines. Choose easy_search_bar when you want the classic "title turns into a search field" app bar, and SearchAnchor when you want the Material 3 look with no package.

Common mistakes

  • Wrapping it in an AppBar. That was how version 1.x worked. In 2.x, EasySearchBar replaces AppBar entirely; nesting them breaks the layout.
  • Filtering the original list. Keep the full list unchanged and compute the filtered results from it, as _results does above. If you overwrite the original, deleted items never come back when the search is cleared.
  • Calling an API without debounce. Every keystroke becomes a network request. Keep debounceDuration at 300–500 ms.
  • Forgetting the empty state. Show a "No results" message instead of a blank screen, so users know the search worked.

Wrapping up

  • EasySearchBar replaces your AppBar and gives you an animated search field with suggestions.
  • Use onSearch to filter your list, and asyncSuggestions with a debounce to search an API.
  • For a no-package option, use Flutter's built-in SearchAnchor.

Watch the full walkthrough in the video above, and see more Flutter tutorials.

Keep reading