A search bar is an essential UI component in modern mobile applications, enabling users to filter and find content efficiently. In Flutter, implementing a search bar can be done using Material UI components, custom widgets, and state management techniques. This guide will cover everything from adding a simple search bar to advanced implementations using SearchAnchor
, toggles, and RxDart.
How Do I Put a Search Bar in Flutter?
To add a basic search bar in Flutter, you can use the TextField
widget inside an AppBar
:
AppBar(
title: TextField(
decoration: InputDecoration(
hintText: 'Search...',
border: InputBorder.none,
prefixIcon: Icon(Icons.search),
),
),
)
This approach integrates a simple search bar within the app’s navigation bar. Learn more from Flutter’s official documentation.
How Do I Make a Search Bar in Material UI?
Flutter’s Material UI provides SearchAnchor
, which is useful for implementing search functionality.
SearchAnchor(
builder: (context, controller) => TextField(
controller: controller,
decoration: InputDecoration(
hintText: 'Search...',
border: OutlineInputBorder(),
),
),
)
Learn more about Flutter’s Material UI and customization in our Mastering Modal Bottom Sheet Guide and from the official Material Design guidelines.
What is the Height of the Search Bar in Flutter?
By default, TextField
in an AppBar
adjusts its height automatically. However, you can specify a custom height using SizedBox
or Container
:
SizedBox(
height: 50,
child: TextField(
decoration: InputDecoration(hintText: 'Search...'),
),
)
What is a Search Anchor in Flutter?
SearchAnchor
is a Flutter widget that provides built-in search functionality by offering a dropdown list of search suggestions. Learn more from Flutter’s API reference.
How Do I Add My Search Bar?
You can create a persistent search bar using the SliverAppBar
widget:
SliverAppBar(
floating: true,
title: TextField(
decoration: InputDecoration(hintText: 'Search...'),
),
)
How Do You Add a Bar in Flutter?
Adding a bar, such as a BottomNavigationBar
or AppBar
, can be done using Flutter’s Scaffold
:
Scaffold(
appBar: AppBar(title: Text('My App')),
bottomNavigationBar: BottomNavigationBar(items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
]),
)
How Do I Put the Search Bar Above?
To place a search bar at the top of the screen, use a Column
:
Column(
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(hintText: 'Search...'),
),
),
],
)
How Wide Should a Search Bar Be?
A search bar should generally take most of the screen width. Use MediaQuery
for a responsive width:
SizedBox(
width: MediaQuery.of(context).size.width * 0.9,
child: TextField(
decoration: InputDecoration(hintText: 'Search...'),
),
)
How to Create a Search Button in Flutter?
A search button can trigger a search function when clicked:
IconButton(
icon: Icon(Icons.search),
onPressed: () {
print('Search triggered');
},
)
What is Toggle in Flutter?
A toggle, such as Switch
, allows users to enable or disable search filters:
Switch(
value: true,
onChanged: (bool newValue) {
print('Toggle changed: $newValue');
},
)
What is RxDart in Flutter?
RxDart is a reactive programming library for Flutter that helps with managing search input streams. Here’s an example:
final searchSubject = BehaviorSubject<String>();
TextField(
onChanged: (query) => searchSubject.add(query),
)
Learn more about RxDart from the official RxDart GitHub repository.
To improve your Flutter development experience, check out our article on Best Visual Studio Code Extensions for Flutter Development and visit the Flutter community on Medium.
Final Thoughts
A search bar is a vital component in any app. Whether using SearchAnchor
, TextField
, or advanced solutions like RxDart, Flutter provides flexible options to integrate search functionality. Experiment with different styles and customize them to fit your UI.
For more inspiration, check out Dribbble’s Flutter UI designs.
Let us know in the comments if you have any questions or need further guidance!