dict_label.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import 'package:flutter/material.dart';
  2. import 'package:chicken_farm/stores/dict_stroe.dart';
  3. import 'package:chicken_farm/modes/system/dict.dart';
  4. class DictLabel extends StatelessWidget {
  5. final String dictType;
  6. final String value;
  7. const DictLabel({
  8. Key? key,
  9. required this.dictType,
  10. required this.value,
  11. }) : super(key: key);
  12. @override
  13. Widget build(BuildContext context) {
  14. return FutureBuilder<DictDataModel?>(
  15. future: DictStore().getDictByTypeAndValue(dictType, value),
  16. builder: (context, snapshot) {
  17. if (snapshot.connectionState == ConnectionState.waiting) {
  18. return const SizedBox(
  19. width: 16,
  20. height: 16,
  21. child: CircularProgressIndicator(strokeWidth: 2),
  22. );
  23. }
  24. if (snapshot.hasData && snapshot.data != null) {
  25. DictDataModel dict = snapshot.data!;
  26. Color backgroundColor = _getColorByListClass(dict.listClass);
  27. Color textColor = _getTextColorByListClass(dict.listClass);
  28. return Container(
  29. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
  30. decoration: BoxDecoration(
  31. color: backgroundColor,
  32. borderRadius: BorderRadius.circular(4),
  33. ),
  34. child: Text(
  35. dict.dictLabel,
  36. style: TextStyle(
  37. color: textColor,
  38. fontSize: 12,
  39. fontWeight: FontWeight.w400,
  40. ),
  41. ),
  42. );
  43. }
  44. return Container(
  45. padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
  46. decoration: BoxDecoration(
  47. color: Colors.grey[200],
  48. borderRadius: BorderRadius.circular(4),
  49. ),
  50. child: Text(
  51. value,
  52. style: const TextStyle(
  53. color: Colors.grey,
  54. fontSize: 12,
  55. fontWeight: FontWeight.w400,
  56. ),
  57. ),
  58. );
  59. },
  60. );
  61. }
  62. Color _getColorByListClass(String? listClass) {
  63. switch (listClass) {
  64. case 'primary':
  65. return Colors.blue.withOpacity(0.1);
  66. case 'success':
  67. return Colors.green.withOpacity(0.1);
  68. case 'danger':
  69. return Colors.red.withOpacity(0.1);
  70. case 'warning':
  71. return Colors.orange.withOpacity(0.1);
  72. case 'info':
  73. return Colors.cyan.withOpacity(0.1);
  74. default:
  75. return Colors.grey.withOpacity(0.1);
  76. }
  77. }
  78. Color _getTextColorByListClass(String? listClass) {
  79. switch (listClass) {
  80. case 'primary':
  81. return Colors.blue;
  82. case 'success':
  83. return Colors.green;
  84. case 'danger':
  85. return Colors.red;
  86. case 'warning':
  87. return Colors.orange;
  88. case 'info':
  89. return Colors.cyan;
  90. default:
  91. return Colors.grey;
  92. }
  93. }
  94. }