Files
rog_app/lib/pages/permission/permission.dart
2023-08-16 14:53:32 +05:30

213 lines
6.4 KiB
Dart

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<PermissionHandlerScreen> createState() => _PermissionHandlerScreenState();
}
class _PermissionHandlerScreenState extends State<PermissionHandlerScreen> {
Future<void> _showMyDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('ロケーション許可'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text( 'このアプリでは、位置情報の収集を行います。'),
Text( 'このアプリでは、開始時点で位置情報を収集します。'),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('わかった'),
onPressed: () {
//Navigator.of(context).pop();
Get.toNamed(AppPages.TRAVEL);
},
),
],
);
},
);
}
@override
void initState() {
// TODO: implement initState
super.initState();
//permissionServiceCall();
}
Future<PermissionStatus> checkLocationPermission() async {
return await Permission.location.status;
}
permissionServiceCall() async {
await permissionServices().then(
(value) {
if (value[Permission.location]!.isGranted ) {
/* ========= New Screen Added ============= */
Get.toNamed(AppPages.TRAVEL);
// Navigator.pushReplacement(
// context,
// MaterialPageRoute(builder: (context) => SplashScreen()),
// );
}
else{
_showMyDialog();
}
},
);
}
/*Permission services*/
Future<Map<Permission, PermissionStatus>> permissionServices() async {
// You can request multiple permissions at once.
Map<Permission, PermissionStatus> statuses = await [
Permission.location,
//add more permission to request here.
].request();
if (statuses[Permission.location]!.isPermanentlyDenied) {
await openAppSettings().then(
(value) async {
if (value) {
if (await Permission.location.status.isPermanentlyDenied == true &&
await Permission.location.status.isGranted == false) {
// openAppSettings();
permissionServiceCall(); /* opens app settings until permission is granted */
}
}
},
);
} else {
if (statuses[Permission.location]!.isDenied) {
permissionServiceCall();
}
}
/*{Permission.camera: PermissionStatus.granted, Permission.storage: PermissionStatus.granted}*/
return statuses;
}
requestPermission() async {
PermissionStatus permission = await Permission.location.status;
if(permission == PermissionStatus.permanentlyDenied){
showPermanentAlert(context);
}else{
PermissionStatus newPermission = await Permission.location.request();
//showAlert(context);
if (newPermission != PermissionStatus.granted) {
// If permission not granted, handle the issue in your own way
exit(0);
}
else{
Get.toNamed(AppPages.TRAVEL);
}
}
// if (permission != PermissionStatus.granted) {
// }
// If permission is granted or already was granted
return true;
}
@override
Widget build(BuildContext context) {
var status = Permission.location.status.then((value){
if(value.isGranted == false){
showAlert(context);
//requestPermission() ? Get.toNamed(AppPages.TRAVEL) : exit(0);
//Future.delayed(Duration.zero, () => showAlert(context));
}
else if(value.isPermanentlyDenied){
}
else {
Get.toNamed(AppPages.TRAVEL);
}
});
return Scaffold(
body: Container(
child: const Text(""),
),
);
}
void showPermanentAlert(BuildContext context) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text('無効'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text( '位置情報が無効になっています'),
Text('このアプリケーションへの位置情報アクセスが無効になっています。続行するには設定>プライバシーとセキュリティ>位置情報サービス>岐阜ナビ で有効にしてください。'),
],
),
),
actions: <Widget>[
ElevatedButton(
child: const Text('OK'),
onPressed: () {
//requestPermission();
permissionServiceCall();
},
),
],
)
);
}
void showAlert(BuildContext context) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text('ロケーション許可'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text( 'このアプリでは、位置情報の収集を行います。'),
Text('岐阜ナビアプリではチェックポイントの自動チェックインの機能を可能にするために、現在地のデータが収集されます。アプリを閉じている時や、使用していないときにも収集されます。位置情報は、個人を特定できない統計的な情報として、ユーザーの個人情報とは一切結びつかない形で送信されます。お知らせの配信、位置情報の利用を許可しない場合は、この後表示されるダイアログで「許可しない」を選択してください。'),
],
),
),
actions: <Widget>[
ElevatedButton(
child: const Text('OK'),
onPressed: () {
requestPermission();
//permissionServiceCall();
},
),
],
)
);
}
}