flutter_search bar
a simple and mostly automatic material search bar for flutter (dart).
note: use flutter_search_bar and not search_bar — i own both packages but i’m just a tad bit locked out of search_bar, so it won’t be updated.
screenshots
normal state (search is not active yet, only title
and actions
are set, with the only action being a search button)
inbar set to false (background white, back button inherited):
inbar set to true (background inherited):
usage
a simple usage example:
class _myhomepagestate extends state<myhomepage> {
searchbar searchbar;
appbar buildappbar(buildcontext context) {
return new appbar(
title: new text('my home page'),
actions: [searchbar.getsearchaction(context)]
);
}
_myhomepagestate() {
searchbar = new searchbar(
inbar: false,
setstate: setstate,
onsubmitted: print,
builddefaultappbar: buildappbar
);
}
@override
widget build(buildcontext context) {
return new scaffold(
appbar: searchbar.build(context)
);
}
}
this will create an appbar with a search button as its only action, and on press the appbar will turn white and have a textfield inside, allowing user input. once the user has input something and pressed the “enter” button on their keyboard, it will close and the value will be printed to the debugger.
searchbar vs flutter’s showsearch + searchdelegate
using searchbar
essentially, this class returns two different app bars based on whether search is active. colors from the most recently built “default” app bar are used to color the search bar.
i may refer to the two different app bars as default and search. default is, as may be inferred, the default app bar. it shows up when you open your app, and is the “default state”.
once its search button has been pressed, the search app bar appears, where the user can put in input and submit a field.
this documentation may be slightly outdated. it will soon be generated with dartdoc instead of being manual (not really sure why it wasn’t that way from the start).
typedefs
appbarcallback
typedef appbar appbarcallback(buildcontext context);
this should take buildcontext and return an appbar.
textfieldsubmitcallback
typedef void textfieldsubmitcallback(string value);
this should take the value of the input field and return nothing.
setstatecallback
typedef void setstatecallback(fn);
this should take a function and return nothing. generally, this should just be setstate
. more below.
constructor
bool inbar – whether the search should take place “in the existing search bar”, meaning whether it has the same background as the appbar or a flipped (white) one (which also has a colored back button if necessary). defaults to true.
bool colorbackbutton – whether the back button in the search bar should be colored, if false it will be colors.grey.shade400
. defaults to true.
bool closeonsubmit – whether the search bar should close once it has been submitted. you should probably keep this on, and it defaults to true anyways.
string hinttext – the hint text for the textfield that appears when the search button is pressed. defaults to just ‘search’.
appbarcallback builddefaultappbar – the function to be called each time the default app bar is built. the colors from the most recent appbar build will be used for the next search app bar.
textfieldsubmitcallback onsubmitted – a void callback called when the search bar is submitted.
setstatecallback setstate – this is called every time the state needs to be updated (i.e. when the appbar changes).
you can literally just pass setstate
to this, unless for some reason you want to do extra stuff each time the appbar changes.
properties
in addition to all of the above properties set in the constructor:
bool _issearching – whether search is active.
appbar _defaultappbar – the last built default app bar.
methods
getsearchaction
iconbutton getsearchaction(buildcontext context)
this takes context
as an argument, and returns an iconbutton suitable for an action in an appbar. if you want your searchbar to actually work, put this inside your builddefaultappbar
pub method.
build
appbar build(buildcontext context)
this takes context
as an argument, and returns an appbar based on whether search is active.
Comments are closed.