Flutter动画过渡效果插件spotflow的使用

Flutter动画过渡效果插件spotflow的使用

Flutter SDK

我们的Flutter SDK提供了丰富的预构建UI组件和API,以帮助您在Flutter应用程序中无缝集成支付功能。

简介

Spotflow Flutter SDK 包允许开发人员轻松地将无摩擦的支付功能集成到他们的Flutter应用程序中。它支持Android和iOS平台,使其适用于移动应用开发。

安装

要使用 Spotflow Flutter SDK 包,请在您的 pubspec.yaml 文件中添加以下依赖项:

dependencies:
  spotflow: ^1.0.0

然后运行 flutter pub get 来获取该包。

使用

支付

要启动 Spotflow 包,可以使用 Spotflow().start() 方法。此方法需要一个 BuildContext 和一个包含各种参数的 SpotFlowPaymentManager

以下是如何使用 Spotflow 包进行支付的一个示例:

Spotflow().start(
  context: context,
  paymentManager: SpotFlowPaymentManager(
    merchantId: "", // 商户ID
    customerEmail: "customer@example.com", // 客户邮箱
    customerName: "John Snow", // 可选参数
    customerPhoneNumber: "000-000-000", // 可选参数
    customerId: "unique_id", // 可选参数
    planId: "plan_id", // 计划ID
    amount: 10, // 金额
    key: "your_api_key", // API密钥
    encryptionKey: "encryption_key", // 加密密钥
    paymentDescription: "Product purchase", // 支付描述
    appLogo: SizedBox(), // 可选参数
  ),
  onComplete: (paymentResponseBody) {
    // 支付完成后的回调
  },
);

参数

  • context: 应用程序的 BuildContext
  • paymentManager: 包含支付详细信息的 SpotFlowPaymentManager 实例。
  • onComplete: 支付成功时可选的回调函数。

SpotFlowPaymentManager

SpotFlowPaymentManager 类需要以下参数:

  • customerEmail: 客户的电子邮件地址。
  • amount: 需要支付的金额。
  • key: 支付服务提供商的API密钥。
  • planId: 唯一标识支付计划的ID。
  • encryptionKey: 用于保护交易的加密密钥。
  • customerId(可选): 客户的唯一标识符。
  • customerName(可选): 客户的名字。
  • customerPhoneNumber(可选): 客户的电话号码。
  • paymentDescription(可选): 支付的描述。
  • appLogo(可选): 应用程序logo的图像小部件。
  • appName(可选): 应用程序的名称。

测试您的实现

使用支付服务提供商提供的测试卡来测试您的实现。

运行示例项目

我们在 GitHub仓库 提供了一个示例项目。克隆仓库并导航到示例文件夹。使用支持的IDE打开它,或者从终端运行 flutter run


示例代码

以下是完整的示例代码,展示了如何在Flutter应用程序中使用 Spotflow 插件:

// example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:spotflow/spotflow.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
        useMaterial3: true,
        textTheme: GoogleFonts.latoTextTheme(),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _startPayment() {
    Spotflow().start(
      context: context,
      paymentManager: SpotFlowPaymentManager(
        customerEmail: emailController.text,
        key: merchantKeyController.text,
        encryptionKey: encryptionKeyController.text,
        paymentDescription: paymentDescriptionController.text,
        planId: planIdController.text,
        amount: num.tryParse(amountController.text) ?? 5,
        appLogo: Image.asset('assets/images/nba-logo.png'),
      ),
    );
  }

  TextEditingController emailController = TextEditingController(text: "jon@snow.com");
  TextEditingController encryptionKeyController = TextEditingController(text: "");
  TextEditingController planIdController = TextEditingController(text: "");
  TextEditingController merchantKeyController = TextEditingController(text: "");
  TextEditingController paymentDescriptionController = TextEditingController(text: "League Pass");
  TextEditingController amountController = TextEditingController(text: "5");

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20.0),
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                const SizedBox(height: 16),
                const Text(
                  "点击下方按钮开始支付",
                  style: TextStyle(fontSize: 26, fontWeight: FontWeight.w600, color: Colors.black),
                ),
                const SizedBox(height: 32),
                TextField(
                  controller: emailController,
                  keyboardType: TextInputType.emailAddress,
                  decoration: const InputDecoration(
                    hintText: "email@sample.com",
                    border: OutlineInputBorder(),
                    enabledBorder: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: planIdController,
                  decoration: const InputDecoration(
                    hintText: '计划ID',
                    border: OutlineInputBorder(),
                    enabledBorder: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: merchantKeyController,
                  decoration: const InputDecoration(
                    hintText: '商户密钥',
                    border: OutlineInputBorder(),
                    enabledBorder: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: encryptionKeyController,
                  decoration: const InputDecoration(
                    hintText: '加密密钥',
                    border: OutlineInputBorder(),
                    enabledBorder: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: amountController,
                  keyboardType: TextInputType.number,
                  decoration: const InputDecoration(
                    hintText: '金额',
                    border: OutlineInputBorder(),
                    enabledBorder: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  controller: paymentDescriptionController,
                  decoration: const InputDecoration(
                    hintText: '支付描述(可选)',
                    border: OutlineInputBorder(),
                    enabledBorder: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                  ),
                ),
                const SizedBox(height: 64),
                ElevatedButton(
                  onPressed: () {
                    if (emailController.text.isEmpty ||
                        merchantKeyController.text.isEmpty ||
                        encryptionKeyController.text.isEmpty) {
                      const snackBar = SnackBar(
                        backgroundColor: Colors.red,
                        content: Text(
                          '请输入所有必填字段',
                          style: TextStyle(color: Colors.white, fontSize: 15),
                        ),
                      );
                      ScaffoldMessenger.of(context).showSnackBar(snackBar);
                      return;
                    }
                    if (amountController.text.isEmpty && planIdController.text.isEmpty) {
                      const snackBar = SnackBar(
                        backgroundColor: Colors.red,
                        content: Text(
                          '请至少输入金额或计划ID',
                          style: TextStyle(color: Colors.white, fontSize: 15),
                        ),
                      );
                      ScaffoldMessenger.of(context).showSnackBar(snackBar);
                      return;
                    }

                    _startPayment();
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.deepOrange,
                    elevation: 0,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
                  ),
                  child: const Padding(
                    padding: EdgeInsets.symmetric(vertical: 10.0),
                    child: Text(
                      '开始支付',
                      style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
                const SizedBox(height: 64),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter动画过渡效果插件spotflow的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter动画过渡效果插件spotflow的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


SpotFlow 是一个用于 Flutter 的动画过渡效果插件,它可以帮助开发者轻松地实现各种复杂的动画效果。SpotFlow 的核心思想是通过定义一系列的“点”(spots)和“路径”(flows),来实现流畅的动画过渡。

安装 SpotFlow

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

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

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

使用 SpotFlow

1. 基本使用

SpotFlow 的基本用法是通过 SpotFlowBuilder 来构建动画。你需要定义一个 SpotFlow 对象,并指定动画的起始点和结束点。

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

class MyAnimatedWidget extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return SpotFlowBuilder(
      spotFlow: SpotFlow(
        start: Spot(0.0, 0.0),
        end: Spot(1.0, 1.0),
        duration: Duration(seconds: 2),
      ),
      builder: (BuildContext context, Spot spot) {
        return Container(
          width: 100.0,
          height: 100.0,
          color: Colors.blue,
          transform: Matrix4.translationValues(spot.x * 100, spot.y * 100, 0),
        );
      },
    );
  }
}

在这个例子中,我们定义了一个 SpotFlow,动画从 (0.0, 0.0) 开始,到 (1.0, 1.0) 结束,持续时间为 2 秒。builder 函数会根据当前的 spot 值来构建 UI。

2. 自定义路径

你可以通过自定义 Path 来定义动画的路径。SpotFlow 支持使用 Path 对象来定义复杂的动画路径。

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

class MyAnimatedWidget extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    Path path = Path()
      ..moveTo(0.0, 0.0)
      ..quadraticBezierTo(0.5, 1.0, 1.0, 0.0);

    return SpotFlowBuilder(
      spotFlow: SpotFlow(
        path: path,
        duration: Duration(seconds: 2),
      ),
      builder: (BuildContext context, Spot spot) {
        return Container(
          width: 100.0,
          height: 100.0,
          color: Colors.blue,
          transform: Matrix4.translationValues(spot.x * 100, spot.y * 100, 0),
        );
      },
    );
  }
}

在这个例子中,我们使用 Path 定义了一个二次贝塞尔曲线路径,动画将沿着这条路径移动。

3. 多段动画

SpotFlow 还支持多段动画,你可以通过 addSpot 方法添加多个“点”,SpotFlow 会自动在这些点之间进行平滑过渡。

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

class MyAnimatedWidget extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return SpotFlowBuilder(
      spotFlow: SpotFlow(
        start: Spot(0.0, 0.0),
        duration: Duration(seconds: 2),
      )
        ..addSpot(Spot(1.0, 0.0), Duration(seconds: 1))
        ..addSpot(Spot(1.0, 1.0), Duration(seconds: 1)),
      builder: (BuildContext context, Spot spot) {
        return Container(
          width: 100.0,
          height: 100.0,
          color: Colors.blue,
          transform: Matrix4.translationValues(spot.x * 100, spot.y * 100, 0),
        );
      },
    );
  }
}
回到顶部