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

    }

  • 效果展示

相关推荐
Sindyue7 小时前
flutter项目老是卡在Running Gradle task ‘assembleRelease‘......
flutter
西西学代码7 小时前
Flutter---泛型
flutter
写不完的程序7 小时前
Flutter 3.38 版本发布了,看看有哪些新特性
flutter
QuantumLeap丶9 小时前
《Flutter全栈开发实战指南:从零到高级》- 14 -网络请求与数据解析
flutter·ios·dart
程序员老刘9 小时前
华为小米都在布局的多屏协同,其实Android早就有了!只是你不知道...
android·flutter
清凉夏日9 小时前
Flutter 国际化完整指南
前端·flutter
猫林老师9 小时前
Flutter for HarmonyOS开发指南(九):测试、调试与质量保障体系
flutter·wpf·harmonyos
猫林老师10 小时前
Flutter for HarmonyOS开发指南(五):性能调优与性能分析全攻略
flutter·华为·harmonyos
全栈派森15 小时前
初见 Dart:这门新语言如何让你的 App「动」起来?
android·flutter·ios
恋猫de小郭16 小时前
Dart 3.10 发布,快来看有什么更新吧
android·前端·flutter