select.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import 'package:cool_dropdown/cool_dropdown.dart';
  2. import 'package:cool_dropdown/models/cool_dropdown_item.dart';
  3. import 'package:flutter/material.dart';
  4. class VSelectKV<T> {
  5. final String label;
  6. final T value;
  7. VSelectKV(this.label, this.value);
  8. }
  9. class VSelect<T, TValue> extends StatelessWidget {
  10. final List<T> source;
  11. final TValue? value;
  12. final String Function(T data) labelGetter;
  13. final TValue Function(T data) valueGetter;
  14. final ValueChanged<TValue>? onChanged;
  15. const VSelect({
  16. super.key,
  17. required this.source,
  18. this.value,
  19. required this.labelGetter,
  20. required this.valueGetter,
  21. this.onChanged,
  22. });
  23. @override
  24. Widget build(BuildContext context) {
  25. CoolDropdownItem<TValue>? defaultItem;
  26. List<CoolDropdownItem<TValue>> items = source.map((e) {
  27. final itemValue = valueGetter(e);
  28. bool isSelected = value == itemValue;
  29. final item = CoolDropdownItem<TValue>(
  30. label: labelGetter(e),
  31. value: itemValue,
  32. isSelected: isSelected,
  33. );
  34. if (isSelected) {
  35. defaultItem = item;
  36. }
  37. return item;
  38. }).toList();
  39. final controller = DropdownController(
  40. duration: const Duration(milliseconds: 200),
  41. );
  42. final primaryColor = Theme.of(context).primaryColor;
  43. final secondaryColor = Theme.of(context).secondaryHeaderColor;
  44. const borderSide = BorderSide(width: 1, color: Colors.grey);
  45. final openBorderSide = BorderSide(width: 1, color: primaryColor);
  46. return CoolDropdown<TValue>(
  47. defaultItem: defaultItem,
  48. resultOptions: ResultOptions(
  49. boxDecoration: const BoxDecoration(
  50. color: Colors.white,
  51. borderRadius: BorderRadius.zero,
  52. border: Border(
  53. top: borderSide,
  54. bottom: borderSide,
  55. left: borderSide,
  56. right: borderSide,
  57. ),
  58. ),
  59. openBoxDecoration: BoxDecoration(
  60. color: Colors.white,
  61. borderRadius: BorderRadius.zero,
  62. border: Border(
  63. top: openBorderSide,
  64. bottom: openBorderSide,
  65. left: openBorderSide,
  66. right: openBorderSide,
  67. ),
  68. boxShadow: const [
  69. BoxShadow(
  70. color: Color(0x1a9E9E9E),
  71. spreadRadius: 1,
  72. blurRadius: 10,
  73. offset: Offset(0, 1),
  74. ),
  75. ],
  76. ),
  77. ),
  78. dropdownOptions: const DropdownOptions(
  79. animationType: DropdownAnimationType.size,
  80. borderRadius: BorderRadius.zero,
  81. duration: Duration(milliseconds: 200),
  82. ),
  83. dropdownItemOptions: DropdownItemOptions(
  84. selectedTextStyle: TextStyle(
  85. color: primaryColor,
  86. fontSize: 16,
  87. fontWeight: FontWeight.w400,
  88. ),
  89. selectedBoxDecoration: BoxDecoration(
  90. color: secondaryColor,
  91. borderRadius: BorderRadius.zero,
  92. ),
  93. ),
  94. dropdownList: items,
  95. controller: controller,
  96. onChange: (TValue value) {
  97. try {
  98. onChanged?.call(value);
  99. } catch (e) {}
  100. controller.close();
  101. },
  102. );
  103. }
  104. }