Flutter意图跳转插件flutter_intent_forzzh的使用

Flutter意图跳转插件flutter_intent_forzzh的使用

用于启动原生系统(Android/iOS)界面,或者跳转三方App的插件。如果是跳转三方应用,请先确保你的移动设备上已安装该第三方App。


Android 使用说明:

1. 在 AndroidManifest.xml 中添加需要启动的包名配置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zzh.intent.native_intent_example">

    ...
    <queries>
        <!-- 配置需要启动的应用包名 -->
        <package android:name="com.taobao.taobao" />
    </queries>

</manifest>

2. 通过 Scheme 启动

NativeIntent intent = NativeIntent(
   action: AndroidIntent.ACTION_VIEW,
   data: "taobao://s.click.taobao.com/NMJ5nJu",
);
    
intent.launch();

3. 通过 AppId 启动

NativeIntent intent = NativeIntent(
  action: AndroidIntent.ACTION_VIEW,
  package: 'com.taobao.taobao',
);
intent.launch();

4. 跳转系统界面

NativeIntent intent = const NativeIntent(
  action: Settings.ACTION_NFC_SETTINGS, // 跳转系统 NFC 设置
);
intent.launch();

iOS 使用说明(iOS 需要 10.0 及以上版本):

1. 在 [Info.plist] 中添加 Scheme 白名单

<key>LSApplicationQueriesSchemes</key>
<array>
    <!-- URL Scheme 白名单 -->
    <string>taobao</string>
    <string>vipshop</string>
    <!-- 其他需要的白名单 -->
    <!-- URL Scheme 白名单 -->
</array>

2. 启动方式同上,传入参数 data 必须是有效值

var intent = NativeIntent(
  data: "taobao://s.click.taobao.com/NMJ5nJu",
);
intent.launch();

3. 或者启动系统界面

NativeIntent intent = const NativeIntent(
    action: IOSIntent.APPSTORE, // 跳转 App Store
);
intent.launch();

完整示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 中使用 flutter_intent_forzzh 插件来实现意图跳转。

示例代码

import 'package:flutter/material.dart';
import 'package:flutter_intent_forzzh/native_intent_lib.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Intent Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              buildButton("启动淘宝", () {
                NativeIntent intent = NativeIntent(
                  action: AndroidIntent.ACTION_VIEW,
                  data: "taobao://s.click.taobao.com/NMJ5nJu",
                );
                intent.launch();
              }),

              buildButton("跳转设置 (NFC)", () {
                NativeIntent intent = const NativeIntent(
                  action: Settings.ACTION_NFC_SETTINGS,
                );
                intent.launch();
              }),

              buildButton("跳转 App Store", () {
                NativeIntent intent = const NativeIntent(
                  action: IOSIntent.APPSTORE,
                );
                intent.launch();
              }),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildButton(String title, Function function) {
    return GestureDetector(
      onTap: () {
        function.call();
      },
      child: Container(
        width: 200,
        height: 35,
        margin: const EdgeInsets.only(top: 20),
        alignment: Alignment.center,
        decoration: const BoxDecoration(
          color: Colors.lightBlueAccent,
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        child: Text(title),
      ),
    );
  }
}

更多关于Flutter意图跳转插件flutter_intent_forzzh的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter意图跳转插件flutter_intent_forzzh的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_intent_forzzh 是一个用于在 Flutter 中实现 Android 意图(Intent)跳转的插件。它允许你在 Flutter 应用中调用 Android 的原生功能,如打开其他应用、拨打电话、发送短信等。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 flutter_intent_forzzh 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_intent_forzzh: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装插件。

使用插件

以下是一些常见的使用场景和示例代码:

1. 打开一个 URL

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Intent Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              var intent = FlutterIntentForzzh()
                ..setAction(Action.ACTION_VIEW)
                ..setData(Uri.parse('https://www.example.com'));
              await intent.launch();
            },
            child: Text('Open URL'),
          ),
        ),
      ),
    );
  }
}

2. 拨打电话

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Intent Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              var intent = FlutterIntentForzzh()
                ..setAction(Action.ACTION_DIAL)
                ..setData(Uri.parse('tel:1234567890'));
              await intent.launch();
            },
            child: Text('Call Phone'),
          ),
        ),
      ),
    );
  }
}

3. 发送短信

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Intent Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              var intent = FlutterIntentForzzh()
                ..setAction(Action.ACTION_SENDTO)
                ..setData(Uri.parse('smsto:1234567890'))
                ..putExtra('sms_body', 'Hello, this is a test message.');
              await intent.launch();
            },
            child: Text('Send SMS'),
          ),
        ),
      ),
    );
  }
}

4. 打开地图

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Intent Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              var intent = FlutterIntentForzzh()
                ..setAction(Action.ACTION_VIEW)
                ..setData(Uri.parse('geo:37.7749,-122.4194?q=restaurants'));
              await intent.launch();
            },
            child: Text('Open Map'),
          ),
        ),
      ),
    );
  }
}

注意事项

  1. 权限:某些操作(如拨打电话、发送短信)可能需要特定的权限。你需要在 AndroidManifest.xml 中添加相应的权限声明。

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
回到顶部