Coder Social home page Coder Social logo

uttusharma / flutter-search-bar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arcticzeroo/flutter-search-bar

0.0 0.0 0.0 194 KB

(mostly) Automatic search-enabled appBar for flutter

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%

flutter-search-bar's Introduction

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)

Normal State

inBar set to false (background white, back button inherited):

inBar false

inBar set to true (background inherited):

inBar true

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 buildDefaultAppBarpub method.

build

AppBar build(BuildContext context)

This takes context as an argument, and returns an AppBar based on whether search is active.

Features and bugs

Please file feature requests and bugs at the issue tracker.

flutter-search-bar's People

Contributors

arcticzeroo avatar bcko avatar chances avatar cmaster11 avatar dineshba avatar gozeloglu avatar grouslan avatar klisiewicz avatar orischwartz avatar ride4sun avatar saltypandastudios avatar sidrao2006 avatar urmatt avatar victorpimentel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.