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

    }

  • 效果展示

相关推荐
G_dou_4 小时前
Flutter三方库适配OpenHarmony【countdown_timer】倒计时器项目完整实战
flutter·harmonyos
特立独行的猫a5 小时前
Tauri 应用移植到 OpenHarmony/鸿蒙PC完整指南
华为·rust·harmonyos·tauri·移植·鸿蒙pc
互联网散修6 小时前
鸿蒙实战:文字放大镜精确跟随手指放大
华为·harmonyos
金启攻9 小时前
【鸿蒙应用开发实战·食光篇】第二篇:首页与菜系导航——圆形封面与美食榜单
华为·harmonyos
JohnnyDeng949 小时前
【鸿蒙】ArkUI 列表性能优化:LazyForEach 与组件复用深度解析
性能优化·harmonyos·arkts·鸿蒙·arkui
●VON10 小时前
AtomGit Flutter鸿蒙客户端:设置页面
flutter·华为·跨平台·harmonyos·鸿蒙
FrameNotWork10 小时前
HarmonyOS6.1 AI 模型管理架构设计与最佳实践
人工智能·harmonyos
wordbaby10 小时前
rn-cross-calendar:一个兼容 React 18/19、RN/RNOH 的跨平台日历组件
前端·react native·harmonyos
●VON11 小时前
AtomGit Flutter鸿蒙客户端:用户资料
flutter·华为·架构·跨平台·harmonyos·鸿蒙
悟空瞎说11 小时前
Flutter 三大主流本地存储全解:SharedPreferences、Hive、SQLite 实战指南
flutter