鸿蒙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,
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    }

    }

  • 效果展示

相关推荐
iFlyCai9 小时前
Flutter原理深度解析:从架构到渲染的全面剖析(二)
flutter·flutter 三棵树
里欧跑得慢11 小时前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
iFlyCai20 小时前
深入理解Flutter:StatefulWidget生命周期全解析(一)
flutter·dart·状态管理·statefulwidget
xiangxiongfly9151 天前
Flutter video_player总结
flutter·video_player·chewie
见山是山-见水是水2 天前
鸿蒙版 Flutter Video 视频播放组件:播放控制、全屏切换与倍速播放
flutter·音视频·harmonyos
六年码农2 天前
2026最新开源 屏译ScreenTranslator 0.4.4 开源 全屏实时翻译教程
运维·人工智能·flutter·开源
WeiAreYoung2 天前
Flutter+iOS 实现 Live Activity
flutter·ios
恋猫de小郭2 天前
超好用 R8 Configuration Analyzer, 优化你的 App 大小和内存
android·前端·flutter
键盘飞行员2 天前
Flutter App 全套实战学习计划:从零搭建到 APK 部署
学习·flutter
GitLqr3 天前
Flutter 实战:图片上传前的压缩优化,解决带宽与存储的“大户”问题
flutter·全栈·dart