app_dropdown_search.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'package:dropdown_search/dropdown_search.dart';
  2. import 'package:flutter/material.dart';
  3. import '../themes/themes.dart';
  4. class AppDropdownSearch extends StatelessWidget {
  5. final void Function(dynamic)? onChanged;
  6. final dynamic selectedItem;
  7. final String? etiqueta;
  8. final TextEditingController controller;
  9. final String Function(dynamic)? itemAsString;
  10. final Future<List<dynamic>> Function(String)? asyncItems;
  11. final bool enabled;
  12. final List<dynamic> items;
  13. final String? Function(dynamic)? validator;
  14. final dynamic Function()? onPressedClear;
  15. const AppDropdownSearch({
  16. super.key,
  17. required this.controller,
  18. this.itemAsString,
  19. this.asyncItems,
  20. this.etiqueta,
  21. this.selectedItem,
  22. this.onChanged,
  23. this.onPressedClear,
  24. this.validator,
  25. this.enabled = true,
  26. this.items = const [],
  27. });
  28. double _getFontSize(BuildContext context) {
  29. double screenWidth = MediaQuery.of(context).size.width;
  30. if (screenWidth < 480) {
  31. return 12;
  32. } else if (screenWidth < 800) {
  33. return 15;
  34. }
  35. return 16;
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. double fontSize = _getFontSize(context);
  40. TextStyle dropdownTextStyle = TextStyle(fontSize: fontSize);
  41. return Column(
  42. crossAxisAlignment: CrossAxisAlignment.start,
  43. children: [
  44. if (etiqueta != null)
  45. Text(
  46. etiqueta.toString(),
  47. style: TextStyle(fontSize: fontSize, fontWeight: FontWeight.bold),
  48. ),
  49. if (etiqueta != null) const SizedBox(height: 5),
  50. DropdownSearch(
  51. suffixProps: DropdownSuffixProps(
  52. clearButtonProps: ClearButtonProps(
  53. isVisible: true,
  54. icon: const Icon(Icons.clear),
  55. iconSize: 20,
  56. ),
  57. ),
  58. validator: validator,
  59. onChanged: onChanged,
  60. // asyncItems: asyncItems,
  61. // items: items,
  62. itemAsString: itemAsString,
  63. selectedItem: selectedItem,
  64. enabled: enabled,
  65. popupProps: PopupProps.menu(
  66. emptyBuilder: (context, searchEntry) => Center(
  67. child: Column(
  68. mainAxisAlignment: MainAxisAlignment.center,
  69. children: [
  70. Icon(Icons.inbox, size: 60, color: Colors.grey),
  71. const SizedBox(height: 10),
  72. Text(
  73. 'No se encontró información',
  74. style: dropdownTextStyle,
  75. ),
  76. ],
  77. ),
  78. ),
  79. // isFilterOnline: true,
  80. showSearchBox: true,
  81. searchFieldProps: TextFieldProps(
  82. controller: controller,
  83. autofocus: true,
  84. decoration: InputDecoration(
  85. hintText: 'Teclee para buscar',
  86. hintStyle: dropdownTextStyle,
  87. suffixIcon: const Icon(Icons.search, color: Colors.grey),
  88. fillColor: Colors.white,
  89. focusedBorder: UnderlineInputBorder(
  90. borderSide: BorderSide(color: AppTheme.primary),
  91. ),
  92. enabledBorder: const UnderlineInputBorder(
  93. borderSide: BorderSide(color: Colors.grey),
  94. ),
  95. errorBorder: UnderlineInputBorder(
  96. borderSide: BorderSide(
  97. color: Colors.grey[400]!,
  98. ),
  99. ),
  100. focusedErrorBorder: UnderlineInputBorder(
  101. borderSide: BorderSide(
  102. color: AppTheme.primary,
  103. ),
  104. ),
  105. floatingLabelStyle: TextStyle(
  106. color: AppTheme.primary,
  107. fontSize: fontSize,
  108. ),
  109. ),
  110. ),
  111. ),
  112. decoratorProps: DropDownDecoratorProps(
  113. baseStyle: dropdownTextStyle,
  114. decoration: InputDecoration(
  115. fillColor: Colors.white,
  116. suffixIcon: Icon(
  117. Icons.arrow_drop_down_circle,
  118. color: AppTheme.primary,
  119. ),
  120. focusedBorder: OutlineInputBorder(
  121. borderRadius: BorderRadius.circular(10),
  122. borderSide: BorderSide(
  123. color: AppTheme.primary,
  124. ),
  125. ),
  126. enabledBorder: OutlineInputBorder(
  127. borderRadius: BorderRadius.circular(10),
  128. borderSide: BorderSide(
  129. color: Colors.grey[400]!,
  130. ),
  131. ),
  132. errorBorder: OutlineInputBorder(
  133. borderRadius: BorderRadius.circular(10),
  134. borderSide: BorderSide(
  135. color: Colors.grey[300]!,
  136. ),
  137. ),
  138. focusedErrorBorder: OutlineInputBorder(
  139. borderRadius: BorderRadius.circular(10),
  140. borderSide: BorderSide(
  141. color: AppTheme.primary,
  142. ),
  143. ),
  144. disabledBorder: OutlineInputBorder(
  145. borderRadius: BorderRadius.circular(10),
  146. borderSide: BorderSide(
  147. color: Colors.grey[300]!,
  148. ),
  149. ),
  150. ),
  151. ),
  152. ),
  153. ],
  154. );
  155. }
  156. }