import 'dart:io'; import 'package:cached_network_image/cached_network_image.dart'; 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 { final logic = Get.put(MinePersonInfoLogic()); final state = Get.find().state; @override initState() { super.initState(); logic.getUserInfoRequest(); } @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: [ Obx(() => CommonItem( leftTitel: TranslationLoader.lanKeys!.avatar!.tr, rightTitle: "", allHeight: 100.h, isHaveLine: true, isHaveDirection: true, isHaveRightWidget: true, rightWidget: ClipOval( 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, ), ), action: () async { //安卓平台下首次进入应用需向用户告知获取权限用途弹窗 if (Platform.isAndroid) { AppFirstEnterHandle() .getAppFirstEnter(context, isAgreeCamera); var getFlag = await Storage.getString(isAgreeCamera); if (getFlag == isAgreeCamera) { requestCameraPermission(); } } else { requestCameraPermission(); } }, )), 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 { // 检查是否已经授予相机权限 PermissionStatus status = await Permission.camera.status; if (status.isGranted) { // 如果权限已经被授予,执行您的相机操作 // 这里可以调用打开相机的方法或者跳转到相机页面等 _openModalBottomSheet(); } else { // 如果权限尚未被授予,请求相机权限 // 此处会显示系统权限请求对话框 status = await Permission.camera.request(); if (status.isGranted) { // 如果用户授予了相机权限,执行您的相机操作 _openModalBottomSheet(); } else { // 如果用户拒绝了相机权限,您可以提供适当的提示 // 或者引导用户手动授予权限 _showPermissinDialog(); } } } Future _showPermissinDialog() async { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('Permission Required'), content: const Text( 'You need to grant camera permission to use this feature.'), actions: [ TextButton( child: const Text('OK'), onPressed: () { Navigator.of(context).pop(); }, ), ], ); }, ); } 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(); } else if (getSelectIndex == 1) { selectImage(); } }, ); }); } ///拍摄照片 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(() {}); } } }