Create a Search Experience
Now you have simple app showing a map. In this step, you'll create a simple search and display the search results in a list. You'll also learn how to filter the data displayed on the map based on the search results.
Create a Simple Query Search
Start by creating a new Widget
to facilitate searches on your application. Here we will be using a SearchWidget
for searching with, while using a Drawer
to display the results. We also create a search input field on our Scaffold
for the user to input the text they want to search for. This is already setup in the basic example app.
To perform a search you will need to have initiated MapsIndoors
. This was shown in the previous section of the getting started tutorial how you do this.
For advanced usage of the search functionality read the Search guide and tutorials connected to it: Search Guide, the examples are given for Android and iOS but the parameters and methods are the same in Flutter.
Show a List of Search Results
Create a search method that takes a search string as a parameter in your State
class. In this example we only use the setTake
on the MPFilter
to limit our result to 30 locations. We will expand on this method later.
Next we create our own Widget to act as our search bar, this widget will take a function onSubmitted
as a parameter, this is the function we will use to do the searcing when the user submits their search text.
To be able to search we will use a text input field where a user can write what they want to search for. We can change the AppBar
in our Scaffold
to be the SearchWidget
that we created earlier.
Now, when the user submits their text in the SearchWidget
it will call the search
function which we created earlier.
We now need some UI that can show the result of our search, for this we will use a Drawer
which we can attach to our Scaffold
. As the drawer is built directly in the build tree we can use a shared variable _searchResults
which we will populate when a search is performed.
Now that we have implemented the Drawer
UI. Now we can add its activation to the search query method. We do this by putting the locations returned from our search into the _searchResults
variable and then opening the Drawer
from our Scaffold
.
See the full example of the search method here: main.dart
Filter Locations on Map Based on Search Results
When getting a search result, you might want to only show those search results on the map. You can do this through calling setFilterWithLocations
on MapsIndoorsWidget
. This method has different parameters to make it easier for you as a developer to fit your exact need in terms of animation and more.
The standard implementation animates the camera to fit all Locations on the map and show the info window of a Location, if it's a list of only one Location.
When you are done showing the search results you can call clearFliter
.
Expected result:
The accompanying UI and implementation of this search experience can be found in the getting started app sample. Getting Started App sample
Last updated