Flutter WhatsApp调用插件whatsapp_call的使用
whatsapp_call介绍
一个用于在Flutter应用中调用WhatsApp的新插件项目。
这些代码并非由作者创建,作者只是将其迁移以支持最新版本。 来源: https://pub.dev/packages/call_with_whatsapp
使用示例
以下是一个完整的示例代码,展示如何使用whatsapp_call
插件来实现WhatsApp通话功能。
示例代码
import 'package:flutter/material.dart';
import 'package:call_with_whatsapp/call_with_whatsapp.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// 控制器用于监听权限状态
final _permission = ValueNotifier<bool>(true);
@override
void initState() {
super.initState();
// 请求权限
_requestPermission();
}
// 请求权限
void _requestPermission() {
CallWithWhatsapp.requestPermissions().then((x) {
print("success");
}).catchError((e) {
print(e);
// 如果权限请求失败,则设置权限为false
_permission.value = false;
});
}
// 初始化呼叫
void _initiateCall() {
CallWithWhatsapp.initiateCall("+6281215951538").then((x) {
print("success");
}).catchError((e) {
print(e);
});
}
// 拨号盘按钮点击事件
void _callNumber(String phoneNumber) {
CallWithWhatsapp.initiateCall(phoneNumber).then((x) {
print("success");
}).catchError((e) async {
print(e);
// 如果没有联系人,则尝试插入新联系人
if (e == 'NO_CONTACT') {
await CallWithWhatsapp.createContact("Mas Zian", phoneNumber);
Future.delayed(Duration(seconds: 10)).then((value) {
CallWithWhatsapp.initiateCall(phoneNumber);
});
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('WhatsApp 调用插件示例'),
),
body: ValueListenableBuilder<bool>(
valueListenable: _permission,
builder: (context, value, child) {
if (!_permission.value) {
return Center(
child: Text('权限未授予,请允许访问联系人!'),
);
} else {
return _body();
}
},
),
),
);
}
// 主页面布局
Widget _body() {
return SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: _requestPermission,
child: const Text("请求权限"),
),
ElevatedButton(
onPressed: _initiateCall,
child: const Text("发起呼叫"),
),
],
),
DialPad(
dialButtonIconColor: Colors.indigo,
keyPadColor: Colors.lightBlue,
dialButtonColor: Colors.greenAccent[400],
buttonIcon: Icons.call,
backspaceButtonIconColor: Colors.blue,
useNumber: (number) {
print(number);
_callNumber(number);
},
),
],
),
),
),
);
}
}
// 自定义拨号盘组件
class DialPad extends StatelessWidget {
final Color dialButtonIconColor;
final Color keyPadColor;
final Color dialButtonColor;
final IconData buttonIcon;
final Color backspaceButtonIconColor;
final Function(String) useNumber;
const DialPad({
required this.dialButtonIconColor,
required this.keyPadColor,
required this.dialButtonColor,
required this.buttonIcon,
required this.backspaceButtonIconColor,
required this.useNumber,
});
@override
Widget build(BuildContext context) {
return Container(
color: keyPadColor,
padding: const EdgeInsets.all(16.0),
child: Wrap(
spacing: 8.0,
runSpacing: 8.0,
children: List.generate(10, (index) {
if (index == 0) {
return ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.backspace, color: backspaceButtonIconColor),
label: Text("Back"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(dialButtonColor),
),
);
}
return ElevatedButton(
onPressed: () {
useNumber(index.toString());
},
child: Text(index.toString()),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(dialButtonColor),
),
);
}),
),
);
}
}
功能说明
-
请求权限:
- 使用
CallWithWhatsapp.requestPermissions()
请求访问联系人的权限。 - 如果权限被拒绝,用户需要手动授予权限。
- 使用
-
发起呼叫:
- 使用
CallWithWhatsapp.initiateCall(phoneNumber)
发起WhatsApp呼叫。 - 如果联系人不存在,会抛出
NO_CONTACT
错误,此时可以尝试插入新联系人。
- 使用
-
拨号盘:
- 提供一个简单的拨号盘界面,用户可以通过点击数字按钮输入电话号码并发起呼叫。
注意事项
- 确保设备已安装WhatsApp应用。
- 在Android设备上,需要在
AndroidManifest.xml
中添加必要的权限:<uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
更多关于Flutter WhatsApp调用插件whatsapp_call的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter WhatsApp调用插件whatsapp_call的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中调用WhatsApp的功能,可以使用第三方插件 whatsapp_call
来实现。这个插件允许你通过Flutter应用程序直接调用WhatsApp的拨号功能。以下是如何使用 whatsapp_call
插件的步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 whatsapp_call
插件的依赖。
dependencies:
flutter:
sdk: flutter
whatsapp_call: ^0.0.1 # 请确保使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入插件
在你的Dart文件中导入 whatsapp_call
插件。
import 'package:whatsapp_call/whatsapp_call.dart';
3. 调用WhatsApp拨号功能
使用 WhatsappCall.call
方法来调用WhatsApp的拨号功能。你需要传入一个电话号码作为参数。
void callWhatsApp(String phoneNumber) async {
try {
await WhatsappCall.call(phoneNumber);
} catch (e) {
print("Error calling WhatsApp: $e");
}
}
4. 使用示例
你可以在按钮的 onPressed
事件中调用 callWhatsApp
方法。
import 'package:flutter/material.dart';
import 'package:whatsapp_call/whatsapp_call.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('WhatsApp Call Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
callWhatsApp('+1234567890'); // 替换为你要拨打的电话号码
},
child: Text('Call WhatsApp'),
),
),
),
);
}
}
void callWhatsApp(String phoneNumber) async {
try {
await WhatsappCall.call(phoneNumber);
} catch (e) {
print("Error calling WhatsApp: $e");
}
}