Flutter插件cutosffigen_app的简介与使用方法

Flutter插件cutosffigen_app的简介与使用方法

Flutter插件cutosffigen_app简介

cutosffigen_app 是一个全新的 Flutter FFI 插件项目。这个项目是一个起点,用于创建一个专门包含本地代码的包,该本地代码可以通过 Dart FFI 直接调用。

Flutter插件cutosffigen_app入门指南

这个项目作为 Flutter 的 FFI 插件的起点,它包括直接通过 Dart FFI 调用的本地代码。

项目结构

该项目使用了以下结构:

  • src:包含本地源代码以及用于将这些源代码构建为动态库的 CmakeFile.txt 文件。
  • lib:包含定义插件 API 的 Dart 代码,并使用 dart:ffi 调用本地代码。
  • 平台文件夹(如 androidioswindows 等):包含用于构建和捆绑本地代码库的构建文件。

构建和捆绑本地代码

pubspec.yaml 文件中指定 FFI 插件的方式如下:

plugin:
  platforms:
    some_platform:
      ffiPlugin: true

这种配置会针对各种目标平台调用本地构建,并将二进制文件捆绑到使用这些 FFI 插件的 Flutter 应用程序中。

可以结合 dartPluginClass 使用,例如当 FFI 用于实现联合插件中的一个平台时:

plugin:
  implements: some_other_plugin
  platforms:
    some_platform:
      dartPluginClass: SomeClass
      ffiPlugin: true

一个插件可以同时具有 FFI 和方法通道:

plugin:
  platforms:
    some_platform:
      pluginClass: SomeName
      ffiPlugin: true

通过 FFI(以及方法通道)插件调用的本地构建系统包括:

  • 对于 Android:Gradle,它调用 Android NDK 进行本地构建。
  • 对于 iOS 和 MacOS:Xcode,通过 CocoaPods。
  • 对于 Linux 和 Windows:CMake。

绑定到本地代码

要使用本地代码,需要在 Dart 中进行绑定。为了避免手动编写这些绑定,它们可以从头文件(src/cutosffigen_app.h)生成,使用 package:ffigen。重新生成绑定可以通过运行 flutter pub run ffigen --config ffigen.yaml 来完成。

调用本地代码

非常短的本地函数可以直接从任何隔离区调用。例如,参见 lib/cutosffigen_app.dart 中的 sum 函数。

对于长时间运行的函数,应该在一个辅助隔离区中调用,以避免在 Flutter 应用程序中丢帧。例如,参见 lib/cutosffigen_app.dart 中的 sumAsync 函数。

示例代码

下面是使用 cutosffigen_app 插件的完整示例代码:

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

import 'package:cutosffigen_app/cutosffigen_app.dart' as cutosffigen_app;

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

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

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

class _MyAppState extends State<MyApp> {
  late int sumResult;
  late Future<int> sumAsyncResult;

  [@override](/user/override)
  void initState() {
    super.initState();
    // 调用本地函数 sum
    sumResult = cutosffigen_app.sum(1, 2);
    // 调用异步本地函数 sumAsync
    sumAsyncResult = cutosffigen_app.sumAsync(3, 4);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 25);
    const spacerSmall = SizedBox(height: 10);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Native Packages'),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              children: [
                const Text(
                  'This calls a native function through FFI that is shipped as source in the package. '
                  'The native code is built as part of the Flutter Runner build.',
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
                spacerSmall,
                // 显示同步调用的结果
                Text(
                  'sum(1, 2) = $sumResult',
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
                spacerSmall,
                // 使用 FutureBuilder 显示异步调用的结果
                FutureBuilder<int>(
                  future: sumAsyncResult,
                  builder: (BuildContext context, AsyncSnapshot<int> value) {
                    final displayValue =
                        (value.hasData) ? value.data.toString() : 'loading';
                    return Text(
                      'await sumAsync(3, 4) = $displayValue',
                      style: textStyle,
                      textAlign: TextAlign.center,
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter插件cutosffigen_app的简介与使用方法的实战教程也可以访问 https://www.itying.com/category-92-b0.html

回到顶部