@@ -29,17 +29,17 @@ library;
29
29
30
30
import 'package:flutter/material.dart' ;
31
31
32
- import 'package:permission_handler/permission_handler.dart' ;
33
32
import 'package:universal_io/io.dart' show Platform;
34
33
import 'package:ussd_phone_call_sms/ussd_phone_call_sms.dart' ;
35
34
36
35
import 'package:healthpod/utils/show_alert.dart' ;
37
36
38
37
/// A widget that displays a phone icon. On iOS, Android, and Linux the icon is interactive.
39
- /// On Linux, we bypass permission checks because permission_handler does not support Linux .
38
+ /// (Permission checks have been removed for Linux since permission_handler does not work there.) .
40
39
41
40
class CallIcon extends StatefulWidget {
42
41
final String contactNumber;
42
+
43
43
const CallIcon ({
44
44
super .key,
45
45
required this .contactNumber,
@@ -54,7 +54,7 @@ class _CallIconState extends State<CallIcon> {
54
54
55
55
@override
56
56
Widget build (BuildContext context) {
57
- // Enable interactive behavior on mobile and Linux.
57
+ // Enable interactive behavior on iOS, Android, and Linux.
58
58
59
59
if (Platform .isIOS || Platform .isAndroid || Platform .isLinux) {
60
60
return GestureDetector (
@@ -68,72 +68,10 @@ class _CallIconState extends State<CallIcon> {
68
68
}
69
69
}
70
70
71
- Future <void > _showPermissionDialog (BuildContext context) async {
72
- if (! mounted) return ;
73
- final result = await showDialog <bool >(
74
- context: context,
75
- builder: (dialogContext) => AlertDialog (
76
- title: const Text ("Permission Required" ),
77
- content: const Text (
78
- "This app requires phone call permissions to make a call. Please enable it." ),
79
- actions: < Widget > [
80
- ElevatedButton (
81
- child: const Text ("Cancel" ),
82
- onPressed: () => Navigator .of (dialogContext).pop (false ),
83
- ),
84
- ElevatedButton (
85
- child: const Text ("Ok" ),
86
- onPressed: () => Navigator .of (dialogContext).pop (true ),
87
- ),
88
- ],
89
- ),
90
- );
91
- if (! mounted) return ;
92
- if (result == true ) {
93
- await Permission .phone.request ();
94
- }
95
- }
96
-
97
- Future <void > _showManualPermissionSettingDialog () async {
98
- if (! mounted) return ;
99
- final result = await showDialog <bool >(
100
- context: context,
101
- builder: (dialogContext) {
102
- return AlertDialog (
103
- title: const Text ('Permission Needed' ),
104
- content: const SingleChildScrollView (
105
- child: ListBody (
106
- children: < Widget > [
107
- Text ('This app needs phone permission to make calls.' ),
108
- Text ('Please enable it in the app settings.' ),
109
- ],
110
- ),
111
- ),
112
- actions: < Widget > [
113
- TextButton (
114
- child: const Text ('Cancel' ),
115
- onPressed: () {
116
- Navigator .of (dialogContext).pop (false );
117
- },
118
- ),
119
- TextButton (
120
- child: const Text ('Settings' ),
121
- onPressed: () {
122
- Navigator .of (dialogContext).pop (true );
123
- },
124
- ),
125
- ],
126
- );
127
- },
128
- );
129
- if (! mounted) return ;
130
- if (result == true ) {
131
- await openAppSettings ();
132
- }
133
- }
134
-
71
+ /// Displays a confirmation dialog before initiating the phone call.
72
+
135
73
Future <void > _showConfirmationDialog (BuildContext context) async {
136
- // Capture the context synchronously.
74
+ // Capture the current BuildContext synchronously.
137
75
138
76
final localContext = context;
139
77
showDialog (
@@ -149,7 +87,7 @@ class _CallIconState extends State<CallIcon> {
149
87
ElevatedButton (
150
88
child: const Text ("Yes" ),
151
89
onPressed: () async {
152
- Navigator .of (dialogContext).pop (); // Close the dialog first.
90
+ Navigator .of (dialogContext).pop ();
153
91
await _initiatePhoneCall (localContext);
154
92
},
155
93
),
@@ -158,6 +96,8 @@ class _CallIconState extends State<CallIcon> {
158
96
);
159
97
}
160
98
99
+ /// Initiates the phone call process.
100
+
161
101
Future <void > _initiatePhoneCall (BuildContext context) async {
162
102
// Capture the BuildContext synchronously.
163
103
@@ -168,45 +108,18 @@ class _CallIconState extends State<CallIcon> {
168
108
_iconColor = Colors .red;
169
109
});
170
110
171
- // If running on Linux, skip permission checks .
111
+ // For iOS/Android/ Linux, simply attempt the phone call .
172
112
173
- if (Platform .isLinux) {
174
- try {
175
- await UssdPhoneCallSms ().phoneCall (phoneNumber: widget.contactNumber);
176
- } catch (e) {
177
- if (! mounted) return ;
178
- showAlert (localContext,
179
- 'Fail to call ${widget .contactNumber }! Phone call may not be supported on Linux.' );
180
- }
113
+ try {
114
+ await UssdPhoneCallSms ().phoneCall (phoneNumber: widget.contactNumber);
115
+ } catch (e) {
181
116
if (! mounted) return ;
182
- setState (() {
183
- _iconColor = Colors .deepPurple;
184
- });
185
- return ;
186
- }
187
-
188
- // For iOS/Android, use permission_handler.
189
117
190
- final callStatus = await Permission .phone.status;
191
- if (! mounted) return ;
192
- if (callStatus.isPermanentlyDenied) {
193
- await _showManualPermissionSettingDialog ();
194
- if (! mounted) return ;
195
- } else if (callStatus.isDenied) {
196
- await _showPermissionDialog (localContext);
197
- if (! mounted) return ;
198
- } else {
199
- try {
200
- await UssdPhoneCallSms ().phoneCall (phoneNumber: widget.contactNumber);
201
- } catch (e) {
202
- if (! mounted) return ;
203
- showAlert (localContext,
204
- 'Fail to call ${widget .contactNumber }! Please check app permission!' );
205
- }
118
+ showAlert (localContext,
119
+ 'Fail to call ${widget .contactNumber }! Please check app permission or platform support!' );
206
120
}
207
121
208
122
if (! mounted) return ;
209
-
210
123
setState (() {
211
124
_iconColor = Colors .deepPurple;
212
125
});
0 commit comments