app-starlock/lib/mine/addLock/nearbyLock/nearbyLock_page.dart

315 lines
9.3 KiB
Dart
Raw Normal View History

import 'package:flutter/cupertino.dart';
2023-07-10 17:50:31 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
2024-01-16 10:41:08 +08:00
import 'package:flutter_easyloading/flutter_easyloading.dart';
2023-07-10 17:50:31 +08:00
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
2024-04-30 11:33:44 +08:00
import 'package:star_lock/flavors.dart';
2024-08-01 18:54:32 +08:00
import 'package:star_lock/mine/addLock/nearbyLock/nearbyLock_state.dart';
2023-07-10 17:50:31 +08:00
import '../../../app_settings/app_colors.dart';
import '../../../tools/appRouteObserver.dart';
2023-07-10 17:50:31 +08:00
import '../../../tools/titleAppBar.dart';
import 'nearbyLock_logic.dart';
2023-07-10 17:50:31 +08:00
class NearbyLockPage extends StatefulWidget {
2023-07-15 15:11:28 +08:00
const NearbyLockPage({Key? key}) : super(key: key);
2023-07-10 17:50:31 +08:00
@override
State<NearbyLockPage> createState() => _NearbyLockPageState();
}
class _NearbyLockPageState extends State<NearbyLockPage> with RouteAware {
2024-08-01 18:54:32 +08:00
final NearbyLockLogic logic = Get.put(NearbyLockLogic());
final NearbyLockState state = Get.find<NearbyLockLogic>().state;
2023-07-10 17:50:31 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.mainBackgroundColor,
2024-04-30 11:33:44 +08:00
appBar: F.sw(
2024-05-28 10:40:17 +08:00
skyCall: () => TitleAppBar(
2024-08-01 18:54:32 +08:00
barTitle: '附近的锁'.tr,
2024-04-30 11:33:44 +08:00
haveBack: true,
backgroundColor: AppColors.mainColor,
2024-08-01 18:54:32 +08:00
actionsList: <Widget>[
2024-04-30 11:33:44 +08:00
CupertinoActivityIndicator(
radius: 18.w,
color: Colors.white,
),
SizedBox(width: 30.w)
]),
xhjCall: () => TitleAppBar(
2024-08-01 18:54:32 +08:00
barTitle: '附近的锁'.tr,
2024-04-30 11:33:44 +08:00
haveBack: true,
backgroundColor: Colors.white,
iconColor: AppColors.blackColor,
titleColor: AppColors.blackColor,
2024-08-01 18:54:32 +08:00
actionsList: <Widget>[
2024-04-30 11:33:44 +08:00
CupertinoActivityIndicator(
radius: 18.w,
color: AppColors.blackColor,
),
SizedBox(width: 30.w)
]),
),
2024-08-01 18:54:32 +08:00
body: Obx(listView),
2023-07-10 17:50:31 +08:00
);
}
2024-05-07 16:45:59 +08:00
Widget listView() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
2024-08-01 18:54:32 +08:00
children: <Widget>[
2024-05-07 16:45:59 +08:00
Expanded(
child: ListView.separated(
itemCount: state.devices.length,
2024-08-01 18:54:32 +08:00
itemBuilder: (BuildContext c, int index) {
return nearbyLockItem(
'images/icon_lockGroup_item.png', state.devices[index], () {
2024-05-07 16:45:59 +08:00
String advName = state.devices[index].advertisementData.advName;
state.selectLockName.value = advName;
logic.getServerDatetime();
2024-05-07 16:45:59 +08:00
});
},
separatorBuilder: (BuildContext context, int index) {
return Divider(
height: 1,
color: AppColors.greyLineColor,
indent: 20.w,
endIndent: 0,
);
},
),
),
Padding(
padding: EdgeInsets.only(left: 15.w, bottom: 10.h),
child: TextButton(
onPressed: () async {
bool skip = false;
if (!state.otaState.value) {
skip = await Get.dialog(
const _TipDialog(),
);
}
state.otaState.value = skip;
},
child: Text(
state.otaState.value ? '点击返回设备配对'.tr : '无法连接?尝试升级'.tr,
style: TextStyle(fontSize: 22.sp),
),
),
),
],
);
}
2024-04-30 11:33:44 +08:00
Widget nearbyLockItem(
String lockTypeIcon, ScanResult scanResult, Function() action) {
2023-07-10 17:50:31 +08:00
return GestureDetector(
2024-04-30 11:33:44 +08:00
onTap: ((scanResult.advertisementData.serviceUuids.isNotEmpty
? scanResult.advertisementData.serviceUuids[0]
2024-08-01 18:54:32 +08:00
: '')
2024-04-30 11:33:44 +08:00
.toString()[33] ==
2024-08-01 18:54:32 +08:00
'1')
2024-04-30 11:33:44 +08:00
? action
: null,
2023-07-10 17:50:31 +08:00
child: Column(
2024-08-01 18:54:32 +08:00
children: <Widget>[
2023-07-10 17:50:31 +08:00
Container(
height: 89.h,
2023-09-28 18:05:23 +08:00
width: 1.sw,
2023-07-10 17:50:31 +08:00
color: Colors.white,
child: Row(
2024-08-01 18:54:32 +08:00
children: <Widget>[
SizedBox(width: 20.w),
Image.asset(
lockTypeIcon,
width: 56.w,
height: 56.w,
2023-07-10 17:50:31 +08:00
),
SizedBox(width: 20.w),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
2024-08-01 18:54:32 +08:00
children: <Widget>[
// 第32、33两位00 表示休眠, 01表示唤醒
2024-04-30 11:33:44 +08:00
Text(scanResult.advertisementData.advName,
style: TextStyle(
fontSize: 20.sp,
color: ((scanResult.advertisementData.serviceUuids
.isNotEmpty
? scanResult.advertisementData
.serviceUuids[0]
2024-08-01 18:54:32 +08:00
: '')
2024-04-30 11:33:44 +08:00
.toString()[33] ==
2024-08-01 18:54:32 +08:00
'1')
2024-04-30 11:33:44 +08:00
? AppColors.blackColor
: Colors.grey)),
],
),
SizedBox(
width: 10.w,
),
Image.asset(
2024-08-01 18:54:32 +08:00
'images/mine/icon_mine_main_about.png',
width: 22.w,
height: 22.w,
),
Expanded(child: SizedBox(width: 20.w)),
Image.asset(
2024-05-07 16:45:59 +08:00
state.otaState.value
? 'images/ota_upgrade_icon.png'
: 'images/main/icon_main_addLock.png',
width: 36.w,
height: 36.w,
2024-05-07 16:45:59 +08:00
color: AppColors.mainColor,
),
SizedBox(width: 30.w),
2023-07-10 17:50:31 +08:00
],
),
),
],
),
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
/// 路由订阅
AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!);
}
@override
void dispose() {
/// 取消路由订阅
AppRouteObserver().routeObserver.unsubscribe(this);
super.dispose();
}
/// 从上级界面进入 当前界面即将出现
@override
void didPush() {
super.didPush();
}
/// 返回上一个界面 当前界面即将消失
@override
void didPop() {
super.didPop();
2024-01-16 10:41:08 +08:00
EasyLoading.isShow ? EasyLoading.dismiss() : null;
state.ifCurrentScreen.value = false;
logic.cancelBlueConnetctToastTimer();
logic.stopScanBlueList();
}
/// 从下级返回 当前界面即将出现
@override
void didPopNext() {
super.didPopNext();
state.ifCurrentScreen.value = true;
logic.startScanBlueList();
}
/// 进入下级界面 当前界面即将消失
@override
void didPushNext() {
super.didPushNext();
2024-05-07 16:45:59 +08:00
if (!logic.state.otaState.value) {
state.ifCurrentScreen.value = false;
logic.cancelBlueConnetctToastTimer();
logic.stopScanBlueList();
}
}
}
2024-05-07 16:45:59 +08:00
class _TipDialog extends StatelessWidget {
const _TipDialog({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return CupertinoAlertDialog(
title: Text(
'固件升级提示'.tr,
),
content: Text('请先获取固件文件到手机本地,再选择升级'.tr),
2024-08-01 18:54:32 +08:00
actions: <Widget>[
2024-05-07 16:45:59 +08:00
TextButton(
2024-08-08 11:00:03 +08:00
onPressed: Get.back,
2024-05-07 16:45:59 +08:00
child: Text(
'取消'.tr,
style: TextStyle(fontSize: 22.sp, color: AppColors.blackColor),
),
),
TextButton(
onPressed: () async {
Get.back(result: true);
},
child: Text(
'确定'.tr,
style: TextStyle(fontSize: 22.sp, color: AppColors.blackColor),
),
),
],
);
}
}
class OTAProgressDialog extends StatelessWidget {
NearbyLockLogic logic;
OTAProgressDialog({required this.logic, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Obx(() {
return CupertinoAlertDialog(
title: Text(
'固件升级中'.tr,
),
2024-05-09 17:32:38 +08:00
content: Column(
2024-08-01 18:54:32 +08:00
children: <Widget>[
2024-05-09 17:32:38 +08:00
Padding(
padding: EdgeInsets.only(top: 20.h, bottom: 10.h),
child: Text(
'传输期间请勿离开当前页面'.tr,
style: TextStyle(fontSize: 20.sp, color: AppColors.blackColor),
),
2024-05-07 16:45:59 +08:00
),
2024-05-09 17:32:38 +08:00
Row(
2024-08-01 18:54:32 +08:00
children: <Widget>[
2024-05-09 17:32:38 +08:00
Text(
'传输中'.tr,
style: TextStyle(fontSize: 18.sp, color: AppColors.mainColor),
),
SizedBox(
width: 15.w,
),
Expanded(
child: LinearProgressIndicator(
value: logic.state.otaProgress.value,
color: AppColors.mainColor,
)),
],
2024-05-07 16:45:59 +08:00
),
],
),
2024-08-01 18:54:32 +08:00
actions: <Widget>[
2024-05-07 16:45:59 +08:00
TextButton(
onPressed: () {
logic.closeOTADAta();
},
child: Text(
'取消升级'.tr,
style: TextStyle(fontSize: 22.sp, color: AppColors.blackColor),
),
),
],
);
});
}
2023-07-10 17:50:31 +08:00
}