import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:rogapp/routes/app_pages.dart'; import 'dart:io'; class PermissionHandlerScreen extends StatefulWidget { const PermissionHandlerScreen({Key? key}) : super(key: key); @override State createState() => _PermissionHandlerScreenState(); } class _PermissionHandlerScreenState extends State { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { _checkPermissionStatus(); }); } Future _checkPermissionStatus() async { PermissionStatus status = await Permission.location.status; if (status.isGranted) { if (context.mounted) { Get.toNamed(AppPages.LOGIN); } } else { if (context.mounted) { Get.toNamed(AppPages.HOME); } } } /* _checkPermissionStatus() async { PermissionStatus status = await Permission.location.status; if (status.isGranted == false) { if (context.mounted) { showAlert(context); } } else if (status.isPermanentlyDenied) { await requestPermission(); } else { if (mounted) { Get.toNamed(AppPages.LOGIN); } } } */ void showAlert(BuildContext context) { showDialog( context: context, builder: (_) => AlertDialog( title: Text('location_permission_title'.tr), content: SingleChildScrollView( child: Text('location_permission_content'.tr), ), actions: [ ElevatedButton( child: const Text('OK'), onPressed: () { Get.back(); requestPermission(); }, ), ], )); } // 要検討:位置情報の許可が拒否された場合、適切なエラーメッセージを表示することを検討してください。 // /* Future requestPermission() async { PermissionStatus permission = await Permission.location.status; if (permission == PermissionStatus.permanentlyDenied) { showPermanentAlert(); } else { PermissionStatus newPermission = await Permission.location.request(); if (newPermission != PermissionStatus.granted) { exit(0); } else { if (context.mounted) { Get.toNamed(AppPages.LOGIN); } } } } */ Future requestPermission() async { PermissionStatus permission = await Permission.location.request(); if (permission == PermissionStatus.granted) { if (context.mounted) { Get.toNamed(AppPages.LOGIN); } } else if (permission == PermissionStatus.denied) { await showLocationPermissionDeniedDialog(); } else if (permission == PermissionStatus.permanentlyDenied) { await showPermanentlyDeniedDialog(); } } Future showLocationPermissionDeniedDialog() async { await showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text('location_permission_denied_title'.tr), content: Text('location_permission_denied_message'.tr), actions: [ TextButton( child: Text('ok'.tr), onPressed: () => Navigator.of(context).pop(), ), ], ); }, ); } Future showPermanentlyDeniedDialog() async { await showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text('location_permission_permanently_denied_title'.tr), content: Text('location_permission_permanently_denied_message'.tr), actions: [ TextButton( child: Text('open_settings'.tr), onPressed: () { Navigator.of(context).pop(); openAppSettings(); }, ), ], ); }, ); } @override Widget build(BuildContext context) { return const Scaffold( body: Text(""), ); } // 要検討:ユーザーが位置情報の許可を拒否し続けた場合の対処方法を明確にすることをお勧めします。 // void showPermanentAlert() { showDialog( context: context, builder: (_) => AlertDialog( title: Text('location_disabled_title'.tr), content: SingleChildScrollView( child: Text('location_disabled_content'.tr), ), actions: [ ElevatedButton( child: const Text('OK'), onPressed: () async { await openAppSettings().then( (value) async { if (value) { if (await Permission .location.status.isPermanentlyDenied == true && await Permission.location.status.isGranted == false) { requestPermission(); /* opens app settings until permission is granted */ } } }, ); }, ), ], )); } }