single_select.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: 24,
  39. child: DropdownButtonHideUnderline(
  40. child: DropdownButton2<String>(
  41. dropdownMaxHeight: 280,
  42. buttonWidth: PtToPxConverter.ptToPx(singleSelected.lineWidth),
  43. itemHeight: 28,
  44. itemPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  45. isExpanded: false,
  46. dropdownOverButton: false,
  47. scrollbarAlwaysShow: true,
  48. scrollbarThickness: 4,
  49. scrollbarRadius: const Radius.circular(2),
  50. buttonPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  51. dropdownDecoration: BoxDecoration(
  52. borderRadius: BorderRadius.circular(4),
  53. ),
  54. buttonDecoration: buttonDecoration,
  55. offset: const Offset(0, -4),
  56. selectedItemHighlightColor: Theme.of(context).secondaryHeaderColor,
  57. selectedItemBuilder: (_) => _items!
  58. .map((e) => _OptionRow(
  59. singleSelected,
  60. e,
  61. height: 22,
  62. ))
  63. .toList(),
  64. value: _value.isEmpty ? null : _value,
  65. items: buildItems(),
  66. onChanged: (v) {
  67. setState(() {
  68. _value = v!;
  69. });
  70. },
  71. ),
  72. ),
  73. );
  74. }
  75. List<DropdownMenuItem<String>> buildItems() {
  76. return _items!.map((String e) {
  77. final active = e == _value;
  78. return DropdownMenuItem<String>(
  79. child: _OptionRow(singleSelected, e, isActive: active),
  80. value: e,
  81. );
  82. }).toList();
  83. }
  84. }
  85. class _OptionRow extends StatelessWidget {
  86. _OptionRow(
  87. this.singleSelected,
  88. this.label, {
  89. Key? key,
  90. this.isActive = false,
  91. this.height,
  92. }) : super(key: key);
  93. SingleSelected singleSelected;
  94. final String label;
  95. final bool isActive;
  96. final double? height;
  97. @override
  98. Widget build(BuildContext context) {
  99. final color = singleSelected.fontColor!;
  100. final background = singleSelected.background!;
  101. final text = Text(
  102. label,
  103. style: TextStyle(
  104. backgroundColor: Color.fromARGB(
  105. background.a!, background.r!, background.g!, background.b!),
  106. fontSize: PtToPxConverter.ptToPx(singleSelected.fontSize),
  107. color: Color.fromARGB(color.a!, color.r!, color.g!, color.b!),
  108. ),
  109. );
  110. if (height != null) {
  111. return Container(
  112. alignment: Alignment.centerLeft,
  113. height: height,
  114. child: text,
  115. );
  116. } else {
  117. return text;
  118. }
  119. }
  120. }