鸿蒙harmonyos next flutter混合开发之开发FFI plugin

  • 创建FFI plugin summation,默认创建的FFI plugin是求两个数的和

    flutter create --template=plugin_ffi summation --platforms=android,ios,ohos

  • 创建my_application

    flutter create --org com.example my_application

  • 在my_application项目中文件pubspec.yaml引用summation

    复制代码
    summation:
      path: /Users/administrator/Desktop/workspace/summation
  • my_application中调用summation

    import 'package:flutter/material.dart';
    import 'package:summation/summation.dart' as summation;

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

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

    复制代码
    @override
    State<MyApp> createState() => _MyAppState();

    }

    class _MyAppState extends State {
    int sumResult = 0;
    int sumAsyncResult = 0;

    复制代码
    @override
    void initState() {
      super.initState();
      sumResult = summation.sum(1, 2);
       summation.sumAsync(3, 4).then((value) {
         sumAsyncResult = value;
         setState(() {
    
         });
       });
    }
    
    @override
    Widget build(BuildContext context) {
      const textStyle = TextStyle(fontSize: 25);
      const spacerSmall = SizedBox(height: 10);
      return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('my_application调用ffiplugin'),
          ),
          body: SingleChildScrollView(
            child: Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                children: [
                  Text(
                    '同步求和sum(1, 2) = $sumResult',
                    style: textStyle,
                    textAlign: TextAlign.center,
                  ),
                  spacerSmall,
                  Text(
                    '异步求和sum(3, 4) = $sumAsyncResult',
                    style: textStyle,
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    }

    }

  • 效果展示

相关推荐
看昭奚恤哭1 小时前
Flutter 布局核心思想
开发语言·javascript·flutter
末代iOS程序员华仔2 小时前
iOS上架海外工具类应用合规指南:避免封号与下架风险
flutter·ios·swift
末代iOS程序员华仔4 小时前
Flutter 开发 iOS App 上架全流程与常见问题分析
flutter·ios
末代iOS程序员华仔4 小时前
Flutter开发iOS海外工具类应用上架合规指南与风险规避
flutter·ios
蜡台16 小时前
Flutter 环境搭建 Dart配置
flutter·dart
落叶飘飘s1 天前
餐饮服务与软件创新的融合:解析海底捞 APP 的 Flutter 鸿蒙开发之路
flutter·华为·harmonyos
起司喵喵1 天前
Flutter-MacOS桌面OS系统|flutter.+window_manager客户端OS模板
flutter·macos·策略模式
Wuxiaoming1352 天前
flutter app的logo和splash logo尺寸
flutter
GitLqr2 天前
深入理解 Flutter 架构:从 Widget 到 GPU 像素的全链路解析
flutter·面试·dart
SoaringHeart2 天前
Flutter进阶|最佳实践:组件内阴影实现
前端·flutter