项目作者: pkmangukiya

项目描述 :
A Flutter search view with auto-complete suggestions, and fully customized with filters.
高级语言: Dart
项目地址: git://github.com/pkmangukiya/flutter_search_view_pk.git
创建时间: 2020-07-04T04:54:33Z
项目社区:https://github.com/pkmangukiya/flutter_search_view_pk

开源协议:

下载


flutter_search_view_pk

A search view with auto-complete suggestions. and Easiest way to search from API

Awesome search view, written in Flutter(Dart), appears search view like Instagram Search view. You can fully customize this repository. You can use this repository with any flutter! project See usage in below

ezgif com-video-to-gif

How to integreat source?

1 : Directly drag and drop the pk_search_bar directory into your Flutter project.

2 : Create a Class SearchScreen and in build > Scaffold > child: SearchBar()

  1. Navigator.push(
  2. context,
  3. MaterialPageRoute(
  4. builder: (context) => SearchScreen()),
  5. );

Example

  1. class SearchScreen extends StatefulWidget {
  2. final List<CountryModel> countryModelList;
  3. @override
  4. _SearchScreenState createState() => _SearchScreenState();
  5. const SearchScreen(this.countryModelList);
  6. }
  7. class _SearchScreenState extends State<SearchScreen> {
  8. @override
  9. void initState() {
  10. // TODO: implement initState
  11. super.initState();
  12. }
  13. @override
  14. Widget build(BuildContext context) {
  15. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  16. return Scaffold(
  17. body: SafeArea(
  18. bottom: false,
  19. child: Container(
  20. child: Padding(
  21. padding: const EdgeInsets.symmetric(horizontal: 5),
  22. child: searchBar(context)),
  23. )),
  24. );
  25. }
  26. // TODO: CountrySearchBar
  27. Widget searchBar(BuildContext context) {
  28. return SearchBar<CountryModel>(
  29. searchBarPadding: EdgeInsets.only(left: 5, right: 5, top: 10, bottom: 5),
  30. headerPadding: EdgeInsets.only(left: 0, right: 0),
  31. listPadding: EdgeInsets.only(left: 0, right: 0),
  32. hintText: "Search Placeholder",
  33. hintStyle: TextStyle(
  34. color: Colors.black54,
  35. ),
  36. textStyle: TextStyle(
  37. color: Colors.black,
  38. fontWeight: FontWeight.normal,
  39. ),
  40. iconActiveColor: Colors.deepPurple,
  41. shrinkWrap: true,
  42. mainAxisSpacing: 5,
  43. crossAxisSpacing: 5,
  44. suggestions: widget.countryModelList,
  45. cancellationWidget: Text("Cancel"),
  46. minimumChars: 1,
  47. // placeHolder: Center(
  48. // child: Padding(
  49. // padding: const EdgeInsets.only(left: 10, right: 10),
  50. // child: Text(searchMessage, textAlign: TextAlign.center, style: CustomTextStyle.textSubtitle1(context).copyWith(fontSize: 14),),
  51. // ),
  52. // ),
  53. emptyWidget: Center(
  54. child: Padding(
  55. padding: const EdgeInsets.only(left: 10, right: 10),
  56. child: Text("There is no any data found"),
  57. ),
  58. ),
  59. onError: (error) {
  60. return Center(
  61. child: Padding(
  62. padding: const EdgeInsets.only(left: 10, right: 10),
  63. child: Text("$error", textAlign: TextAlign.center),
  64. ),
  65. );
  66. },
  67. loader: Center(
  68. child: LoadingIndicator(),
  69. ),
  70. onSearch: getCountrySearchWithSuggestion, /// CountrySearch // if want to search with API then use thi ----> getCountryListFromApi
  71. onCancelled: () {
  72. Navigator.pop(context);
  73. },
  74. buildSuggestion: (CountryModel countryModel, int index) {
  75. return countryGenerateColumn(countryModel, index);
  76. },
  77. onItemFound: (CountryModel countryModel, int index) {
  78. return countryGenerateColumn(countryModel, index);
  79. },
  80. );
  81. }
  82. Widget countryGenerateColumn(CountryModel countryModel, int index) => InkWell(
  83. child: Stack(
  84. children: <Widget>[
  85. Padding(
  86. padding: const EdgeInsets.only(
  87. left: 5.0, top: 5.0, right: 5.0, bottom: 5.0),
  88. child: ConstrainedBox(
  89. constraints: BoxConstraints(
  90. minHeight: 50,
  91. ),
  92. child: Row(
  93. mainAxisAlignment: MainAxisAlignment.start,
  94. children: <Widget>[
  95. Container(
  96. padding: EdgeInsets.fromLTRB(8.0, 5.0, 0.0, 5.0),
  97. width: MediaQuery.of(context).size.width * .60,
  98. color: Colors.transparent,
  99. child: Column(
  100. crossAxisAlignment: CrossAxisAlignment.start,
  101. children: <Widget>[
  102. Text(countryModel.countryName,
  103. maxLines: 1,
  104. overflow: TextOverflow.ellipsis
  105. ),
  106. Text(
  107. countryModel.countryCode,
  108. maxLines: 2,
  109. overflow: TextOverflow.ellipsis,
  110. ),
  111. SizedBox(height: 8),
  112. Divider(height: 0.5)
  113. ],
  114. ),
  115. ),
  116. ],
  117. ),
  118. ),
  119. ),
  120. ],
  121. ),
  122. );
  123. // Future<List<CountryModel>> getCountryListFromApi(String search) async {
  124. // var _param = {
  125. // "search_value":search,
  126. // "user_token": "",
  127. // };
  128. // print("Resident search = $search");
  129. // if (search == "empty") return [];
  130. // if (search == "error") throw Error();
  131. //
  132. // var response = await ApiManager.instance
  133. // .postAPICall(BASE_URL_Local + get_search_country_list, _param, context);
  134. // var data = response["data"];
  135. // List<CountryModel> countryModelList = [];
  136. // for (var u in data) {
  137. // CountryModel countryModel = CountryModel(
  138. // u["countryName"],
  139. // u["countryCode"]
  140. // );
  141. // countryModelList.add(countryModel);
  142. // }
  143. // return countryModelList;
  144. // }
  145. Future<List<CountryModel>> getCountrySearch(String search) async {
  146. print("Resident search = $search");
  147. if (search == "empty") return [];
  148. if (search == "error") throw Error();
  149. List<CountryModel> filterCountryList = [];
  150. widget.countryModelList.forEach((CountryModel) {
  151. if (CountryModel.countryName
  152. .toLowerCase()
  153. .contains(search.toLowerCase()) ||
  154. CountryModel.countryCode
  155. .toLowerCase()
  156. .contains(search.toLowerCase()))
  157. filterCountryList.add(CountryModel);
  158. });
  159. return filterCountryList;
  160. }
  161. Future<List<CountryModel>> getCountrySearchWithSuggestion(
  162. String search) async {
  163. print("Resident search = $search");
  164. if (search == "empty") return [];
  165. if (search == "error") throw Error();
  166. List<CountryModel> filterCountryList = [];
  167. widget.countryModelList.forEach((CountryModel) {
  168. if (CountryModel.countryName
  169. .toLowerCase()
  170. .contains(search.toLowerCase()) ||
  171. CountryModel.countryCode
  172. .toLowerCase()
  173. .contains(search.toLowerCase()))
  174. filterCountryList.add(CountryModel);
  175. });
  176. final suggestionList =
  177. search.isEmpty ? widget.countryModelList : filterCountryList;
  178. return suggestionList;
  179. }
  180. }

Inspired by and just improvement some points and changes https://github.com/smartnsoft/flappy_search_bar