如何在 Flutter 中向我的 ListTile 添加 phone 调用操作

How Can I Add phone call action to my ListTile in Flutter

我正在为社区编写一个工具。我的目的是在 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.
    );
  }
}

您需要删除 phone 号码中的空格(如果有),以便本机 phone 应用程序能够正确设置号码格式(例如 +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');
     } 
  }