Flutter推广链接管理插件flutter_referral_link的使用

Flutter推广链接管理插件flutter_referral_link的使用

通过我们的综合推荐链接服务,您可以最大化您的影响力并扩展您的网络。轻松生成和分享独特的推荐链接,跟踪转化,并奖励成功的推荐。让您的社区传播信息,共同解锁新的机会。


示例代码

import 'package:flutter/material.dart';
import 'package:flutter_referral_link/flutter_referral_link.dart'; // 导入flutter_referral_link插件

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  String? _referralLink; // 用于存储生成的推荐链接

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  // 生成推荐链接的方法
  Future<void> _generateReferralLink() async {
    try {
      String link = await FlutterReferralLink.generateReferralLink(
        campaignId: 'your_campaign_id', // 替换为您的活动ID
        userId: 'user_id', // 替换为用户的唯一标识符
      );
      setState(() {
        _referralLink = link;
      });
    } catch (e) {
      print("Failed to generate referral link: $e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(height: 20), // 添加间距
            ElevatedButton(
              onPressed: _generateReferralLink, // 点击按钮生成推荐链接
              child: const Text('Generate Referral Link'),
            ),
            if (_referralLink != null)
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  'Your referral link: $_referralLink',
                  style: TextStyle(fontSize: 16),
                ),
              ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

说明

在上面的示例代码中,我们展示了如何使用flutter_referral_link插件来生成和管理推荐链接。主要步骤如下:

  1. 导入插件:首先导入flutter_referral_link插件。

    import 'package:flutter_referral_link/flutter_referral_link.dart';
    
  2. 生成推荐链接:创建一个方法_generateReferralLink来生成推荐链接。该方法调用FlutterReferralLink.generateReferralLink方法,并传入必要的参数(如活动ID和用户ID)。

    Future<void> _generateReferralLink() async {
      try {
        String link = await FlutterReferralLink.generateReferralLink(
          campaignId: 'your_campaign_id', // 替换为您的活动ID
          userId: 'user_id', // 替换为用户的唯一标识符
        );
        setState(() {
          _referralLink = link;
        });
      } catch (e) {
        print("Failed to generate referral link: $e");
      }
    }
    
  3. 显示推荐链接:在UI中添加一个按钮,当点击时调用_generateReferralLink方法生成推荐链接。如果生成成功,则将链接显示在屏幕上。

    ElevatedButton(
      onPressed: _generateReferralLink,
      child: const Text('Generate Referral Link'),
    ),
    if (_referralLink != null)
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(
          'Your referral link: $_referralLink',
          style: TextStyle(fontSize: 16),
        ),
      ),
    

更多关于Flutter推广链接管理插件flutter_referral_link的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter推广链接管理插件flutter_referral_link的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_referral_link 是一个用于管理推广链接的 Flutter 插件。它可以帮助你在应用中跟踪用户的推广链接,并根据链接中的参数执行相应的操作,比如记录用户的来源、给予奖励等。

安装

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

dependencies:
  flutter:
    sdk: flutter
  flutter_referral_link: ^0.1.0  # 请检查最新版本

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

基本用法

  1. 初始化插件:

    在使用插件之前,你需要在应用的 main 函数中初始化插件:

    import 'package:flutter_referral_link/flutter_referral_link.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await FlutterReferralLink.init();
      runApp(MyApp());
    }
    
  2. 获取推广链接参数:

    你可以在应用的任何地方获取推广链接的参数。通常,你会在应用的首页或用户首次启动应用时获取这些参数。

    import 'package:flutter_referral_link/flutter_referral_link.dart';
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      Map<String, String>? referralParams;
    
      @override
      void initState() {
        super.initState();
        _getReferralParams();
      }
    
      Future<void> _getReferralParams() async {
        referralParams = await FlutterReferralLink.getReferralParams();
        if (referralParams != null) {
          // 根据参数执行相应操作,比如记录用户来源、给予奖励等
          print('Referral Params: $referralParams');
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Flutter Referral Link Example'),
          ),
          body: Center(
            child: Text('Referral Params: $referralParams'),
          ),
        );
      }
    }
    
  3. 处理深度链接:

    插件还支持处理深度链接,你可以在应用的 onGenerateRouteonGenerateInitialRoutes 中处理推广链接。

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          onGenerateRoute: (settings) {
            if (settings.name == '/referral') {
              // 处理推广链接
              final referralParams = settings.arguments as Map<String, String>;
              return MaterialPageRoute(
                builder: (context) {
                  return ReferralPage(referralParams: referralParams);
                },
              );
            }
            return null;
          },
          home: MyHomePage(),
        );
      }
    }
    

处理不同的平台

flutter_referral_link 插件支持 Android 和 iOS 平台。确保你在 AndroidManifest.xmlInfo.plist 文件中正确配置了深度链接。

Android

AndroidManifest.xml 中添加以下配置:

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https" android:host="yourdomain.com" />
</intent-filter>

iOS

Info.plist 中添加以下配置:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>yourdomain</string>
        </array>
    </dict>
</array>
回到顶部