import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:image_picker/image_picker.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:star_lock/app_settings/app_colors.dart'; import 'package:star_lock/mine/minePersonInfo/minePersonInfoPage/minePersonInfo_logic.dart'; import 'package:star_lock/tools/appFirstEnterHandle.dart'; import 'package:star_lock/tools/custom_bottom_sheet.dart'; import 'package:star_lock/tools/storage.dart'; import '../../../appRouters.dart'; import '../../../tools/commonItem.dart'; import '../../../tools/titleAppBar.dart'; import '../../../translations/trans_lib.dart'; class MinePersonInfoPage extends StatefulWidget { const MinePersonInfoPage({Key? key}) : super(key: key); @override State createState() => _MinePersonInfoPageState(); } class _MinePersonInfoPageState extends State with WidgetsBindingObserver { final logic = Get.put(MinePersonInfoLogic()); final state = Get.find().state; @override initState() { super.initState(); WidgetsBinding.instance.addObserver(this); // 添加观察者 logic.getUserInfoRequest(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); // 移除观察者 super.dispose(); } // 当应用生命周期状态变化时调用 @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); if (state == AppLifecycleState.resumed) { // 当应用从后台返回前台时检查相机权限 checkCameraPermission(); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: AppColors.mainBackgroundColor, appBar: TitleAppBar( barTitle: TranslationLoader.lanKeys!.personalInformation!.tr, haveBack: true, backgroundColor: AppColors.mainColor), body: Column( children: [ CommonItem( leftTitel: TranslationLoader.lanKeys!.avatar!.tr, rightTitle: "", allHeight: 100.h, isHaveLine: true, isHaveDirection: true, isHaveRightWidget: true, rightWidget: ClipOval( child: state.image != null ? Image.file( File(state.image!.path), width: 72.w, height: 72.w, fit: BoxFit.fill, ) : Image.asset( 'images/controls_user.png', width: 72.w, height: 72.w, fit: BoxFit.fill, ), ), action: () async { //安卓平台下首次进入应用需向用户告知获取权限用途弹窗 if (Platform.isAndroid) { AppFirstEnterHandle() .getAppFirstEnter(context, isAgreeCamera); var getFlag = await Storage.getString(isAgreeCamera); if (getFlag == isAgreeCamera) { _openModalBottomSheet(); } } else { _openModalBottomSheet(); } }, ), Obx(() => CommonItem( leftTitel: TranslationLoader.lanKeys!.nickName!.tr, rightTitle: state.mineInfoData.value.nickname != null ? state.mineInfoData.value.nickname! : "", isHaveLine: true, isHaveDirection: true, action: () { Navigator.pushNamed( context, Routers.minePersonInfoEditNamePage, arguments: { 'nickName': state.mineInfoData.value.nickname }).then((value) => logic.getUserInfoRequest()); })), Obx(() => CommonItem( leftTitel: TranslationLoader.lanKeys!.mobileNumber!.tr, rightTitle: state.mineInfoData.value.mobile != null ? state.mineInfoData.value.mobile! : TranslationLoader.lanKeys!.goBind!.tr, isHaveLine: true, isHaveDirection: true, action: () { //有手机号 则去修改手机号 否则去绑定新的手机号 isFrom:1 短信,2 邮箱 if (state.mineInfoData.value.mobile!.isNotEmpty) { Navigator.pushNamed( context, Routers.mineUnbindPhoneOrEmailPage, arguments: { 'mobile': state.mineInfoData.value.mobile!, 'isFrom': '1' }).then((value) => logic.getUserInfoRequest()); } else { Navigator.pushNamed( context, Routers.mineBindPhoneOrEmailPage, arguments: { 'mobile': state.mineInfoData.value.mobile!, 'isFrom': '1' }).then((value) => logic.getUserInfoRequest()); } })), Obx(() => CommonItem( leftTitel: TranslationLoader.lanKeys!.email!.tr, rightTitle: state.mineInfoData.value.email != null ? state.mineInfoData.value.email! : TranslationLoader.lanKeys!.goBind!.tr, isHaveLine: true, isHaveDirection: true, action: () { //有邮箱 则去修改邮箱 否则去绑定新的邮箱 isFrom:1 短信,2 邮箱 if (state.mineInfoData.value.email!.isNotEmpty) { Navigator.pushNamed( context, Routers.mineUnbindPhoneOrEmailPage, arguments: { 'isFrom': '2', 'email': state.mineInfoData.value.email! }).then((value) => logic.getUserInfoRequest()); } else { Navigator.pushNamed( context, Routers.mineBindPhoneOrEmailPage, arguments: { 'isFrom': '2', 'email': state.mineInfoData.value.email! }).then((value) => logic.getUserInfoRequest()); } })), CommonItem( leftTitel: TranslationLoader.lanKeys!.resetPasswords!.tr, rightTitle: "", isHaveLine: true, isHaveDirection: true, action: () { Navigator.pushNamed( context, Routers.minePersonInfoResetPasswordPage); }), Obx(() => CommonItem( leftTitel: TranslationLoader.lanKeys!.safetyProblem!.tr, rightTitle: state.mineInfoData.value.haveSafeAnswer == 0 ? "去设置" : "", isHaveLine: true, isHaveDirection: true, action: () { if (state.mineInfoData.value.haveSafeAnswer == 0) { Navigator.pushNamed( context, Routers.minePersonInfoSetSafetyProblemPage) .then((value) => logic.getUserInfoRequest()); } else { Navigator.pushNamed( context, Routers.minePersonInfoViewSafetyProblemPage); } })), Obx(() => CommonItem( leftTitel: TranslationLoader.lanKeys!.countryAndRegion!.tr, rightTitle: state.mineInfoData.value.countryName != null ? state.mineInfoData.value.countryName! : "", isHaveLine: false, isHaveDirection: false)), ], )); } Future requestCameraPermission() async { var status = await Permission.camera.status; if (status.isGranted) { selectCamera(); } else { status = await Permission.camera.request(); if (status.isGranted) { selectCamera(); } else { showPermissionDeniedDialog(); } } } Future requestPhotoPermission() async { var status = await Permission.photos.status; if (status.isGranted) { selectImage(); } else { status = await Permission.photos.request(); if (status.isGranted) { selectImage(); } else { showPermissionDeniedDialog(); } } } // 显示权限被永久拒绝的提示对话框 void showPermissionDeniedDialog() { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('权限被拒绝'), content: const Text('请手动在系统设置中开启相册权限以继续使用应用。'), actions: [ TextButton( child: const Text('去设置'), onPressed: () { Navigator.of(context).pop(); // 关闭对话框 openAppSettings(); // 打开系统设置页面 }, ), ], ); }, ); } Future _openModalBottomSheet() async { showModalBottomSheet( context: context, shape: RoundedRectangleBorder( borderRadius: BorderRadiusDirectional.circular(10)), builder: (BuildContext context) { return AlertBottomWidget( topTitle: '', items: const ['拍照', '从相册选择'], chooseCallback: (value) { int getSelectIndex = value; if (getSelectIndex == 0) { //拍照选项 // selectCamera(); requestCameraPermission(); } else if (getSelectIndex == 1) { // selectImage(); requestPhotoPermission(); } }, ); }); } ///拍摄照片 selectCamera() async { XFile? photo = await state.imagePicker.pickImage( source: ImageSource.camera, preferredCameraDevice: CameraDevice.rear); if (photo != null) { state.image = photo; // logic.getUpTokenRequest(); setState(() {}); } } ///从相册选取 selectImage() async { XFile? image = await state.imagePicker.pickImage( source: ImageSource.gallery, maxHeight: 250, maxWidth: 250, ); if (image != null) { state.image = image; var bytes = File(state.image!.path); var enc = await bytes.readAsBytes(); print(enc.length); print( "state.image!.path:${state.image!.path} state.image!.name:${state.image!.name} state.image!.length():${state.image!.length()}"); logic.getUpTokenRequest(state.image!.name, enc.length); setState(() {}); } } Future checkCameraPermission() async { var status = await Permission.camera.status; if (status.isGranted) { // 如果权限已经被授予,打开相机 } else if (status.isPermanentlyDenied || status.isDenied) { // 如果权限被永久拒绝,显示对话框引导用户去设置页面 } } Future checkPhotosPermission() async { var status = await Permission.photos.status; if (status.isGranted) { // 如果权限已经被授予,打开相机 } else if (status.isPermanentlyDenied || status.isDenied) { // 如果权限被永久拒绝,显示对话框引导用户去设置页面 } } // child: state.mineInfoData.value.headUrl != null // ? CachedNetworkImage( // imageUrl: Uri.encodeFull( // state.mineInfoData.value.headUrl!), // width: 72.w, // height: 72.w, // fit: BoxFit.fill, // placeholder: (context, url) => Image.asset( // 'images/controls_user.png', // width: 72.w, // height: 72.w, // fit: BoxFit.fill, // )) // : Image.asset( // 'images/controls_user.png', // width: 72.w, // height: 72.w, // fit: BoxFit.fill, // ), }