option_pick_util.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  4. import 'package:get/get.dart';
  5. import '../my_text_view.dart';
  6. import '../utils/dark_theme_util.dart';
  7. import 'package:cs_resources/constants/color_constants.dart';
  8. class OptionPickerUtil {
  9. static void showCupertinoOptionPicker({
  10. required List<String> items,
  11. required Function(List<String>, int) onPickerChanged,
  12. int? initialSelectIndex = 0,
  13. String? title,
  14. String? confirmTextStr,
  15. String? cancelTextStr,
  16. Function()? onReset,
  17. }) {
  18. SmartDialog.show(
  19. usePenetrate: false,
  20. tag: 'data_picker',
  21. maskColor: Colors.black.withOpacity(0.4),
  22. alignment: Alignment.bottomCenter,
  23. builder: (BuildContext context) {
  24. return Container(
  25. height: 280,
  26. decoration: BoxDecoration(
  27. color: DarkThemeUtil.multiColors(Colors.white, darkColor: ColorConstants.darkBlackItem),
  28. borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20)),
  29. ),
  30. child: Column(
  31. children: [
  32. Row(
  33. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  34. children: [
  35. CupertinoButton(
  36. child: Text(
  37. cancelTextStr ?? 'Cancel'.tr,
  38. style: TextStyle(
  39. fontSize: 16,
  40. fontWeight: FontWeight.w600,
  41. color: ColorConstants.appBlue,
  42. ),
  43. ),
  44. onPressed: onReset ??
  45. () {
  46. SmartDialog.dismiss(tag: 'data_picker');
  47. },
  48. ),
  49. Text(
  50. title ?? "",
  51. style: TextStyle(
  52. fontSize: 18,
  53. fontWeight: FontWeight.w500,
  54. color: DarkThemeUtil.multiColors(Colors.black, darkColor: Colors.white),
  55. ),
  56. ),
  57. CupertinoButton(
  58. child: Text(
  59. confirmTextStr ?? 'Confirm'.tr,
  60. style: TextStyle(
  61. fontSize: 16,
  62. fontWeight: FontWeight.w600,
  63. color: ColorConstants.appBlue,
  64. ),
  65. ),
  66. onPressed: () {
  67. SmartDialog.dismiss(tag: 'data_picker');
  68. // 将选中的值通过回调函数传递出去
  69. onPickerChanged(items, initialSelectIndex!);
  70. },
  71. ),
  72. ],
  73. ),
  74. const Divider(color: const Color(0x4dd9d9d9), height: 0.5),
  75. Expanded(
  76. child: Container(
  77. alignment: Alignment.center,
  78. child: CupertinoPicker(
  79. scrollController: FixedExtentScrollController(initialItem: initialSelectIndex ?? 0),
  80. itemExtent: 45,
  81. diameterRatio: 1,
  82. selectionOverlay: const CupertinoPickerDefaultSelectionOverlay(
  83. capStartEdge: false,
  84. capEndEdge: false,
  85. ),
  86. onSelectedItemChanged: (int value) {
  87. initialSelectIndex = value;
  88. },
  89. children: items
  90. .map((item) => Container(
  91. alignment: Alignment.center,
  92. child: MyTextView(
  93. item.toString(),
  94. textAlign: TextAlign.center,
  95. textColor: DarkThemeUtil.multiColors(
  96. const Color(0XFF404A5B),
  97. darkColor: Colors.white,
  98. ),
  99. fontSize: 16,
  100. isFontBold: true,
  101. ),
  102. ))
  103. .toList(),
  104. ),
  105. )),
  106. ],
  107. ),
  108. );
  109. },
  110. );
  111. }
  112. }