撥打電話按鈕

This commit is contained in:
kuei
2024-10-04 02:43:59 +08:00
parent 4400081d0a
commit 98293d34c2

View File

@@ -4,6 +4,7 @@ import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:topic/NoSwipeBackRoute.dart';
import 'package:topic/main.dart';
import 'package:url_launcher/url_launcher.dart';
class PersonalInfo extends StatefulWidget {
final String email; // 接收來自上個頁面的 email
@@ -201,23 +202,15 @@ class _PersonalInfoState extends State<PersonalInfo> {
onTap: () {},
),
Divider(),
ListTile(
leading: Icon(Icons.local_phone_rounded),
title: Text('撥打電話', style: TextStyle(fontSize: 18)),
onTap: () {
_showDialerDialog(context); // 顯示對話框,確認是否撥打電話
},
),
Divider(),
// ElevatedButton(
// onPressed: () {
// if (_isEditing) {
// _saveChanges();
// } else {
// _toggleEdit();
// }
// },
// child: Text(_isEditing ? '儲存變更' : '修改資料'),
// style: TextButton.styleFrom(
// backgroundColor: Color(0xFFF5E3C3),
// textStyle: TextStyle(fontSize: 18),
// shadowColor: Colors.transparent,
// ),
// ),
ElevatedButton(
onPressed: _loginOut,
child: Text('登出'),
@@ -566,4 +559,47 @@ class _AccountPageState extends State<AccountPage> {
),
);
}
}
void _launchDialer(BuildContext context, String phoneNumber) async {
final Uri launchUri = Uri(
scheme: 'tel',
path: phoneNumber,
);
if (await canLaunch(launchUri.toString())) {
await launch(launchUri.toString());
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('無法撥打電話至 $phoneNumber')),
);
}
}
// 顯示確認撥打電話的對話框
void _showDialerDialog(BuildContext context) {
String phoneNumber = "123456789";
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('撥打電話'),
content: Text('你確定要撥打電話至 $phoneNumber 嗎?'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('關閉'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
_launchDialer(context, phoneNumber);//跳至撥打電話
},
child: Text('撥打電話'),
),
],
);
},
);
}