app_dropdown_modelo.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'package:flutter/material.dart';
  2. import '../themes/themes.dart';
  3. class AppDropdownModel<T> extends StatelessWidget {
  4. final String? etiqueta;
  5. final String? hint;
  6. final T? selectedValue;
  7. final void Function(T?)? onChanged;
  8. final List<DropdownMenuItem<T>>? items;
  9. const AppDropdownModel({
  10. super.key,
  11. this.etiqueta,
  12. this.hint,
  13. this.selectedValue,
  14. this.onChanged,
  15. required this.items,
  16. });
  17. double _getFontSize(BuildContext context) {
  18. double screenWidth = MediaQuery.of(context).size.width;
  19. if (screenWidth < 480) {
  20. return 12;
  21. } else if (screenWidth < 800) {
  22. return 15;
  23. }
  24. return 18;
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. double fontSize = _getFontSize(context);
  29. TextStyle dropdownTextStyle = TextStyle(fontSize: fontSize);
  30. return Column(
  31. crossAxisAlignment: CrossAxisAlignment.start,
  32. children: [
  33. if (etiqueta != null &&
  34. etiqueta!
  35. .isNotEmpty) // Verifica si la etiqueta no es nula y no está vacía
  36. Text(
  37. etiqueta!,
  38. style: TextStyle(
  39. fontSize: fontSize,
  40. fontWeight: FontWeight.bold,
  41. ),
  42. ),
  43. if (etiqueta != null &&
  44. etiqueta!.isNotEmpty) // Solo muestra el espacio si hay una etiqueta
  45. const SizedBox(
  46. height: 5,
  47. ),
  48. Container(
  49. decoration: BoxDecoration(
  50. color: Colors.white, borderRadius: BorderRadius.circular(15)),
  51. child: DropdownButtonFormField(
  52. hint: Text(hint ?? '', style: dropdownTextStyle),
  53. style: dropdownTextStyle,
  54. borderRadius: BorderRadius.circular(10),
  55. icon: Icon(
  56. Icons.arrow_drop_down_circle,
  57. color: AppTheme.primary,
  58. ),
  59. decoration: InputDecoration(
  60. floatingLabelStyle: TextStyle(
  61. color: AppTheme.primary,
  62. fontSize: fontSize,
  63. ),
  64. labelStyle: TextStyle(color: Colors.grey, fontSize: fontSize),
  65. focusedBorder: OutlineInputBorder(
  66. borderRadius: BorderRadius.circular(15),
  67. borderSide: BorderSide(color: AppTheme.tertiary),
  68. ),
  69. enabledBorder: OutlineInputBorder(
  70. borderRadius: BorderRadius.circular(15),
  71. borderSide: const BorderSide(color: Colors.grey),
  72. ),
  73. ),
  74. value: selectedValue,
  75. items: items,
  76. onChanged: onChanged,
  77. ),
  78. ),
  79. ],
  80. );
  81. }
  82. }