app_dropdown.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:flutter/material.dart';
  2. import 'package:japp_flutter/core/models/dropdown_item.dart';
  3. class AppDropdown<T> extends StatelessWidget {
  4. final String label;
  5. final T? value;
  6. final List<DropdownItem<T>> items; // 改为接收通用列表
  7. final ValueChanged<T?> onChanged;
  8. final FormFieldValidator<T>? validator;
  9. final String? hintText;
  10. final bool isExpanded;
  11. final Widget? prefixIcon;
  12. final InputBorder? border;
  13. final TextStyle? textStyle;
  14. final double? iconSize;
  15. final Color? iconColor;
  16. const AppDropdown({
  17. super.key,
  18. required this.label,
  19. required this.value,
  20. required this.items,
  21. required this.onChanged,
  22. this.validator,
  23. this.hintText,
  24. this.isExpanded = true,
  25. this.prefixIcon,
  26. this.border,
  27. this.textStyle,
  28. this.iconSize = 20.0,
  29. this.iconColor,
  30. });
  31. @override
  32. Widget build(BuildContext context) {
  33. return DropdownButtonFormField<T>(
  34. decoration: InputDecoration(
  35. labelText: label,
  36. hintText: hintText,
  37. border: border ?? const OutlineInputBorder(),
  38. prefixIcon: prefixIcon,
  39. ),
  40. value: value,
  41. items: items.map((item) {
  42. return DropdownMenuItem<T>(
  43. value: item.value,
  44. child: Row(
  45. children: [
  46. if (item.icon != null)
  47. Icon(
  48. item.icon,
  49. size: iconSize,
  50. color: item.iconColor ?? iconColor,
  51. ),
  52. if (item.icon != null) const SizedBox(width: 8),
  53. Text(
  54. item.label,
  55. style: textStyle ?? Theme.of(context).textTheme.bodyMedium,
  56. ),
  57. ],
  58. ),
  59. );
  60. }).toList(),
  61. onChanged: onChanged,
  62. validator: validator,
  63. isExpanded: isExpanded,
  64. );
  65. }
  66. }