Flutter应用内购买插件indigitall_inapp_flutter_plugin的使用

Flutter应用内购买插件indigitall_inapp_flutter_plugin的使用

indigitall_inapp_flutter_plugin

这是一个新的Flutter插件项目。

使用步骤

本项目是一个用于Flutter的插件包起点,包含适用于Android和/或iOS的平台特定实现代码。

若要开始Flutter开发,可以查看在线文档,其中包含教程、示例、移动开发指南以及完整的API参考。


示例代码

以下是一个完整的示例代码,展示如何在Flutter应用中使用indigitall_inapp_flutter_plugin插件。

示例代码:example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:indigitall_inapp_flutter_plugin/core/utils/IndigitallInAppParams.dart';
import 'package:indigitall_inapp_flutter_plugin/indigitall_inapp_flutter_plugin.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> {
  String _platformVersion = 'Unknown';
  final _indigitallInappFlutterPlugin = IndigitallInappFlutterPlugin();

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 初始化插件状态
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    // try {
    //   platformVersion =
    //       await _indigitallInappFlutterPlugin.getPlatformVersion() ?? 'Unknown platform version';
    // } on PlatformException {
    //   platformVersion = 'Failed to get platform version.';
    // }

    // 配置参数
    Map params = {
      IndigitallInAppParams.PARAM_APP_KEY: "537a3609-299f-4fc1-8c5e-a74a4fa3a2d2", // 应用密钥
      IndigitallInAppParams.PARAM_LOG_DEBUG: false, // 是否开启调试日志
      IndigitallInAppParams.PARAM_DOMAIN_INAPP: "https://device-api.indigitall.com/v2", // InApp服务域名
      IndigitallInAppParams.PARAM_INAPP_CODE: "Billboard" // InApp商品代码
    };

    // 显示弹窗并处理回调
    IndigitallInappFlutterPlugin.showPopUp(params, (inapp) => {
      print("showpopu ok: $inapp"), // 成功回调
    }, (error) => {
      print("error init device ${error.errorMessage}") // 错误回调
    });

    // 如果小部件从树中移除时异步平台消息仍在飞行中,则丢弃回复而不是调用setState。
    if (!mounted) return;

    setState(() {
      // _platformVersion = platformVersion;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}

更多关于Flutter应用内购买插件indigitall_inapp_flutter_plugin的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用内购买插件indigitall_inapp_flutter_plugin的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


indigitall_inapp_flutter_plugin 是一个用于在 Flutter 应用中实现应用内购买的插件。以下是如何使用该插件的基本步骤:

1. 添加依赖

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

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

然后,运行 flutter pub get 来获取依赖。

2. 初始化插件

在你的 Flutter 应用中,你需要在应用启动时初始化 indigitall_inapp_flutter_plugin 插件。

import 'package:indigitall_inapp_flutter_plugin/indigitall_inapp_flutter_plugin.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化插件
  await IndigitallInAppFlutterPlugin.initialize(
    apiKey: 'YOUR_API_KEY',  // 替换为你的 API Key
  );

  runApp(MyApp());
}

3. 获取产品信息

你可以使用插件来获取应用内购买的产品信息。

Future<void> fetchProducts() async {
  try {
    List<InAppProduct> products = await IndigitallInAppFlutterPlugin.getProducts();
    for (var product in products) {
      print('Product ID: ${product.productId}');
      print('Price: ${product.price}');
      print('Title: ${product.title}');
    }
  } catch (e) {
    print('Failed to fetch products: $e');
  }
}

4. 发起购买

当用户选择购买某个产品时,你可以使用插件发起购买请求。

Future<void> purchaseProduct(String productId) async {
  try {
    PurchaseResult result = await IndigitallInAppFlutterPlugin.purchaseProduct(productId);
    if (result.success) {
      print('Purchase successful!');
    } else {
      print('Purchase failed: ${result.errorMessage}');
    }
  } catch (e) {
    print('Failed to purchase product: $e');
  }
}

5. 处理购买结果

你可以监听购买结果并处理相应的逻辑。

void listenToPurchases() {
  IndigitallInAppFlutterPlugin.purchaseStream.listen((PurchaseResult result) {
    if (result.success) {
      print('Purchase successful!');
      // 处理购买成功逻辑
    } else {
      print('Purchase failed: ${result.errorMessage}');
      // 处理购买失败逻辑
    }
  });
}

6. 恢复购买

对于非消耗型产品,你可以提供一个选项让用户恢复购买。

Future<void> restorePurchases() async {
  try {
    List<PurchaseResult> results = await IndigitallInAppFlutterPlugin.restorePurchases();
    for (var result in results) {
      if (result.success) {
        print('Restore successful for product: ${result.productId}');
      } else {
        print('Restore failed: ${result.errorMessage}');
      }
    }
  } catch (e) {
    print('Failed to restore purchases: $e');
  }
}

7. 处理订阅

如果你有订阅型产品,你可以使用类似的逻辑来处理订阅。

Future<void> subscribeToProduct(String productId) async {
  try {
    PurchaseResult result = await IndigitallInAppFlutterPlugin.subscribeToProduct(productId);
    if (result.success) {
      print('Subscription successful!');
    } else {
      print('Subscription failed: ${result.errorMessage}');
    }
  } catch (e) {
    print('Failed to subscribe to product: $e');
  }
}

8. 处理错误

确保在购买过程中处理可能出现的错误,例如网络问题或用户取消购买。

try {
  PurchaseResult result = await IndigitallInAppFlutterPlugin.purchaseProduct(productId);
  if (result.success) {
    print('Purchase successful!');
  } else {
    print('Purchase failed: ${result.errorMessage}');
  }
} catch (e) {
  print('Failed to purchase product: $e');
}
回到顶部