news 2026/5/3 20:46:18

Flutter for HarmonyOS 开发指南(二):Hello World

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Flutter for HarmonyOS 开发指南(二):Hello World

一:零基础快速入门Dart Flutter开发

这段代码是 Flutter 官方提供的标准“计数器”示例,也是学习 Flutter 的“Hello World”。现对它进行了一些修改(添加了全局主题配置)。

效果:

二:示例代码

main.dart代码如下:

import 'package:flutter/material.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( colorScheme: ColorScheme.fromSeed(seedColor: Colors.red.shade500), appBarTheme: const AppBarTheme( // 设置背景色(可选) // backgroundColor: Colors.white, // 设置标题文本的全局样式 titleTextStyle: TextStyle( color: Colors.black, // 全局标题颜色 fontSize: 18, // 全局字体大小 fontWeight: FontWeight.bold, ), ), ), home: const MyHomePage(title: '首页'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text('You have pushed the button this many times:'), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } }

三:代码解读

1. 入口函数 (main)

void main() { runApp(const MyApp()); }
  • main(): 这是 Dart 程序的入口,一切从这里开始。

  • runApp(): 这是 Flutter 的核心函数。它接受一个 Widget(在这里是 MyApp),并将其作为根节点挂载到屏幕上。

2. 根组件 (MyApp)

class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.red.shade500), appBarTheme: const AppBarTheme( // 设置背景色(可选) // backgroundColor: Colors.white, // 设置标题文本的全局样式 titleTextStyle: TextStyle( color: Colors.black, // 全局标题颜色 fontSize: 18, // 全局字体大小 fontWeight: FontWeight.bold, ), ), ), home: const MyHomePage(title: '首页'), ); } }
  • StatelessWidget: 表示这个组件是无状态的。也就是说,MyApp 初始化后,它自己的内部状态不会发生改变(它只是配置整个 App 的壳子)。

  • MaterialApp: 这是 Flutter 提供的一个顶层组件,它封装了 Google Material Design(安卓风格)的设计规范,提供了路由、主题等核心功能。

  • ThemeData: 定义 App 的外观。

  • ColorScheme.fromSeed: 这是 Material 3 的新特性。只需要给一个种子颜色(这里是白色),Flutter 会自动生成一套协调的配色方案(主色、副色、背景色等)。

  • appBarTheme: 这里是设置的全局样式。这意味着,只要在 App 里使用 AppBar 且不单独覆盖样式。

3. 有状态的主页 (MyHomePage)

class MyHomePage extends StatefulWidget { ... }
  • StatefulWidget: 这里变成了有状态组件。因为这个页面需要记录点击次数(_counter),数字会变化,所以界面需要随之刷新。

  • title: 这是一个参数,由父组件(MyApp)传递进来,在这里是 "首页"。

4. 状态逻辑与界面 (_MyHomePageState)

这是 MyHomePage 的“伴生”类,所有的逻辑和界面构建都在这里。

变量与逻辑
int _counter = 0; // 状态变量,记录点击次数 void _incrementCounter() { setState(() { _counter++; }); }
  • setState(() { ... }):这句最重要!在 Flutter 中,单纯改变变量 _counter++ 是不会更新界面的。必须调用 setState 通知 Flutter:“数据变了,请重新运行 build 方法刷新屏幕。”

界面构建 (build)
@override Widget build(BuildContext context) { return Scaffold( ... ); }
  • caffold: 意为“脚手架”。它实现了 Material Design 的基本页面布局结构,提供了 appBar(顶部导航栏)、body(中间内容)、floatingActionButton(悬浮按钮)等槽位。

这里的代码有一个“冲突”点:
appBar: AppBar( // 这里的设置优先级高于 MyApp 里的全局设置! backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ),
  • 注意:你在 MyApp 里设置了全局 backgroundColor: Colors.white,但是在这里(MyHomePage),代码显式地设置了 backgroundColor 为 inversePrimary(一种浅紫色)。

    • 结果:背景色会是浅紫色(局部覆盖了全局)。

    • 标题文字:因为这里没设置 TextStyle,所以它会使用你在 MyApp 定义的蓝色、18号字(全局样式生效)。

页面主体 (body)
body: Center( // 让内容居中 child: Column( // 垂直排列子组件 mainAxisAlignment: MainAxisAlignment.center, // 垂直方向居中 children: <Widget>[ const Text('You have pushed...'), // 固定文本 Text( '$_counter', // 显示计数变量 style: Theme.of(context).textTheme.headlineMedium, // 使用默认的大标题样式 ), ], ), ),

四:学习建议

  1. Widget 是积木:Flutter 也就是在拼积木。Text、Center、Column 都是积木。

  2. Stateless vs Stateful

    • 如果页面只是展示静态内容(如“关于我们”),用 StatelessWidget。

    • 如果页面有交互、数据会变(如“计数器”、“登录页”),用 StatefulWidget。

  3. 样式优先级:就像 CSS 一样,就近原则

    • MyApp 里的 theme 是全局样式。

    • 具体 Widget(如 AppBar)里的属性是局部样式,会覆盖全局样式。

欢迎加入开源鸿蒙跨平台社区 https://openharmonycrossplatform.csdn.net

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/3 19:12:49

sparse4D V2核心要点

这个图是sparseV2的结构&#xff0c;单帧网络输出的instance和历史帧的instance是如何在多帧网络里融合的&#xff1f;因为单帧网络基于当前img检出的结果肯定跟历史帧是有重叠的&#xff0c;初读文章的疑问是&#xff1a;如何把重合的这部分一一对应上呢一句话先给结论&#x…

作者头像 李华
网站建设 2026/4/28 20:19:11

学霸同款2026 AI论文工具TOP9:自考毕业论文全攻略

学霸同款2026 AI论文工具TOP9&#xff1a;自考毕业论文全攻略 2026年自考论文写作工具测评&#xff1a;为何需要这份榜单&#xff1f; 随着人工智能技术的不断进步&#xff0c;AI论文工具逐渐成为学术写作的重要辅助手段。对于自考学生而言&#xff0c;撰写毕业论文不仅是学业的…

作者头像 李华
网站建设 2026/4/28 22:07:11

A.每日一题——3507. 移除最小数对使数组有序 I

题目链接&#xff1a;3507. 移除最小数对使数组有序 I&#xff08;简单&#xff09; 算法原理&#xff1a; 解法&#xff1a;模拟 3ms击败55.74% 时间复杂度O(N) 由于数据范围非常小&#xff0c;所以直接按题意模拟即可 将nums的所有相邻元素全部扔进顺序表里&#xff0c;方便修…

作者头像 李华