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