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(); }); } _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: const Text('ロケーション許可'), content: const SingleChildScrollView( child: ListBody( children: [ Text('このアプリでは、位置情報の収集を行います。'), Text( '岐阜ナビアプリではチェックポイントの自動チェックインの機能を可能にするために、現在地のデータが収集されます。アプリを閉じている時や、使用していないときにも収集されます。位置情報は、個人を特定できない統計的な情報として、ユーザーの個人情報とは一切結びつかない形で送信されます。お知らせの配信、位置情報の利用を許可しない場合は、この後表示されるダイアログで「許可しない」を選択してください。'), ], ), ), 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); } } } } @override Widget build(BuildContext context) { return const Scaffold( body: Text(""), ); } void showPermanentAlert() { showDialog( context: context, builder: (_) => AlertDialog( title: const Text('無効'), content: const SingleChildScrollView( child: ListBody( children: [ Text('位置情報が無効になっています'), Text( 'このアプリケーションへの位置情報アクセスが無効になっています。続行するには設定>プライバシーとセキュリティ>位置情報サービス>岐阜ナビ で有効にしてください。'), ], ), ), 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 */ } } }, ); }, ), ], )); } }