2024-07-26 09:21:22 +08:00

382 lines
13 KiB
Dart
Executable File

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_lock/app_settings/app_colors.dart';
import 'package:star_lock/tools/pickers/pickers.dart';
import 'package:star_lock/tools/pickers/time_picker/model/date_mode.dart';
import 'package:star_lock/tools/pickers/time_picker/model/pduration.dart';
import '../../../../../../tools/commonItem.dart';
import '../../../../../../tools/submitBtn.dart';
import '../../../../../appRouters.dart';
import '../../../../../tools/dateTool.dart';
import '../../../../../tools/storage.dart';
import '../../../../../translations/trans_lib.dart';
import 'massSendElectronicKey_logic.dart';
import 'massSendElectronicKey_state.dart';
class MassSendElectronicKeyPage extends StatefulWidget {
const MassSendElectronicKeyPage({required this.type, Key? key}) : super(key: key);
final String type;
@override
State<MassSendElectronicKeyPage> createState() =>
_MassSendElectronicKeyPageState();
}
class _MassSendElectronicKeyPageState extends State<MassSendElectronicKeyPage> {
final MassSendElectronicKeyLogic logic = Get.put(MassSendElectronicKeyLogic());
final MassSendElectronicKeyState state = Get.find<MassSendElectronicKeyLogic>().state;
@override
initState() {
super.initState();
getDemoMode();
WidgetsBinding.instance.addPostFrameCallback((_) {
logic.resetData();
});
}
Future<void> getDemoMode() async {
state.isDemoMode = (await Storage.getBool(ifIsDemoModeOrNot))!;
}
@override
Widget build(BuildContext context) {
state.type.value = widget.type;
return SingleChildScrollView(
child: state.isDemoMode
? indexChangeWidget()
: Obx(indexChangeWidget),
);
}
Widget indexChangeWidget() {
if (state.isSendSuccess.value) {
return sendElectronicKeySucceed();
} else {
switch (int.parse(widget.type)) {
case 0:
{
// 限时
return Column(
children: <Widget>[
keyInfoWidget(),
keyTimeWidget(),
remoteUnlockingWidget(),
keyBottomWidget()
],
);
}
case 1:
{
// 永久
return Column(
children: <Widget>[
keyInfoWidget(),
remoteUnlockingWidget(),
keyBottomWidget()
],
);
}
case 2:
{
// 循环
return Column(
children: <Widget>[
keyInfoWidget(),
keyPeriodValidityWidget(),
remoteUnlockingWidget(),
keyBottomWidget()
],
);
}
default:
return Container();
}
}
}
// 发送电子钥匙成功
Widget sendElectronicKeySucceed() {
return Column(
children: <Widget>[
Container(
height: 250.h,
width: 1.sw,
color: Colors.white,
child: Column(
children: <Widget>[
SizedBox(
height: 30.h,
),
Image.asset(
'images/icon_send_success.png',
width: 100.w,
height: 100.w,
),
SizedBox(
height: 20.h,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'发送成功'.tr,
style: TextStyle(
fontSize: 26.sp,
color: Colors.black,
fontWeight: FontWeight.w500),
),
],
),
],
),
),
SizedBox(
height: 20.h,
),
SubmitBtn(
btnName: '完成'.tr,
onClick: () {
state.isSendSuccess.value = false;
Get.back();
}),
],
);
}
// 顶部钥匙信息widget
Widget keyInfoWidget() {
return Column(
children: <Widget>[
Obx(() => CommonItem(
leftTitel: '接收者'.tr,
rightTitle: state.receiverList.value.isEmpty
? '请添加'.tr
: state.receiverList.value.length.toString(),
isHaveLine: true,
isHaveDirection: true,
action: () {
Navigator.pushNamed(context, Routers.massSendReceiverPage,
arguments: <String, List>{
'lockUserList': state.receiverList.value,
}).then((Object? value) {
if (value != null) {
value as Map<String, dynamic>;
state.receiverList.value = value['lockUserList'];
// setState(() {});
}
});
},
)),
CommonItem(
leftTitel: TranslationLoader.lanKeys!.countryAndRegion!.tr,
rightTitle: '',
isHaveLine: true,
isHaveRightWidget: true,
isHaveDirection: true,
rightWidget: Text(
'${state.countryName.value} +${state.countryCode.value}',
textAlign: TextAlign.end,
style:
TextStyle(fontSize: 22.sp, color: AppColors.darkGrayTextColor),
),
action: () async {
Object? result = await Navigator.pushNamed(
context, Routers.selectCountryRegionPage);
if (result != null) {
result as Map<String, dynamic>;
state.countryCode = result['code'];
state.countryName = result['countryName'];
setState(() {});
}
},
),
CommonItem(
leftTitel: ''.tr,
rightTitle: state.lockIdList.isEmpty
? '请选择'.tr
: state.lockIdList.length.toString(),
isHaveDirection: true,
action: () async {
final result = await Get.toNamed(Routers.massSendLockGroupListPage,
arguments: <String, Object>{
'keyLimits': '2',
'lockIdList': state.lockIdList
});
if (result != null && result.isNotEmpty) {
final List selectLockIdList = result['selectLockIdList'];
state.lockIdList.clear();
// state.lockIdList.addAll(state.lockIdList.value);
selectLockIdList.forEach((element) {
state.lockIdList.add(element);
});
setState(() {});
}
}),
Container(height: 10.h),
],
);
}
// 生效失效时间
Widget keyTimeWidget() {
return Column(
children: <Widget>[
Obx(() => CommonItem(
leftTitel: '生效时间'.tr,
rightTitle: state.beginTime.value,
isHaveLine: true,
isHaveDirection: true,
action: () {
PDuration selectDate =
PDuration.parse(DateTime.tryParse(state.beginTime.value));
Pickers.showDatePicker(context,
selectDate: selectDate, mode: DateMode.YMDHM, onConfirm: (PDuration p) {
state.beginTime.value = DateTool().getYMDHNDateString(p, 1);
});
})),
Obx(() => CommonItem(
leftTitel: '失效时间'.tr,
rightTitle: state.endTime.value,
isHaveDirection: true,
action: () {
PDuration selectDate = PDuration.parse(DateTime.tryParse(state.endTime.value));
Pickers.showDatePicker(context, selectDate: selectDate, mode: DateMode.YMDHM, onConfirm: (PDuration p) {
state.endTime.value = DateTool().getYMDHNDateString(p, 1);
});
})),
Container(height: 10.h),
],
);
}
// 远程开锁
Widget remoteUnlockingWidget() {
return Column(
children: <Widget>[
CommonItem(
leftTitel: '允许远程开锁'.tr,
rightTitle: '',
isHaveRightWidget: true,
rightWidget: SizedBox(width: 60.w, height: 50.h, child: _switch()),
action: () {}),
Container(height: 40.h),
],
);
}
// 有效期
Widget keyPeriodValidityWidget() {
return Column(
children: <Widget>[
Obx(() => CommonItem(
leftTitel: '有效期'.tr,
rightTitle: '${state.beginTime.value}\n${state.endTime.value}',
isHaveDirection: true,
isHaveLine: true,
action: () async {
var result =
await Get.toNamed(Routers.seletKeyCyclicDatePage, arguments: <String, Object>{
'validityValue': state.weekdaysList.value,
'starDate': state.beginTime.value,
'endDate': state.endTime.value,
'starTime': state.effectiveDateTime.value,
'endTime': state.failureDateTime.value
});
if (result != null && result.isNotEmpty) {
state.weekdaysList.value = result['validityValue'];
state.beginTime.value = result['starDate'];
state.endTime.value = result['endDate'];
state.effectiveDateTime.value = result['starTime'];
state.failureDateTime.value = result['endTime'];
}
})),
Obx(() => Visibility(
visible: state.weekdaysList.isNotEmpty,
child: CommonItem(
leftTitel: '有效日'.tr,
rightTitle: logic.weekDayStr.join(',').toString(),
isHaveDirection: true,
isHaveLine: true,
action: () async {
var result = await Get.toNamed(Routers.seletKeyCyclicDatePage,
arguments: <String, Object>{
'validityValue': state.weekdaysList.value,
'starDate': state.beginTime.value,
'endDate': state.endTime.value,
'starTime': state.effectiveDateTime.value,
'endTime': state.failureDateTime.value
});
if (result != null && result.isNotEmpty) {
state.weekdaysList.value = result['validityValue'];
state.beginTime.value = result['starDate'];
state.endTime.value = result['endDate'];
state.effectiveDateTime.value = result['starTime'];
state.failureDateTime.value = result['endTime'];
}
}))),
Obx(() => Visibility(
visible: state.effectiveDateTime.value.isNotEmpty,
child: CommonItem(
leftTitel: '有效时间'.tr,
rightTitle:
'${state.effectiveDateTime.value}-${state.failureDateTime.value}',
isHaveDirection: true,
action: () async {
var result = await Get.toNamed(
Routers.seletKeyCyclicDatePage,
arguments: <String, Object>{
'validityValue': state.weekdaysList.value,
'starDate': state.beginTime.value,
'endDate': state.endTime.value,
'starTime': state.effectiveDateTime.value,
'endTime': state.failureDateTime.value
});
if (result != null && result.isNotEmpty) {
state.weekdaysList.value = result['validityValue'];
state.beginTime.value = result['starDate'];
state.endTime.value = result['endDate'];
state.effectiveDateTime.value = result['starTime'];
state.failureDateTime.value = result['endTime'];
Get.back(result: result);
}
}),
)),
Container(height: 10.h),
],
);
}
Widget keyBottomWidget() {
return Column(
children: <Widget>[
SubmitBtn(
btnName: '发送'.tr,
onClick: () {
logic.massKeyChecksRequest();
}),
],
);
}
CupertinoSwitch _switch() {
return CupertinoSwitch(
activeColor: CupertinoColors.activeBlue,
trackColor: CupertinoColors.systemGrey5,
thumbColor: CupertinoColors.white,
value: state.isRemoteUnlock.value,
onChanged: (bool value) {
setState(() {
state.isRemoteUnlock.value = !state.isRemoteUnlock.value;
});
},
);
}
}