我正在写一个社区工具。我的目的是在listtile中给出姓名和数字,当人们点击名称时,它会拨打分配的号码。我尝试了url_launcher,但我无法成功。可以检查并告诉我我的代码有什么问题。这是我无法启动的代码我错过了什么吗?
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:sarisayfav1/Gas.dart';
import 'package:sarisayfav1/Kargo.dart';
import 'package:sarisayfav1/Tamir.dart';
import 'package:url_launcher/url_launcher.dart';
import 'Book.dart';
import 'Food.dart';
class Arac extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Akcakale Sari Sayfa',
theme: ThemeData(
backgroundColor: Colors.black87,
primaryColor: Colors.black,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Akçakale Sarı Sayfa'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Akçakale Sarı Sayfa Uygulaması',
style: TextStyle(color: Colors.amberAccent, fontSize: 25)),
decoration: BoxDecoration(
color: Colors.black87,
),
),
ListTile(
leading: Icon(Icons.directions_bus),
title: Text(
'Kargo Telefon Numaraları',
),
onTap: () {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Kargo()));
// Update the state of the app.
// ...
},
),
ListTile(
leading: Icon(Icons.food_bank_outlined),
title: Text(
'Lokanta-Yiyecek-İçecek',
),
onTap: () {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Food()));
// Update the state of the app.
// ...
},
),
ListTile(
leading: Icon(Icons.menu_book_rounded),
title: Text(
'Kırtasiye-Kitapçı',
),
onTap: () {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Book()));
// Update the state of the app.
// ...
},
),
ListTile(
leading: Icon(Icons.local_gas_station),
title: Text('Tüpçü-Sucu'),
onTap: () {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Gas()));
},
),
ListTile(
leading: Icon(Icons.home_repair_service),
title: Text(
'Elektrik-Su-Mobilya Tamircisi',
),
onTap: () {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Tamir()));
// Update the state of the app.
// ...
},
),
ListTile(
leading: Icon(Icons.local_taxi),
title: Text('Taşıma Hizmeti Sağlayan Özel Araç Sahipleri'),
onTap: () {
Navigator.push(context,
new MaterialPageRoute(builder: (context) => new Arac()));
},
),
],
),
),
appBar: AppBar(
leading: Icon(Icons.settings),
backgroundColor: Colors.black87,
title: Text(widget.title, style: TextStyle(color: Colors.amberAccent)),
),
body: ListTile(
leading: Icon(Icons.person),
dense: true,
onTap: () => tel:+123456789,
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
您需要删除电话号码中的空格(如果有的话),以便本机手机应用程序将正确格式化号码(例如+1 234 567 8901)。我认为这个问题只存在于iOS中,因为Android即使使用空格也能正确格式化。
import 'package:url_launcher/url_launcher.dart' as UrlLauncher;
_launchUrl(String phoneNumber) async {
try {
if (phoneNumber != null) {
phoneNumber = phoneNumber.replaceAll(" ", "");
if (await UrlLauncher.canLaunch(phoneNumber)) {
await UrlLauncher.launch(phoneNumber);
}
}
} catch(e){
print('Cannot launch url');
}
}