2024-04-09 08:31:17 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
2024-04-09 11:44:55 +08:00
|
|
|
import 'package:sk_base_mobile/apis/api.dart';
|
2024-04-09 08:31:17 +08:00
|
|
|
import 'package:sk_base_mobile/app_theme.dart';
|
2024-04-09 11:44:55 +08:00
|
|
|
import 'package:sk_base_mobile/models/dept.model.dart';
|
|
|
|
import 'package:sk_base_mobile/util/logger_util.dart';
|
2024-04-09 08:31:17 +08:00
|
|
|
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
|
|
|
|
import 'package:sk_base_mobile/widgets/core/sk_cascade_picker.dart';
|
|
|
|
|
|
|
|
class DeptPicker extends StatelessWidget {
|
2024-04-10 11:04:27 +08:00
|
|
|
final Function(CascadeItem<int, DeptModel>)? onSelected;
|
2024-04-09 11:44:55 +08:00
|
|
|
DeptPicker({super.key, this.onSelected});
|
2024-04-09 08:31:17 +08:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-09 11:44:55 +08:00
|
|
|
return SkCascadePicker<int, DeptModel>(
|
|
|
|
getData: getData,
|
|
|
|
initialPageData: (cascadeController) =>
|
|
|
|
cascadeController.treeData.toList(),
|
|
|
|
nextPageData: (pageCallback, currentPage, selectIndex, currentPageItem,
|
|
|
|
cascadeController) async {
|
|
|
|
if (currentPageItem.children != null) {
|
|
|
|
List<CascadeItem<int, DeptModel>>? nextPageData = currentPageItem
|
|
|
|
.children!
|
|
|
|
.map<CascadeItem<int, DeptModel>>((e) =>
|
|
|
|
CascadeItem(label: e.name, value: e.id, children: e.children))
|
|
|
|
.toList();
|
|
|
|
if (nextPageData.isNotEmpty) pageCallback(nextPageData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onConfirm: (List<CascadeItem<int, DeptModel>> value) {
|
|
|
|
if (onSelected != null && value.isNotEmpty) {
|
2024-04-10 11:04:27 +08:00
|
|
|
onSelected!(value.last);
|
2024-04-09 11:44:55 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
maxPageNum: 10,
|
|
|
|
selectedIcon: const Icon(Icons.check, color: AppTheme.primaryColorLight),
|
|
|
|
);
|
2024-04-09 08:31:17 +08:00
|
|
|
}
|
|
|
|
|
2024-04-09 11:44:55 +08:00
|
|
|
Future<List<CascadeItem<int, DeptModel>>> getData() async {
|
|
|
|
try {
|
|
|
|
final res = await Api.getDepts();
|
|
|
|
if (res.data != null) {
|
|
|
|
List<CascadeItem<int, DeptModel>> result =
|
|
|
|
res.data.map<CascadeItem<int, DeptModel>>((e) {
|
|
|
|
DeptModel data = DeptModel.fromJson(e);
|
|
|
|
return CascadeItem(
|
|
|
|
label: data.name, value: data.id, children: data.children);
|
|
|
|
}).toList();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
} catch (e) {
|
|
|
|
LoggerUtil().error(e);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 08:31:17 +08:00
|
|
|
}
|