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

    }

  • 效果展示

相关推荐
jikuaidi6yuan16 分钟前
鸿蒙系统(HarmonyOS)分布式任务调度
分布式·华为·harmonyos
Summer不秃31 分钟前
Flutter之使用mqtt进行连接和信息传输的使用案例
前端·flutter
旭日猎鹰35 分钟前
Flutter踩坑记录(二)-- GestureDetector+Expanded点击无效果
前端·javascript·flutter
sunly_36 分钟前
Flutter:AnimatedSwitcher当子元素改变时,触发动画
flutter
AiFlutter37 分钟前
Flutter封装Coap
flutter
旭日猎鹰6 小时前
Flutter踩坑记录(三)-- 更改入口执行文件
flutter
旭日猎鹰6 小时前
Flutter踩坑记录(一)debug运行生成的项目,不能手动点击运行
flutter
️ 邪神6 小时前
【Android、IOS、Flutter、鸿蒙、ReactNative 】自定义View
flutter·ios·鸿蒙·reactnative·anroid
智慧化智能化数字化方案6 小时前
华为IPD流程管理体系L1至L5最佳实践-解读
大数据·华为
ZZZCY20037 小时前
华为VER系统及CLI命令熟悉
华为