app-starlock/lib/tools/showBottomSheetTool.dart

110 lines
3.1 KiB
Dart
Raw Normal View History

2023-07-10 17:50:31 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
2023-07-18 18:10:57 +08:00
import 'package:flutter_screenutil/flutter_screenutil.dart';
2024-08-19 15:24:14 +08:00
import 'package:get/get.dart';
2023-07-10 17:50:31 +08:00
///2023/7/17
///底部弹出选择器工具类
typedef StringClickCallback = void Function(int selectIndex, Object selectStr);
typedef ArrayClickCallback = void Function(List<int> selecteds, List<dynamic> strData);
typedef DateClickCallback = void Function(dynamic selectDateStr, dynamic selectDate);
class ShowBottomSheetTool {
2024-08-19 15:24:14 +08:00
ShowBottomSheetTool({
this.pickerHeight = 200.0,
this.itemHeight = 45.0,
this.btnColor = Colors.black,
this.titleColor = const Color.fromRGBO(127, 127, 127, 1.0),
this.textFontSize = 16.0
});
2023-07-10 17:50:31 +08:00
//选择器的高度
double pickerHeight;
//单行的高度
double itemHeight;
//按钮颜色
Color btnColor;
//文本颜色
Color titleColor;
//字体大小
double textFontSize;
///单列
void showSingleRowPicker<T>(
BuildContext context, {
2023-07-15 15:11:28 +08:00
required List<T> data,
String? title,
String? cancelTitle,
String? sureTitle,
int? normalIndex,
PickerDataAdapter? adapter,
required StringClickCallback clickCallBack,
2023-07-10 17:50:31 +08:00
}) {
openPicker(context,
2023-07-15 15:11:28 +08:00
title: title!,
cancelTitle: cancelTitle!,
sureTitle: sureTitle!,
2023-07-10 17:50:31 +08:00
selecteds: [normalIndex ?? 0],
adapter: adapter ?? PickerDataAdapter(pickerData: data, isArray: false),
clickCallBack: (Picker picker, List<int> selecteds) {
2023-07-15 15:11:28 +08:00
clickCallBack(selecteds[0], data[selecteds[0]] as Object);
2023-07-10 17:50:31 +08:00
});
}
///多列
void showArrayPicker<T>(
BuildContext context, {
2023-07-15 15:11:28 +08:00
required List<T> data,
String? title,
List<int>? normalIndex,
PickerDataAdapter? adapter,
required ArrayClickCallback clickCallBack,
2023-07-10 17:50:31 +08:00
}) {
openPicker(context,
2023-07-15 15:11:28 +08:00
selecteds: normalIndex!,
title: title!,
2023-07-10 17:50:31 +08:00
adapter: adapter ?? PickerDataAdapter(pickerData: data, isArray: true),
clickCallBack: (Picker picker, List<int> selecteds) {
clickCallBack(selecteds, picker.getSelectedValues());
});
}
void openPicker(
BuildContext context, {
2023-07-15 15:11:28 +08:00
required PickerAdapter adapter,
String? title,
String? cancelTitle,
String? sureTitle,
List<int>? selecteds,
required PickerConfirmCallback clickCallBack,
2023-07-10 17:50:31 +08:00
}) {
Picker(
adapter: adapter,
title: Text(
2024-08-19 15:24:14 +08:00
title ?? '请选择'.tr,
2023-07-10 17:50:31 +08:00
style: TextStyle(
color: titleColor,
fontSize: textFontSize,
),
),
selecteds: selecteds,
2024-08-19 15:24:14 +08:00
confirmText: sureTitle??'确定'.tr,
cancelText: cancelTitle??'取消'.tr,
2023-07-10 17:50:31 +08:00
cancelTextStyle: TextStyle(
color: btnColor,
fontSize: textFontSize,
),
confirmTextStyle: TextStyle(
color: btnColor,
fontSize: textFontSize,
),
textAlign: TextAlign.right,
itemExtent: itemHeight,
height: pickerHeight,
2024-08-19 15:24:14 +08:00
selectedTextStyle: const TextStyle(
2023-07-10 17:50:31 +08:00
color: Colors.black,
),
onConfirm: clickCallBack,
).showModal(context);
}
}