Flutter付费墙界面插件paywall_ui的使用

Flutter付费墙界面插件paywall_ui的使用

特性

目前该插件仅包含两种布局。

  • OTP:适用于首选一次性支付选项。
  • Multiple Options:适合提供2到3个订阅选项或一次性支付选项。
    Multiple Options 支持垂直布局(默认)和水平布局。

开始使用

要求

在开始之前,请确保您的项目已正确配置并支持 Flutter 和 Dart。

使用方法

以下是一个简单的示例,展示如何在 Flutter 应用中使用 paywall_ui 插件。

示例代码

示例路径

lib/app/modules/paywall/view.dart

完整代码示例

Container(
    child: PaywallUi().oneTimePayment(
        features: ['🔓 feature1', '⌚️ feature2'], // 列表显示功能描述
        paywallStyle: OTPPaywallStyle( // 设置一次性支付样式
            featuresSize: 18, // 设置功能文字大小
        ),
        paywallSettings: PaywallSettings( // 配置按钮点击事件等
            localizedPrice: "\$ 4.99", // 显示价格
            onPressedBackButton: () => Get.back(), // 返回按钮点击事件
            onPressedPurchaseButton: () async { // 购买按钮点击事件
                /// 模拟购买操作
                /// call.purchase(product);
                await Future.delayed(const Duration(seconds: 3)); // 模拟延迟
            },
            onPressedRestoreButton: () async => await Future.delayed(const Duration(seconds: 3)), // 恢复按钮点击事件
            onPressedPrivacyButton: () => Get.defaultDialog( // 隐私政策按钮点击事件
                title: "隐私政策",
                middleText: "隐私政策链接",
            ),
            onPressedTermsButton: () => Get.defaultDialog( // 使用条款按钮点击事件
                title: "使用条款",
                middleText: "使用条款链接",
            ),
        ),
    ),
)

额外信息

您可以在此包的基础上通过提交 PR 在 GitHub 上进行扩展和修改。


paywall_ui

以上内容展示了如何使用 paywall_ui 插件来实现付费墙界面。以下是完整的示例代码及其运行效果:

示例代码

示例路径

example/lib/main.dart

完整代码示例

import 'package:example/app/routes/pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  runApp(
    GetMaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      getPages: AppPages.pages,
      initialRoute: Routes.HOME,
      fallbackLocale: const Locale('en', 'US'),
      locale: Get.locale,
      defaultTransition: Transition.cupertino,
    ),
  );
}

更多关于Flutter付费墙界面插件paywall_ui的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter付费墙界面插件paywall_ui的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


paywall_ui 是一个用于在 Flutter 应用中实现付费墙界面的插件。它提供了一些预定义的 UI 组件,帮助开发者快速构建美观的付费墙界面。以下是如何使用 paywall_ui 插件的基本步骤:

1. 添加依赖

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

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

然后运行 flutter pub get 来安装依赖。

2. 导入包

在你的 Dart 文件中导入 paywall_ui 包:

import 'package:paywall_ui/paywall_ui.dart';

3. 使用 PaywallUI 组件

paywall_ui 提供了 PaywallUI 组件,你可以直接使用它来构建付费墙界面。以下是一个简单的示例:

class PaywallScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Premium Features'),
      ),
      body: PaywallUI(
        title: 'Unlock Premium Features',
        description: 'Get access to all premium features with a subscription.',
        features: [
          PaywallFeature(
            icon: Icons.star,
            title: 'Ad-Free Experience',
            description: 'Enjoy an ad-free experience across the app.',
          ),
          PaywallFeature(
            icon: Icons.lock_open,
            title: 'Unlock All Content',
            description: 'Access all exclusive content and features.',
          ),
          PaywallFeature(
            icon: Icons.cloud_download,
            title: 'Offline Access',
            description: 'Download content and access it offline.',
          ),
        ],
        subscriptionOptions: [
          SubscriptionOption(
            title: 'Monthly',
            price: '\$4.99/month',
            onTap: () {
              // Handle monthly subscription
            },
          ),
          SubscriptionOption(
            title: 'Yearly',
            price: '\$49.99/year',
            onTap: () {
              // Handle yearly subscription
            },
          ),
        ],
        onRestorePurchase: () {
          // Handle restore purchase
        },
        onPrivacyPolicy: () {
          // Handle privacy policy
        },
        onTermsOfService: () {
          // Handle terms of service
        },
      ),
    );
  }
}

4. 自定义 PaywallUI

PaywallUI 组件提供了多个参数,允许你自定义付费墙的外观和行为。以下是一些常用的参数:

  • title: 付费墙的标题。
  • description: 付费墙的描述。
  • features: 一个 PaywallFeature 列表,显示付费墙的功能。
  • subscriptionOptions: 一个 SubscriptionOption 列表,显示订阅选项。
  • onRestorePurchase: 恢复购买的回调函数。
  • onPrivacyPolicy: 隐私政策的回调函数。
  • onTermsOfService: 服务条款的回调函数。

5. 处理订阅逻辑

在实际应用中,你需要处理订阅逻辑。你可以使用 in_app_purchase 插件或其他支付服务来实现订阅功能。在 SubscriptionOptiononTap 回调中,你可以触发订阅流程。

6. 运行应用

完成上述步骤后,你可以运行你的 Flutter 应用,查看付费墙界面的效果。

7. 进一步定制

如果你需要进一步定制付费墙界面,你可以查看 paywall_ui 的源码,并根据需要进行修改或扩展。

示例代码

以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: PaywallScreen(),
    );
  }
}

class PaywallScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Premium Features'),
      ),
      body: PaywallUI(
        title: 'Unlock Premium Features',
        description: 'Get access to all premium features with a subscription.',
        features: [
          PaywallFeature(
            icon: Icons.star,
            title: 'Ad-Free Experience',
            description: 'Enjoy an ad-free experience across the app.',
          ),
          PaywallFeature(
            icon: Icons.lock_open,
            title: 'Unlock All Content',
            description: 'Access all exclusive content and features.',
          ),
          PaywallFeature(
            icon: Icons.cloud_download,
            title: 'Offline Access',
            description: 'Download content and access it offline.',
          ),
        ],
        subscriptionOptions: [
          SubscriptionOption(
            title: 'Monthly',
            price: '\$4.99/month',
            onTap: () {
              // Handle monthly subscription
            },
          ),
          SubscriptionOption(
            title: 'Yearly',
            price: '\$49.99/year',
            onTap: () {
              // Handle yearly subscription
            },
          ),
        ],
        onRestorePurchase: () {
          // Handle restore purchase
        },
        onPrivacyPolicy: () {
          // Handle privacy policy
        },
        onTermsOfService: () {
          // Handle terms of service
        },
      ),
    );
  }
}
回到顶部