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

    }

  • 效果展示

相关推荐
lazy_yun13 小时前
flutter插件开发-ios
flutter·ios·cocoa
火柴就是我14 小时前
flame forge2d 实现随机小怪以及飞镖射中爆炸
flutter
机器瓦力14 小时前
Flutter应用开发:返回列表刷新并保持原始操作位置
flutter
弱水三千只取一瓢编号88090818 小时前
Flutter路由工具类RouteUtils,可二次开发,拿来即用
前端·flutter·vue
迷雾漫步者1 天前
flutter组件————Row和Column
flutter·跨平台·dart
tmacfrank1 天前
Flutter 插件开发入门
flutter
任聪聪2 天前
Flutter 调试环境下浏览器网络请求跨域问题解决方案
flutter
用户26830368955972 天前
记录一次flutter项目更新到3.27
flutter
kirk_wang3 天前
Flutter适配HarmonyOS实践
flutter·华为·harmonyos
Jalor3 天前
HarmonyOS NEXT | 一文搞懂 华为账号登录(获取UnionID/OpenID)
spring boot·flutter·harmonyos