Buy Me A Coffee

DropdownSearch package

Simple and robust DropdownSearch with item search feature, making it possible to use an offline item list or filtering URL for easy customization.

ATTENTION

If you use rxdart in your project in a version lower than 0.23.x, use version 0.1.7+1 of this package. Otherwise, you can use the most current version!

packages.yaml

searchable_dropdown: <lastest version>

Import

import 'package:searchable_dropdown/searchableDropdown.dart';

Simple implementation

DropdownSearch(
  items: ["Brasil", "Itália", "Estados Undos", "Canadá"],
  label: "País",
  onChanged: print,
  selectedItem: "Brasil",
);

customize showed field (itemAsString)

DropdownSearch<UserModel>(
  label: "Nome",
  onFind: (String filter) => getData(filter),
  itemAsString: UserModel.userAsStringByName,
  searchBoxDecoration: InputDecoration(
    hintText: "Search",
    border: OutlineInputBorder(),
  ),
  onChanged: (UserModel data) => print(data),
),

DropdownSearch<UserModel>(
  label: "Nome",
  onFind: (String filter) => getData(filter),
  itemAsString: UserModel.userAsStringById,
  searchBoxDecoration: InputDecoration(
    hintText: "Search",
    border: OutlineInputBorder(),
  ),
  onChanged: (UserModel data) => print(data),
),

Validation

DropdownSearch(
  items: ["Brazil", "France", "Tunisia", "Canada"],
  label: "Country",
  onChanged: print,
  selectedItem: "Tunisia",
  validate: (String item) {
    if (item == null)
      return "Required field";
    else if (item == "Brazil")
      return "Invalid item";
    else
      return null;
  },
);

Endpoint implementation (using Dio package)

DropdownSearch<UserModel>(
  label: "Nome",
  onFind: (String filter) async {
    var response = await Dio().get(
        "http://5d85ccfb1e61af001471bf60.mockapi.io/user",
        queryParameters: {"filter": filter},
    );
    var models = UserModel.fromJsonList(response.data);
    return models;
  },
  onChanged: (UserModel data) {
    print(data);
  },
);

Layout customization

You can customize the layout of the DropdownSearch and its items. EXAMPLE

To customize the DropdownSearch, we have the dropdownBuilder property, which takes a function with the parameters:

  • BuildContext context: current context;
  • T item: Current item, where T is the type passed in the DropdownSearch constructor.

To customize the items, we have the dropdownItemBuilder property, which takes a function with the parameters:

  • BuildContext context: current context;
  • T item: Current item, where T is the type passed in the DropdownSearch constructor.
  • bool isSelected: Boolean that tells you if the current item is selected.

Attention

To use a template as an item type, you need to implement toString, equals and hashcode, as shown below:

class UserModel {
  final String id;
  final DateTime createdAt;
  final String name;
  final String avatar;

  UserModel({this.id, this.createdAt, this.name, this.avatar});

  //this method will prevent the override of toString and make the same model useful for different cases
    static String userAsStringByName(UserModel userModel){
      return '#${userModel.id} ${userModel.name}';
    }

    //this method will prevent the override of toString
    static String userAsStringById(UserModel userModel){
      return '#${userModel.id} ${userModel.id}';
    }


  @override
  String toString() => name;

  @override
  operator ==(o) => o is UserModel && o.id == id;

  @override
  int get hashCode => id.hashCode^name.hashCode^createdAt.hashCode;

}

View more Examples

Libraries

selectDialog