鸿蒙harmonyos next flutter混合开发之开发package

​​​​​​

  • 创建 package

    flutter create --template=package mypackage

package代码如下:

创建hello_world.dart

复制代码
///HelloWorld返回hello world 拼接param
class HelloWorld {
  String helloWorld(String param) => "hello world ${param}";
}

导出hello_world.dart

复制代码
library mypackage;

export 'src/hello_world.dart';
  • 创建flutter项目

    flutter create myflutter

  • myflutter pubspec.yaml中引入myPackage

    dependencies:
    flutter:
    sdk: flutter
    cupertino_icons: ^1.0.2
    mypackage:
    path: /Users/administrator/Desktop/workspace/mypackage

  • myflutter中调用

    import 'package:flutter/material.dart';
    import 'package:mypackage/mypackage.dart';

    void main() {
    runApp(const MyApp());
    }

    class MyApp extends StatelessWidget {
    const MyApp({super.key});

    复制代码
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      );
    }

    }

    class MyHomePage extends StatefulWidget {
    const MyHomePage({super.key, required this.title});

    复制代码
    final String title;
    
    @override
    State<MyHomePage> createState() => _MyHomePageState();

    }

    class _MyHomePageState extends State<MyHomePage> {
    String helloWorld ="";

    复制代码
    @override
    void initState() {
      super.initState();
      helloWorld = HelloWorld().helloWorld("我是拼接的字符串!");
    }
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("你好世界"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('${helloWorld}',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 26),)
            ],
          ),
        ),
      );
    }

    }

  • 效果展示

相关推荐
fruge15 小时前
提升 HarmonyOS 开发效率:DevEco Studio 6.0 热更新调试模式全指南
华为·harmonyos
2501_9197490315 小时前
鸿蒙:使用Image组件展示相册图片或rawfile图片
华为·harmonyos
2501_9197490315 小时前
鸿蒙:PersistenceV2页面间持久化存储数据
华为·harmonyos
yenggd17 小时前
QoS之拥塞管理两种配置方法
网络·华为
一尘之中1 天前
操作系统内核架构深度解析:从单内核、微内核到鸿蒙分布式设计
架构·harmonyos·ai写作
咕噜企业签名分发-淼淼1 天前
App防止恶意截屏功能的方法:iOS、Android和鸿蒙系统的实现方案
android·ios·harmonyos
安卓开发者1 天前
鸿蒙NEXT NearLink Kit入门指南:重新定义短距无线通信
华为·harmonyos
科技风向标1 天前
2025 随身 WiFi 行业报告:格行 WiFi6 技术下放百元市场,中兴华为机型竞争力分析;五款机型芯片方案 / 网速 / 质保深度横评
网络·科技·物联网·华为·随身wifi·格行
想不明白的过度思考者1 天前
鸿蒙系统:不止于“手机OS”的全场景智能操作系统
华为·智能手机·harmonyos
IT WorryFree1 天前
华为光模块命名规则
华为