当前位置:   article > 正文

Flutter鸿蒙项目—如何添加对华为鸿蒙系统的支持_flutter 适配鸿蒙

flutter 适配鸿蒙

1.DevEco Studio 4.0 Release版鸿蒙环境安装

打开IDE,按照引导,安装环境即可,注意,nodejs选择 v16.20

Mac下,在.bash_profile配置如下环境变量

  1. export PATH=/Users/用户名/Documents/DevEcoProjects/dev/bin:$PATH
  2. export NODE_HOME=/Users/用户名/nodejs
  3. export PATH=$NODE_HOME/bin:$PATH
  4. export OHPM_HOME=/Users/用户名/Library/Huawei/ohpm
  5. export PATH=${PATH}:${OHPM_HOME}/bin
  6. export HOS_SDK_HOME=/Users/用户名/Library/Huawei/Sdk
  7. export OHOS_SDK_HOME=/Users/用户名/Library/OpenHarmony/Sdk
  8. export HDC_HOME=/Users/用户名/Library/Huawei/Sdk/openharmony/9/toolchains
  9. export PATH=$PATH:$HDC_HOME
  10. export FLUTTER_GIT_URL=https://gitee.com/openharmony-sig/flutter_flutter.git

2.创建flutter鸿蒙项目

使用命令行flutter create --platforms ohos hellohoos_flutter创建hellohoos_flutter项目

  1. ksnowlv@MacBook-Pro-3 DevEcoProjects % flutter create --platforms ios,android,linux,macos,windows,web,ohos hello_flutter
  2. Signing iOS app for device deployment using developer identity: "Apple Development: ksnowlv@163.com (GQNQ24T735)"
  3. Creating project hello_flutter...
  4. Running "flutter pub get" in hello_flutter...
  5. Resolving dependencies in hello_flutter... (1.0s)
  6. + async 2.10.0 (2.11.0 available)
  7. + boolean_selector 2.1.1
  8. + characters 1.2.1 (1.3.0 available)
  9. + clock 1.1.1
  10. + collection 1.17.0 (1.18.0 available)
  11. + cupertino_icons 1.0.6
  12. + fake_async 1.3.1
  13. + flutter 0.0.0 from sdk flutter
  14. + flutter_lints 2.0.3 (3.0.2 available)
  15. + flutter_test 0.0.0 from sdk flutter
  16. + js 0.6.5 (0.7.1 available)
  17. + lints 2.0.1 (3.0.0 available)
  18. + matcher 0.12.13 (0.12.16+1 available)
  19. + material_color_utilities 0.2.0 (0.11.1 available)
  20. + meta 1.8.0 (1.15.0 available)
  21. + path 1.8.2 (1.9.0 available)
  22. + sky_engine 0.0.99 from sdk flutter
  23. + source_span 1.9.1 (1.10.0 available)
  24. + stack_trace 1.11.0 (1.11.1 available)
  25. + stream_channel 2.1.1 (2.1.2 available)
  26. + string_scanner 1.2.0
  27. + term_glyph 1.2.1
  28. + test_api 0.4.16 (0.7.1 available)
  29. + vector_math 2.1.4
  30. Changed 24 dependencies in hello_flutter!
  31. OhosDependenciesManager: it no need to add plugins dependencies.
  32. Wrote 163 files.
  33. All done!
  34. You can find general documentation for Flutter at: https://docs.flutter.dev/
  35. Detailed API documentation is available at: https://api.flutter.dev/
  36. If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev
  37. In order to run your application, type:
  38. $ cd hello_flutter
  39. $ flutter run
  40. Your application code is in hello_flutter/lib/main.dart.

3.flutter项目示例

在默认代码中,添加

  1. const Text(
  2. 'flutter hoos demo',
  3. style: TextStyle(fontSize: 30, color: Colors.red),
  4. ),
  5. const SizedBox(height: 20,),

main.dart

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({super.key});
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  16. );
  17. }
  18. }
  19. class MyHomePage extends StatefulWidget {
  20. const MyHomePage({super.key, required this.title});
  21. final String title;
  22. @override
  23. State<MyHomePage> createState() => _MyHomePageState();
  24. }
  25. class _MyHomePageState extends State<MyHomePage> {
  26. int _counter = 0;
  27. void _incrementCounter() {
  28. setState(() {
  29. _counter++;
  30. });
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. appBar: AppBar(
  36. title: Text(widget.title),
  37. ),
  38. body: Center(
  39. child: Column(
  40. mainAxisAlignment: MainAxisAlignment.center,
  41. children: <Widget>[
  42. const Text(
  43. 'flutter hoos demo',
  44. style: TextStyle(fontSize: 30, color: Colors.red),
  45. ),
  46. const SizedBox(height: 20,),
  47. const Text(
  48. 'You have pushed the button this many times:',
  49. ),
  50. Text(
  51. '$_counter',
  52. style: Theme.of(context).textTheme.headlineMedium,
  53. ),
  54. ],
  55. ),
  56. ),
  57. floatingActionButton: FloatingActionButton(
  58. onPressed: _incrementCounter,
  59. tooltip: 'Increment',
  60. child: const Icon(Icons.add),
  61. ), // This trailing comma makes auto-formatting nicer for build methods.
  62. );
  63. }
  64. }

4.鸿蒙系统手机运行

切换到hello_flutter目录,执行flutter run

我的是华为P30,选择[1]: ELE AL00 (GBG5T19710007532)即可

  1. ksnowlv@MacBook-Pro-3 DevEcoProjects % cd hello_flutter
  2. ksnowlv@MacBook-Pro-3 hello_flutter % flutter run
  3. OhosDependenciesManager: it no need to add plugins dependencies.
  4. Using hardware rendering with device sdk gphone x86. If you notice graphics artifacts, consider enabling software rendering with "--enable-software-rendering".
  5. Launching lib/main.dart on sdk gphone x86 in debug mode...
  6. Running Gradle task 'assembleDebug'...
  7. Checking the license for package Android SDK Platform 33 in /Users/ksnowlv/Library/Android/sdk/licenses
  8. License for package Android SDK Platform 33 accepted.
  9. Preparing "Install Android SDK Platform 33 (revision: 3)".
  10. "Install Android SDK Platform 33 (revision: 3)" ready.
  11. Installing Android SDK Platform 33 in /Users/ksnowlv/Library/Android/sdk/platforms/android-33
  12. "Install Android SDK Platform 33 (revision: 3)" complete.
  13. "Install Android SDK Platform 33 (revision: 3)" finished.
  14. Multiple devices found:
  15. ELE AL00 (mobile) • GBG5T19710007532 • android-arm64 • Android 10 (API 29)
  16. Phone (mobile) • emulator-5554 • android-x64 • Android 12 (API 31) (emulator)
  17. macOS (desktop) • macos • darwin-x64 • macOS 14.4.1 23E224 darwin-x64
  18. Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.119
  19. [1]: ELE AL00 (GBG5T19710007532)
  20. [2]: Phone (emulator-5554)
  21. [3]: macOS (macos)
  22. [4]: Chrome (chrome)
  23. Please choose one (To quit, press "q/Q"): 1
  24. OhosDependenciesManager: it no need to add plugins dependencies.
  25. Launching lib/main.dart on ELE AL00 in debug mode...
  26. Running Gradle task 'assembleDebug'... 10.4s
  27. ✓ Built build/app/outputs/flutter-apk/app-debug.apk.
  28. Syncing files to device ELE AL00...
  29. W/Gralloc3(28193): mapper 3.x is not supported
  30. I/HwViewRootImpl(28193): removeInvalidNode jank list is null
  31. Syncing files to device ELE AL00... 282ms
  32. Flutter run key commands.
  33. r Hot reload.
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/900320
    推荐阅读
    相关标签