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

    }

  • 效果展示

相关推荐
嘟嘟叽1 天前
初学 flutter 环境变量配置
flutter
iFlyCai1 天前
深入理解Flutter生命周期函数之StatefulWidget(一)
flutter·生命周期·dart·statefulwidget
sunly_1 天前
Flutter:photo_view图片预览功能
android·javascript·flutter
Summer不秃1 天前
Flutter中sqflite的使用案例
flutter
sunly_1 天前
Flutter:TweenAnimationBuilder自定义隐式动画
flutter
AiFlutter1 天前
Flutter-Web首次加载时添加动画
前端·flutter
Allen Su2 天前
【Flutter 问题系列第 84 篇】如何清除指定网络图片的缓存
flutter·缓存·如何清除指定网络图片的缓存·网络图片缓存
sunly_2 天前
Flutter:key的作用原理(LocalKey ,GlobalKey)
开发语言·javascript·flutter
AiFlutter2 天前
Flutter-flutter_blue_plus打包后无法进行设备扫描
flutter
fifiAmx2 天前
Flutter 扫描二维码
flutter