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

    }

  • 效果展示

相关推荐
克喵的水银蛇几秒前
Flutter 通用底部弹窗:ActionSheetWidget 一键实现自定义选项与交互
flutter
小a彤9 分钟前
Flutter 深度解析:跨平台开发的终极利器
flutter
_大学牲14 分钟前
Flutter 勇闯2D像素游戏之路(二):绘制加载游戏地图
flutter·游戏·游戏开发
程序员老刘17 分钟前
千万别再纠结Flutter状态管理,90%项目根本不需要选
flutter·客户端
renxhui39 分钟前
Flutter 常用组件全属性说明(持续更新中)
flutter
m0_看见流浪猫请投喂2 小时前
Flutter鸿蒙化现有三方插件兼容适配鸿蒙平台
flutter·华为·harmonyos·flutterplugin·flutter鸿蒙化
雨季6662 小时前
Flutter 智慧物流仓储服务平台:跨端协同打造高效流转生态
flutter
勇气要爆发2 小时前
【第五阶段—高级特性和框架】第十一章:Flutter屏幕适配开发技巧—变形秘籍
flutter
吃好喝好玩好睡好3 小时前
Flutter与Electron在OpenHarmony生态的融合实践:构建下一代跨平台应用
javascript·flutter·electron
ujainu3 小时前
Flutter:在平台博弈中构建跨端开发新生态
flutter