flutter_call_phone_intent 1.0.2 copy "flutter_call_phone_intent: ^1.0.2" to clipboard
flutter_call_phone_intent: ^1.0.2 copied to clipboard

An Android direct call and hang up delete call history.

example/lib/main.dart

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_call_phone_intent/flutter_call_phone_intent.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  final _flutterPhoneCallAutoPlugin = FlutterCallPhoneIntent();

  var statCallState = '';

  @override
  void initState() {
    super.initState();
    initData();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    print('App lifecycle state changed to $state');
    // 根据不同的状态执行相应的逻辑
    // if (state == AppLifecycleState.paused) {
    //   await _flutterPhoneCallAutoPlugin.endCall();
    // }
  }

  initData() async {
    await _flutterPhoneCallAutoPlugin.init();
    await _flutterPhoneCallAutoPlugin.requestPermissions();
    _flutterPhoneCallAutoPlugin.onCallStateChanged().listen((event) {
      setState(() {
        statCallState += event.toString();
      });
      log('-----------呼叫状态变化:$event------------');
      //呼叫中 OFFHOOK
      //挂断 IDLE
      //来电 RINGING
    });
  }

  onCallPhone() async {
    if (await _flutterPhoneCallAutoPlugin.checkPermissions() ?? false) {
      var number = await _flutterPhoneCallAutoPlugin.callPhone('11111122233');
      log('-----------拨打电话:$number------------');
    } else {
      await _flutterPhoneCallAutoPlugin.requestPermissions();
    }
  }

  ///挂断
  onEndPhone() async {
    await _flutterPhoneCallAutoPlugin.endCall();
  }

  ///接听
  onAnswerCall() async {
    await _flutterPhoneCallAutoPlugin.answerCall();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            InkWell(
              onTap: onCallPhone,
              child: Container(
                padding: const EdgeInsets.all(20),
                child: Text('Call Phone'),
              ),
            ),
            SizedBox(height: 20),
            InkWell(
              onTap: onEndPhone,
              child: Container(
                padding: const EdgeInsets.all(20),
                child: Text('End Call'),
              ),
            ),
            SizedBox(height: 20),
            InkWell(
              onTap: onAnswerCall,
              child: Container(
                padding: const EdgeInsets.all(20),
                child: Text('Answer Call'),
              ),
            ),
            SizedBox(
              height: 20,
            ),
            Center(
              child: Text(
                statCallState,
                style: TextStyle(color: Colors.red),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
135
points
14
downloads

Publisher

unverified uploader

Weekly Downloads

An Android direct call and hang up delete call history.

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_call_phone_intent

Packages that implement flutter_call_phone_intent