import 'package:flutter/material.dart'; import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'; import 'package:project_telephony/base/base_style.dart'; import 'package:project_telephony/ui/widget/plone_back_button.dart'; import 'package:project_telephony/utils/headers.dart'; import 'package:telephony/telephony.dart'; class PrivacyRightsPage extends StatefulWidget { final String name; const PrivacyRightsPage({Key? key, required this.name}) : super(key: key); @override _PrivacyRightsPageState createState() => _PrivacyRightsPageState(); } final Telephony telephony = Telephony.instance; // late final bool permissionsGranted; // String body = ""; // @override // void initState() async { // initPlatformState(); // } class _PrivacyRightsPageState extends State { final Permission _permission = Permission.phone; final PermissionHandlerPlatform _permissionHandler = PermissionHandlerPlatform.instance; PermissionStatus _permissionStatus = PermissionStatus.denied; Future requestPermission(Permission permission) async { final status = await _permissionHandler.requestPermissions([permission]); setState(() { _permissionStatus = status[permission] ?? PermissionStatus.denied; }); } @override void initState() { super.initState(); _listenForPermissionStatus(); } void _listenForPermissionStatus() async { final status = await _permissionHandler.checkPermissionStatus(_permission); setState(() => _permissionStatus = status); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( elevation: 0, title: Text( widget.name, style: TextStyle( fontSize: BaseStyle.fontSize34, color: BaseStyle.color333333, fontWeight: FontWeight.bold), ), titleSpacing: 162.w, leading: const CloudBackButton(isSpecial: true), backgroundColor: kForeGroundColor), backgroundColor: Colors.white, body: widget.name == "隐私政策" ? null : _getRights(true), ); } // onMessage(SmsMessage message) async { // setState(() { // body = message.body ?? "error reading message body."; // print(body); // }); // } // // onSendStatus(SendStatus status) { // setState(() { // body = status == SendStatus.SENT ? "sent" : "delivered"; // }); // } // // Future initPlatformState() async { // final bool? result = await telephony.requestPhoneAndSmsPermissions; // if (result != null && result) { // telephony.listenIncomingSms( // onNewMessage: onMessage, // onBackgroundMessage: onBackgroundMessage, // listenInBackground: true); // } // if (!mounted) return; // } _getRights(bool pd) { return ListView( children: Permission.values .where((permission) { return permission == Permission.phone || permission == Permission.sms; }) .map((permission) => PermissionWidget(permission)) .toList()); } } class PermissionWidget extends StatefulWidget { const PermissionWidget(this._permission); final Permission _permission; @override _PermissionState createState() => _PermissionState(_permission); } class _PermissionState extends State { _PermissionState(this._permission); final Permission _permission; final PermissionHandlerPlatform _permissionHandler = PermissionHandlerPlatform.instance; PermissionStatus _permissionStatus = PermissionStatus.denied; List title = [ "获取来电权限", "获取短信权限", ]; List sub = [ "用于监听来电状态", "用于发送短信", ]; @override void initState() { super.initState(); _listenForPermissionStatus(); } void _listenForPermissionStatus() async { final status = await _permissionHandler.checkPermissionStatus(_permission); setState(() => _permissionStatus = status); } Color getPermissionColor() { switch (_permissionStatus) { case PermissionStatus.denied: return Colors.red; case PermissionStatus.granted: return Colors.green; case PermissionStatus.limited: return Colors.orange; default: return Colors.red; } } String getPermissionStu() { switch (_permissionStatus) { case PermissionStatus.denied: return "未允许"; case PermissionStatus.granted: return "已允许"; default: return "未允许"; } } @override Widget build(BuildContext context) { return ListTile( title: Text( _permission.value==8?title[0]:title[1], style: Theme.of(context).textTheme.titleMedium, ), subtitle: Text( _permission.value==8?sub[0]:sub[1], ), trailing: Wrap( children: [ Text(getPermissionStu(), style: TextStyle(color: getPermissionColor())), const Icon(Icons.arrow_forward_ios), ], ), onTap: () { requestPermission(_permission); }, ); } void checkServiceStatus( BuildContext context, PermissionWithService permission) async { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text( (await _permissionHandler.checkServiceStatus(permission)).toString()), )); } Future requestPermission(Permission permission) async { final status = await _permissionHandler.requestPermissions([permission]); setState(() { print(status); _permissionStatus = status[permission] ?? PermissionStatus.denied; print(_permissionStatus); }); } }