single_select.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  2. import 'package:fis_lib_report/report/singleSelected.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:dropdown_button2/dropdown_button2.dart';
  5. import 'package:fis_ui/simple/select.dart';
  6. class RSingleSelected extends StatefulWidget {
  7. SingleSelected singleSelected;
  8. RSingleSelected(this.singleSelected);
  9. @override
  10. State<StatefulWidget> createState() {
  11. return _RSingleSelectState(singleSelected);
  12. }
  13. }
  14. class _RSingleSelectState extends State<RSingleSelected> {
  15. SingleSelected singleSelected;
  16. _RSingleSelectState(this.singleSelected);
  17. String _value = '';
  18. List<String>? _items = [];
  19. @override
  20. initState() {
  21. if (singleSelected.items!.isNotEmpty) {
  22. _items = singleSelected.items;
  23. }
  24. super.initState();
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. if (_items!.isEmpty) {
  29. return const SizedBox();
  30. }
  31. var buttonDecoration = BoxDecoration(
  32. border: Border.all(color: Colors.grey, width: 0.8),
  33. // border: Border.all(color: Colors.grey[200]!, width: 0.5),
  34. borderRadius: BorderRadius.circular(0),
  35. color: Colors.white,
  36. );
  37. return SizedBox(
  38. height: 28,
  39. child: DropdownButtonHideUnderline(
  40. child: DropdownButton2<String>(
  41. dropdownMaxHeight: 280,
  42. buttonWidth: PtToPxConverter.ptToPx(singleSelected.lineWidth),
  43. buttonHeight: 28,
  44. itemHeight: 28,
  45. itemPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  46. isExpanded: false,
  47. dropdownOverButton: false,
  48. scrollbarAlwaysShow: true,
  49. scrollbarThickness: 4,
  50. scrollbarRadius: const Radius.circular(2),
  51. buttonPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  52. dropdownDecoration: BoxDecoration(
  53. borderRadius: BorderRadius.circular(4),
  54. ),
  55. buttonDecoration: buttonDecoration,
  56. offset: const Offset(0, -4),
  57. selectedItemHighlightColor: Theme.of(context).secondaryHeaderColor,
  58. selectedItemBuilder: (_) => _items!
  59. .map((e) => _OptionRow(
  60. singleSelected,
  61. e,
  62. height: 22,
  63. ))
  64. .toList(),
  65. value: _value.isEmpty ? null : _value,
  66. items: buildItems(),
  67. onChanged: (v) {
  68. setState(() {
  69. _value = v!;
  70. });
  71. },
  72. ),
  73. ),
  74. );
  75. }
  76. List<DropdownMenuItem<String>> buildItems() {
  77. return _items!.map((String e) {
  78. final active = e == _value;
  79. return DropdownMenuItem<String>(
  80. child: _OptionRow(singleSelected, e, isActive: active),
  81. value: e,
  82. );
  83. }).toList();
  84. }
  85. }
  86. class _OptionRow extends StatelessWidget {
  87. _OptionRow(
  88. this.singleSelected,
  89. this.label, {
  90. Key? key,
  91. this.isActive = false,
  92. this.height,
  93. }) : super(key: key);
  94. SingleSelected singleSelected;
  95. final String label;
  96. final bool isActive;
  97. final double? height;
  98. @override
  99. Widget build(BuildContext context) {
  100. final color = singleSelected.fontColor!;
  101. final background = singleSelected.background!;
  102. final text = Text(
  103. label,
  104. style: TextStyle(
  105. backgroundColor: Color.fromARGB(
  106. background.a!, background.r!, background.g!, background.b!),
  107. fontSize: PtToPxConverter.ptToPx(singleSelected.fontSize),
  108. color: Color.fromARGB(color.a!, color.r!, color.g!, color.b!),
  109. ),
  110. );
  111. if (height != null) {
  112. return Container(
  113. alignment: Alignment.centerLeft,
  114. height: height,
  115. child: text,
  116. );
  117. } else {
  118. return text;
  119. }
  120. }
  121. }