harmony_ui 0.4.0
harmony_ui: ^0.4.0 copied to clipboard
Material/Cupertino-style widget library that implements the HarmonyOS design language for Flutter.
harmony_ui #
[harmony_ui gallery 主页] [导航类] [展示类] [操作类] [输入类] [选择类] [容器类]
状态:代码已发布到 GitHub,pub.dev 发布计划中(仓库:
shaohushuo/harmony_ui,包名harmony_ui)。
Material / Cupertino 风格的 Flutter 组件库,实现了 HarmonyOS 设计语言(华为鸿蒙「设计入门」规范),并完整覆盖官方「控件概览」文档中的 6 大类 42 个控件。组件命名与 API 尽可能对齐 Flutter 内置的 Material* / Cupertino* 组件,方便迁移:
| Material / Cupertino | harmony_ui |
|---|---|
Theme / ThemeData |
OhosTheme / OhosThemeData |
Scaffold / AppBar |
OhosScaffold / OhosAppBar |
ElevatedButton / CupertinoButton |
OhosButton(5 种样式 × 3 种形状) |
ListTile / CupertinoListTile |
OhosListTile / OhosList |
Card |
OhosCard |
Switch / Checkbox / Radio |
OhosSwitch / OhosCheckbox / OhosRadio |
TextField / SearchBar |
OhosTextField / OhosSearchBar |
NavigationBar / TabBar |
OhosNavigationBar / OhosTabBar |
Slider / RatingBar |
OhosSlider / OhosRatingBar |
AlertDialog / showDialog |
OhosAlertDialog / showOhosDialog |
SnackBar / showMenu |
OhosSnackBar / showOhosMenu |
Badge |
OhosBadge |
FilterChip |
OhosChip |
Divider / IconButton |
OhosDivider / OhosIconButton |
配套图标包:ohos_icons(HarmonyOS Symbol 字体图标,已发布)。
特性 #
- HarmonyOS 设计令牌:主题色
#0A59F7/#317AF7、11 色多彩色板、功能色、四级文字/图标透明度(90%/60%/40%/20%)、组件分层背景(comp_background_primary/gray)、20%/10% 品牌高亮背景(comp_emphasize_secondary/tertiary)与交互态色(hover/pressed/focus/select),全部对齐官方 Token 表。 - 明暗双主题:
OhosThemeData.light()/OhosThemeData.dark()开箱即用,支持copyWith定制。 - API 对齐 Flutter:使用方式与 Material/Cupertino 组件一致,学习成本低。
- 原生字体:默认使用 HarmonyOS 系统字体
HarmonyOS Sans(非鸿蒙平台自动回退系统字体)。 - 沉浸光感材质:
OhosLightMaterial实现官方 ULTRA_THIN … ULTRA_THICK 五档毛玻璃背板,支持顶部/底部渐变模糊,AppBar、底部页签、半模态面板开箱即用。 - 响应式布局:
OhosResponsiveBuilder+OhosGrid实现 600/840vp 断点(4/8/12 列)、16/24/32vp 边距与 8/12/16vp 槽宽,侧边页签在 ≥840vp 自动生效。 - 转场动效:
OhosGeometry提供与官方「转场动效」一致的弹簧/缓出曲线与 90/200/300/400ms 时长,配套「一镜到底」共享元素演示。 - 零平台依赖:纯 Dart / Flutter 实现,唯一第三方依赖为二维码生成
qr包。 - 完整控件覆盖:官方「控件概览」6 大类 42 个控件全部实现,见下方清单。
- UI Design Kit 增强能力:点光源、按压阴影、侧边栏/侧边菜单、横滑列表项、常驻通知、动态模糊标题栏、可展开操作栏、分层图标、应用内多窗入口等,对照华为「UI Design Kit 能力」文档实现。
- 索引与放大镜:
OhosAlphabetIndexer等高槽位(布局 / 字号 / 命中同源,父级压高度时整条跟着缩),拖动时手指旁出现放大镜;OhosMagnifier+OhosMagnifierController+OhosMagnifierPosition也能独立用于文本选择等「跟随手指」的场景。
安装 #
通过 pub.dev(发布后) #
fvm flutter pub add harmony_ui
通过 Git(当前推荐) #
fvm flutter pub add harmony_ui \
--git-url git@github.com:shaohushuo/harmony_ui.git
# 或直接在 pubspec.yaml 中声明
# dependencies:
# harmony_ui:
# git:
# url: https://github.com/shaohushuo/harmony_ui.git
# ref: main
快速开始 #
沉浸式布局:
OhosAppBar默认primary: true,会自己吃掉MediaQuery.padding.top(背景仍铺到屏幕最顶端,标题/动作下移到系统状态栏下面), 与 MaterialAppBar的SafeArea(bottom: false)一致,直接放进Scaffold.appBar即可。若你已经在外层套过SafeArea,或这条栏不在屏幕顶部,传primary: false。preferredSize保持height——Scaffold的appBarMaxHeight已含状态栏高度, 重复加会多出一条空白。
import 'package:flutter/material.dart';
import 'package:harmony_ui/harmony_ui.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: OhosTheme(
data: OhosThemeData.light(), // 或 OhosThemeData.dark()
child: OhosScaffold(
appBar: OhosAppBar(title: const Text('我的应用')),
body: Center(
child: OhosButton(
onPressed: () {},
child: const Text('开始使用'),
),
),
bottomNavigationBar: OhosNavigationBar(
currentIndex: 0,
onDestinationSelected: (int index) {},
destinations: const [
OhosNavigationDestination(
icon: Icon(Icons.home_filled),
label: '首页',
),
OhosNavigationDestination(
icon: Icon(Icons.person_filled),
label: '我的',
),
],
),
),
),
);
}
}
控件覆盖(对应官方「控件概览」42 控件) #
导航类 #
| 控件 | 实现 |
|---|---|
| 标题栏 | OhosAppBar |
| 子页签 | OhosTabBar |
| 底部页签 | OhosNavigationBar |
| 导航点 | OhosPageIndicator(见 ohos_swiper.dart) |
| 轮播 | OhosSwiper |
展示类 #
| 控件 | 实现 |
|---|---|
| 文本 / 子标题 | OhosText(OhosTextLevel 全字阶)/ OhosSubheader |
| 图片 | OhosImage(支持选中描边) |
| 分隔器 | OhosDivider |
| 进度条 | OhosProgressIndicator(圆形 / 线性) |
| 新事件标记 | OhosBadge(数字 / 红点) |
| 索引条 | OhosAlphabetIndexer(等高槽位 · 拖动放大镜 · selectedIndex 受控高亮 · onSelectEnd) |
| 放大镜 | OhosMagnifier / OhosMagnifierController / OhosMagnifierPosition(跟随手指的浮层,也可用于文本选择) |
| 滚动条 | OhosScrollbar |
| 即时反馈 / 即时操作 | showOhosToast / showOhosSnackBar |
| 气泡提示 | showOhosPopup |
| 数据可视化 | OhosDataPanel |
| 二维码 | OhosQrCode |
| 空白 | OhosBlank |
| 文本时钟 | OhosTextClock |
操作类 #
| 控件 | 实现 |
|---|---|
| 按钮 | OhosButton(filled / tonal / outlined / text / plain × 胶囊 / 圆角 / 圆形) |
| 下拉按钮 | OhosSelect |
| 状态按钮 | OhosToggleButton |
| 操作块 | OhosChip |
| 工具栏 | OhosToolbar |
| 核心操作栏 | OhosActionBar |
| 菜单 | showOhosMenu(列表 / 宫格) |
输入类 #
| 控件 | 实现 |
|---|---|
| 文本输入 | OhosTextField(正常 / 聚焦 / 错误态) |
| 搜索框 | OhosSearchBar |
| 数字加减 | OhosCounter |
| 图案锁 | OhosPatternLock |
选择类 #
| 控件 | 实现 |
|---|---|
| 勾选框 / 单选按钮 / 开关 | OhosCheckbox / OhosRadio / OhosSwitch |
| 评分条 | OhosRatingBar(支持半星) |
| 滑动条 | OhosSlider(刻度 / 气泡数值) |
| 选择器 | showOhosPicker |
| 颜色选择器 | OhosColorPicker |
| 分段按钮 | OhosSegmentedButton |
容器类 #
| 控件 | 实现 |
|---|---|
| 列表 / 列表项 | OhosList / OhosListTile |
| 卡片 | OhosCard |
| 弹出框 | OhosAlertDialog / showOhosDialog |
| 气泡提示 | showOhosPopup |
| 半模态面板 | showOhosBottomSheet |
设计语言实现(对齐官方设计规范) #
实现均对照官方文档的「色彩」「布局」「沉浸光感」「响应式」「转场动效」章节,示例代码见 example/lib/pages/layout_page.dart 与 navigation_page.dart。
色彩 Token #
OhosColors 与 OhosThemeData 中的颜色 1:1 对应官方 Token 表(0xAARRGGBB):
- 文字/图标四级:
font_primary #E5000000、font_secondary #99000000、font_tertiary #66000000、font_fourth #33000000(深色模式对应白色系列)。 - 背景分层:
background_secondary #F1F3F5(页面)、background_tertiary #E5E5EA、comp_background_primary #FFFFFF(卡片/组件)、comp_background_gray #F1F3F5(灰底)。 - 品牌高亮背景:
comp_emphasize_secondary #330A59F7(20%)、comp_emphasize_tertiary #190A59F7(10%),用于选中态与悬浮强调。 - 交互事件色:
interactive_hover #0C000000、interactive_pressed #19000000、interactive_focus #FF0A59F7、interactive_select #330A59F7。 - 组件直接取主题字段:
theme.emphasizeSecondaryColor、theme.compBackgroundGrayColor、theme.interactivePressedColor等。
沉浸光感(Immersive Light) #
// 顶部悬浮:ULTRA_THIN + 渐变模糊
OhosLightMaterial(
level: OhosLightMaterialLevel.ultraThin,
gradientFade: OhosLightFade.top,
child: OhosAppBar(lightMaterial: true, title: const Text('标题')),
)
// 底部悬浮:THIN + 渐变蒙层,光池跟随选中项,按压出现指尖光晕
OhosNavigationBar(
type: OhosNavigationBarType.float,
lightMaterial: true,
glow: true,
destinations: destinations,
...
)
// 半模态 / 大弹层:ULTRA_THICK(可开启光感漫射)
OhosLightMaterial(
level: OhosLightMaterialLevel.ultraThick,
effectTuning: const OhosLightEffectTuning(scatterScale: 0.6),
child: panel,
)
showOhosBottomSheet(context: context, title: '分享', content: ...);
材质由多层合成,对应官方「沉浸光感」的各层描述:
- 五档模糊强度:ULTRA_THIN=8、THIN=16、REGULAR=24、THICK=32、ULTRA_THICK=40(sigma),各档带有独立的表面填充/光池/高光/阴影/漫射参数。
- 光池(glow pools):
OhosLightPalette.glowColors彩色光晕在介质内漫溢,glowAlignment移动时产生「光跟随交互」的效果(底部页签选中项、按压位置)。 - 镜面高光与边缘高光:
animationValue驱动高光扫掠,上边缘画 rim 亮边与顶缘发丝高光。 - 光感漫射:
OhosLightEffectTuning.scatterScale开启背景放大折射带(默认关闭以省性能)。 - 调参:
OhosLightEffectTuning(blur/surface/glow/shadow/specular/scatter 六项倍率),可模拟官方「强/均衡/弱」三档。 OhosBottomSheet默认 ULTRA_THICK;OhosAppBar(lightMaterial: true)默认 ULTRA_THIN。
技术参考(MIT):材质分层思路借鉴自 harmony_immersive_glow_tabbar 与 liquid_glass_widgets。 注意
liquid_glass_widgets要求 Flutter ≥ 3.41.0,而鸿蒙社区分叉为oh-3.35.7-release(3.35.x),无法直接依赖,故采用同思路的自绘实现。
布局基础(栅格 / 断点 / 边距) #
- 断点:
<600vp紧凑 /600–840vp中 /≥840vp宽;栅格 4/8/12 列,边距 16/24/32vp,槽宽 8/12/16vp,内容最大 2220vp。 OhosResponsiveBuilder依据窗口宽度重建子树;OhosGrid+OhosGridItem(columnSpan:)直接按栅格排布:
OhosGrid(
children: [
OhosGridItem(columnSpan: 2, child: card),
OhosGridItem(columnSpan: 2, child: card),
],
)
响应式布局方法 #
- 挪移:
OhosNavigationBar为纯内容组件;在宽屏(≥840vp)建议配合侧边页签使用(导航结构文档的「侧边 Tab」模式),示例可参考OhosWindowSize.useSideTab。 - 折行/缩放/分栏:
example的「响应式断点」演示按断点切换 1/2/4 列,展示 compact/medium/large 三种窗口形态。
转场动效(Transition / Motion) #
- 运动曲线:
OhosGeometry.spring(按压反馈)、easeOut(界面转场)、sharedElement(一镜到底共享转场)。 - 时长:90ms 按压反馈 / 200ms 普通转场 / 300ms 共享转场 / 400ms 页面转场。
- 层级关系:同层级(编辑/页签)、上下层级(父子页面)、跨层级(应用间)分别对应不同曲线。
- 一镜到底:共享元素放大转场演示见「转场动效 一镜到底」Demo(
AnimatedContainer+sharedElement曲线)。
与官方控件的重点一致性 #
| 规范点 | 官方值 | 实现 |
|---|---|---|
| 下划线子页签指示条 | 24×3vp 品牌色 | OhosTabBar(underline) |
| 胶囊子页签激活填充 | 品牌色实色填充 | OhosTabBar(type: capsule) / OhosChipGroup |
| 胶囊子页签未激活背景 | comp_background_gray 灰底 |
OhosChipGroup |
| 胶囊间距 | 不宜过大(默认 8vp) | OhosChipGroup(spacing: 8) |
| 底部页签高度 | 平铺 48vp / 悬浮 56vp | OhosNavigationBar 默认值 |
| 底部页签激活高亮 | 20% 品牌色 comp_emphasize_secondary |
OhosNavigationBar 选中胶囊 |
| 图标尺寸 | 24×24vp | OhosNavigationBar(iconSize: 24) |
| 页签数量建议 | 3–5 个 | 文档建议,未强制 |
UI Design Kit 增强组件(对照华为「UI Design Kit 能力」文档) #
实现均对照华为开发者文档「UI Design Kit 简介」
能力清单(点光源、按压阴影、侧边栏/侧边菜单、横滑列表项、常驻通知、动态模糊标题栏、
可展开操作栏、分层图标、应用内多窗入口等),示例代码见 example/lib/pages/kit_page.dart(示例首页 →「UI Design Kit」分类)。
[点光源效果] [侧边栏 + 侧边菜单] [横滑列表项] [常驻通知 SnackBar] [子页签分割线] [颜色选择器(光谱)] [动态模糊标题栏] [可展开操作栏] [分层图标 + 多窗入口]
点光源(PointLight) #
ArkUI PointLight(点光源)能力:发光源 OhosLightSource 朝 OhosIlluminated
受光面按类型投射环境光与「光晕 + 光柱」效果,支持 5 种受光类型与强度调节。
模拟器不支持原生 HDS 光影(文档明确),本实现用自绘合成达到同样的视觉层次。
OhosIlluminated(
illuminatedType: OhosPointLightIlluminatedType.borderContent, // none/border/content/borderContent/defaultFeatheringBorder
lightAlignment: const Alignment(0.2, 0.3),
options: const OhosPointLightOptions(color: Colors.white, intensity: 12, height: 150),
borderRadius: BorderRadius.circular(22),
child: card,
)
// 发光源(举例:卡片中心的圆钮)
OhosLightSource(size: 34, options: const OhosPointLightOptions(intensity: 10, height: 150))
按压阴影(PressShadow) #
OhosPressShadow 对应 ArkUI visualEffect(pressShadow):按压时表面
BLEND_WHITE(白色混合)或 BLEND_GRADIENT(指尖径向渐变),抬手淡出。
OhosPressShadow(
type: OhosPressShadowType.blendGradient,
borderRadius: BorderRadius.circular(24),
child: card,
)
侧边栏 + 侧边菜单(SideBar / SideMenu) #
OhosSideBar:overlay 悬浮侧边栏,圆角尾缘、遮罩、点外部自动收起、展开动画。OhosSideMenu/OhosSideMenuItem/OhosSideMenuSubItem:一/二/三级菜单、展开态、红点与数字角标。
OhosSideBar(
isShowSideBar: _show,
onIsShowSideBarChanged: (bool v) => setState(() => _show = v),
overlay: true,
width: 230,
sideBar: OhosSideMenu(
items: const [
OhosSideMenuItem(label: '收件箱', icon: Icon(Icons.mail_outline_rounded)),
OhosSideMenuItem(label: '消息', icon: Icon(Icons.chat_bubble_outline_rounded),
badgeCount: 8, subItems: [OhosSideMenuSubItem(label: '短信', badgeCount: 50)]),
],
selectedValue: _selected,
onSelected: (String v) => setState(() => _selected = v),
),
content: const MyPage(),
)
横滑列表项(SwipeAction) #
OhosListItem 左滑显示操作按钮,支持整划删除(fullDelete),滑动越界自动回调并移除。
OhosListItem(
fullDelete: true,
onFullDelete: () => setState(() => rows.removeAt(i)),
actions: [
OhosSwipeAction(icon: const Icon(Icons.share_rounded), backgroundColor: const Color(0xFF64BB5C), onTap: () {}),
OhosSwipeAction(icon: const Icon(Icons.delete_outline_rounded), backgroundColor: theme.dangerColor, onTap: () {}),
],
child: row,
)
常驻通知(Resident SnackBar) #
showOhosSnackBar(resident: true):duration -1 语义,常驻显示直到点击关闭按钮,
支持图标 + 标题 + 描述与「传统消息 + 撤销」组合。
子页签分割线(Divider Follow-Scroll) #
OhosTabBar 新增 OhosTabBarDividerMode.visible / none / followScroll:
常显、常隐、以及绑定滚动控制器「跟手」——内容滑动时分割线淡入,回顶淡出。
颜色选择器(三模式 + 收藏) #
OhosColorPicker 提供 网格 / 光谱 / 滑块 三种模式与收藏管理:
OhosColorPicker(
colors: const [Color(0xFF0A59F7), /* ... */],
selectedColor: _color,
onChanged: (Color c) => setState(() => _color = c),
tabs: const [OhosColorPickerTab.grid, OhosColorPickerTab.spectrum, OhosColorPickerTab.sliders],
favoriteColors: _favorites,
onFavoritesChanged: (List<Color> c) => setState(() => _favorites = c),
)
动态模糊标题栏(AppBar Scroll Effect) #
OhosAppBar 的 scrollEffect:内容穿透标题栏滚动,滚动到阈值区间后
commonBlur / transitionBlur / gradientBlur 背板淡入(BackdropFilter 动态模糊)。
OhosAppBar(
backgroundColor: Colors.transparent,
title: const Text('标题'),
scrollController: _scroller,
scrollEffect: const OhosAppBarScrollEffectOptions(
effect: OhosAppBarScrollEffectType.commonBlur,
blurEffectiveStartOffset: 0,
blurEffectiveEndOffset: 60,
blurSigma: 10,
),
)
可展开操作栏(Expandable ActionBar) #
OhosActionBar(expandable: true):悬浮宫格操作栏,主按钮收起/展开次级操作。
分层图标与多窗入口 #
OhosLayeredIcon:背景板 + 前景图标合成,支持缩放与描边。OhosMultiWindowEntry:应用内多窗入口,无多窗能力设备自动禁用(灰化、不可点)。
索引与放大镜(AlphabetIndexer / Magnifier) #
索引条改为等高槽位并支持受控高亮,拖动时手指旁出现放大镜;标题栏可以把状态栏
那一段单独着色。示例见 example/lib/pages/display_page.dart(展示类 →「索引条
OhosAlphabetIndexer」「放大镜 OhosMagnifier」)与
example/lib/pages/navigation_page.dart(导航类 →「状态栏分段着色」)。
[索引条拖动放大镜] [自定义泡泡 magnifierBuilder] [放大镜跟随色带] [状态栏分段着色]
索引条 OhosAlphabetIndexer #
- 等高槽位:布局、字号与「
dy → 字母」命中用同一个 slot 高度,视觉格与命中格 完全重合;槽位高度取自父级约束(LayoutBuilder的constraints.maxHeight,无界 时退回size.height)。调用方不再需要按size手算第二套几何。 size只是上限:宽度按原值使用,高度是期望值 —— 父级给的高度更小时整条跟着 缩、字号同步缩小(可拖分隔条的面板把 22 字母压到 130 高时不再overflowed)。- 整条都是命中区(
HitTestBehavior.opaque):24vp 宽的条只有中间约 12vp 可点的 日子过去了,字间空隙与两侧留白不再把事件漏给Stack下层的列表。
三条通道各司其职:
| 通道 | 触发时机 | 典型用途 |
|---|---|---|
onSelected |
过程:点按落指必报(含当前已高亮的字母);拖动只在跨格时报,逐帧不通知 | 列表 jumpTo 跟手 |
onSelectEnd |
结果:tap up / drag end / drag cancel,恰好一次,带回最终字母 | 「松手才定位」、Toast |
selectedIndex |
代码发起的定位(恢复上次位置 / 索引与列表互跳) | 受控高亮,不必订阅滚动 |
OhosAlphabetIndexer(
indexer: const <String>['A', 'B', /* … */ 'Y', 'Z'],
size: const Size(24, 220),
selectedIndex: _selected, // 代码定位到 M 也点亮 M
showMagnifier: true, // 拖动时手指旁的泡泡(点按不显示)
magnifierBuilder: (BuildContext context, String letter) => myBubble(letter),
onSelected: _jumpTo, // 过程:跨一格定位一次
onSelectEnd: (String letter) => showOhosToast(context, '定位到 $letter'),
)
从 0.3.0 升级到 0.4.0:
itemHeight已删除,槽位高度改由父级约束决定。旧写法itemHeight: perLetter+size高perLetter × 字母数的结果不变,删掉这个 实参即可。
放大镜 OhosMagnifier #
三个角色分开:画什么(OhosMagnifier)、挂在哪(OhosMagnifierController)、
放在哪(OhosMagnifierPosition)。
// 泡泡本体:text 放一个字符,child 放任意内容(child 优先)
const OhosMagnifier(text: 'M');
const OhosMagnifier(text: 'Z', size: Size.square(60));
OhosMagnifier(child: myCapsule, size: const Size(112, 44));
// 跟随手指:拖动的三段分别对应 show / update / hide
final OhosMagnifierController _magnifier = OhosMagnifierController();
void _onDragStart(DragStartDetails details) => _magnifier.show(
context,
anchor: details.globalPosition, // 全局坐标
text: _valueAt(details.localPosition),
bubble: const Size(112, 44),
gap: 16,
);
void _onDragUpdate(DragUpdateDetails details) => _magnifier.update(
details.globalPosition,
text: _valueAt(details.localPosition),
);
void _onDragEnd(DragEndDetails details) => _magnifier.hide();
@override
void dispose() {
_magnifier.dispose(); // 释放 OverlayEntry,State.dispose 里必须调
super.dispose();
}
- 泡泡进的是根
Overlay(MaterialApp/WidgetsApp提供),所以能画在宿主SizedBox之外;整颗IgnorePointer,不抢宿主手势。祖先没有Overlay时show返回false并打一条debugPrint,不显示也不崩。 - 落点是纯函数
OhosMagnifierPosition.resolve(无 RenderObject,边界情况可直接单测): 泡泡落在手指的四个角之一,纵横向各留gap;该角放不下就翻到同行另一个角、 再换到另一行;四个角都放不下才夹进安全区,此时placement.side按夹紧后的最终 矩形重算 —— 沿用首选角会让调用方把箭头 / 尾巴画到反方向。 OhosMagnifierSide.auto(默认)取离屏幕内侧更近的那个角;宽度上限为可用宽度的 一半,避免泡泡把整条索引条盖住。- 墨水屏(
OhosThemeData.ink(),即monochrome)下自动换成纯黑描边并关掉入场动画。
标题栏状态栏分段着色 #
OhosAppBar(
title: const Text('消息'),
backgroundColor: theme.cardColor, // 工具栏段
statusBarBackgroundColor: theme.backgroundColor, // 状态栏段(MediaQuery.padding.top)
)
// 自己包顶栏时用 heightOf 拿含状态栏内边距的总高,不必手算
final double total = OhosAppBar.heightOf(context, height: 56, bottom: tabBar);
primary: true(默认)时背景被拆成两段,内边距仍由组件吸收:布局与总高不变。preferredSize恒为height + bottom.preferredSize.height(不含内边距,因为Scaffold的appBarMaxHeight已经加过一次,重复加会多出一条空白)。- 名字刻意不叫
statusBarColor:那是SystemUiOverlayStyle里控制图标明暗的字段。 - 与
scrollEffect/lightMaterial叠加时,这一段保持纯色,不参与滚动驱动的 重着色。
安全区只让一次 #
OhosScaffold 与 OhosAppBar 对 MediaQuery.padding 的认领口径:
| 场景 | 顶部内边距由谁让 | 底部内边距由谁让 |
|---|---|---|
appBar: OhosAppBar(primary: true) |
标题栏自己(背景铺到屏幕顶端,标题下移) | 有 bottomNavigationBar 时由它让,body 不再让 |
appBar: OhosAppBar(primary: false) |
OhosScaffold 替它让一次 |
同上 |
没有 appBar |
OhosScaffold 的 body |
同上 |
- 四条边到这里都已由「栏 / 页面容器 / 底部导航」认领完毕,
OhosScaffold会把它们 一并从 body 子树的MediaQuery里摘掉:body 里再包一层SafeArea是空操作,MediaQuery.paddingOf(context)读到的四边都是 0。对照演示见导航类「安全区: body 里再套 SafeArea 不叠第二份」。 - 需要贴着屏幕边缘绘制(背景要铺到状态栏底下)时,原始内边距读
View.of(context).padding;MediaQuery.viewPaddingOf会被同一处一起摘掉,不可靠。 - 同一条内边距让两次 = 多出一条空白;一次都不让 = 内容压进状态栏。
primary: false只用于「这条栏不在屏幕顶部」,或栏被放在OhosScaffold之外、调用方已自己让过。
使用示例 #
主题 #
final OhosThemeData theme = OhosTheme.of(context); // 任意组件内获取
theme.highlightColor; // 品牌色 #0A59F7
theme.backgroundColor; // 页面背景
theme.cardColor; // 卡片背景
theme.multiColors; // 11 色多彩色板
theme.typography.bodyLarge; // 文字样式
OhosTheme(
data: OhosThemeData.dark().copyWith(
highlightColor: const Color(0xFF0A59F7),
dangerColor: const Color(0xFFE84026),
),
child: const MyApp(),
)
按钮 #
OhosButton(onPressed: _save, child: const Text('保存')); // 填充
OhosButton(
onPressed: _save,
style: OhosButtonStyle.tonal, // 次级
child: const Text('保存'),
);
OhosButton(
onPressed: _cancel,
style: OhosButtonStyle.outlined, // 描边
shape: OhosButtonShape.rounded, // 24vp 圆角方块
child: const Text('取消'),
);
OhosButton(
onPressed: _refresh,
loading: true, // 加载态
icon: const Icon(Icons.refresh),
child: const Text('刷新'),
);
底部导航与角标 #
OhosNavigationBar(
currentIndex: _index,
onDestinationSelected: (int i) => setState(() => _index = i),
destinations: const [
OhosNavigationDestination(
icon: Icon(OhosIcons.square_grid_2x2),
label: '组件',
),
OhosNavigationDestination(
icon: Icon(OhosIcons.person),
label: '我的',
badge: OhosBadge(count: 5), // 可选角标
),
],
)
列表与卡片 #
OhosList(
header: const OhosSubheader(title: '设置'),
children: [
OhosListTile(
leading: const Icon(OhosIcons.wifi),
title: const Text('无线网络'),
subtitle: const Text('已连接'),
trailing: const Icon(OhosIcons.chevron_right),
onTap: _openWifi,
),
OhosListTile(
leading: const Icon(OhosIcons.moon_fill),
title: const Text('深色模式'),
selected: true, // 选中态品牌色高亮
trailing: OhosSwitch(value: true, onChanged: _onDarkMode),
),
],
)
表单输入 #
OhosSearchBar(
hintText: '搜索',
onChanged: (String query) {},
onSubmitted: (String query) {},
);
OhosTextField(
labelText: '用户名',
prefixIcon: const Icon(Icons.person),
onChanged: (String v) {},
);
OhosTextField(
labelText: '密码',
obscureText: true,
errorText: '密码至少 8 位', // 错误态
);
评分 / 滑动 / 选择 #
OhosRatingBar(value: 3, onChanged: (int v) {}, starSize: 32);
OhosSlider(
value: _volume,
onChanged: (double v) {},
min: 0,
max: 100,
divisions: 20,
showValueBubble: true, // 拖动时气泡数值
);
OhosSegmentedButton<String>(
segments: const [
OhosSegment(label: '日', value: 'day'),
OhosSegment(label: '周', value: 'week'),
],
selected: _view,
onSelected: (String v) {},
);
// 底部弹窗选择器
final String? city = await showOhosPicker<String>(
context: context,
title: '选择城市',
initialValue: _city,
items: const [
OhosPickerItem(label: '北京', value: 'beijing'),
OhosPickerItem(label: '上海', value: 'shanghai'),
],
);
对话框 / 半模态面板 / 菜单 / 气泡 #
final bool? ok = await showOhosDialog<bool>(
context: context,
title: '删除文件?',
content: '删除后无法恢复,请确认是否继续。',
actions: [
OhosDialogAction(
label: '取消',
onPressed: () => Navigator.pop(context, false),
),
OhosDialogAction(
label: '删除',
danger: true,
onPressed: () => Navigator.pop(context, true),
),
],
);
await showOhosBottomSheet(
context: context,
title: '分享到',
content: SizedBox(
height: 200,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
// 分享入口图标
],
),
),
);
final String? action = await showOhosMenu<String>(
context: context,
items: const [
OhosMenuItem(label: '复制', icon: Icons.copy_rounded, value: 'copy'),
OhosMenuItem(label: '删除', icon: Icons.delete_rounded, value: 'delete', danger: true),
],
);
showOhosPopup(
context: context,
content: '长按扫码支付',
targetKey: payButtonKey, // GlobalKey
);
即时反馈 / 进度 / 二维码 #
showOhosToast(context, '操作成功', icon: OhosIcons.checkmark_circle_fill);
showOhosSnackBar(
context,
message: '已删除 1 个文件',
actionLabel: '撤销',
onAction: () => showOhosToast(context, '已撤销'),
);
const OhosProgressIndicator.circular(); // 无限转圈
OhosProgressIndicator.linear(value: 0.6); // 线性进度
OhosQrCode(data: 'https://github.com/shaohushuo/harmony_ui'); // 二维码
在 HarmonyOS 模拟器上运行 Demo #
example/ 是一个展示全部控件的画廊应用(6 大控件分类 + 布局与动效分类,共 7 个入口)。要求使用鸿蒙社区 Flutter 分叉:
- 仓库:
https://atomgit.com/CPF-Flutter/flutter_flutter - 推荐分支:
oh-3.35.7-release - 通过 fvm 管理:
fvm use ohos/oh-3.35.7-release
1. 准备环境 #
需要 DevEco Studio 内置工具链(node / ohpm / hvigor)与 OpenHarmony SDK:
export DEVECO_SDK_HOME=/Applications/DevEco-Studio.app/Contents/sdk
export JAVA_HOME=/Applications/DevEco-Studio.app/Contents/jbr/Contents/Home
export PATH="/Applications/DevEco-Studio.app/Contents/tools/node/bin:/Applications/DevEco-Studio.app/Contents/tools/ohpm/bin:/Applications/DevEco-Studio.app/Contents/tools/hvigor/bin:$HOME/Library/OpenHarmony/Sdk/26.0.0/toolchains:$PATH"
本仓库自带
env.sh,source env.sh即可。
2. 生成 ohos 平台工程并拉取依赖 #
cd example
fvm flutter pub get
fvm flutter create --platforms ohos .
3. 构建 HAP 并安装到模拟器 #
fvm flutter build hap --debug --no-codesign
hdc list targets # 确认模拟器在线(如 127.0.0.1:5555)
hdc install -r build/ohos/hap/entry-default-unsigned.hap
hdc shell aa start -a EntryAbility -b com.example.ohos_ui_example
4. 截图(可选) #
hdc shell snapshot_display -f /data/local/tmp/home.jpeg
hdc file recv /data/local/tmp/home.jpeg ./gallery_home.png
字体:鸿蒙设备自带
HarmonyOS Sans,harmony_ui 会自动使用;其他平台自动回退系统字体。
截图 #
所有截图来自 OpenHarmony 模拟器(API 26,1280×2832)实机运行:
| 页面 | 截图 |
|---|---|
| 主页(7 大分类) | doc/screenshots/gallery_home.png |
| 导航类:沉浸光感标题栏 / 下划线+胶囊子页签 / ChipGroup 多选 / 平铺+悬浮底部页签 | doc/screenshots/gallery_navigation.png |
| 导航类(滚动):胶囊子页签 / ChipGroup / 底部页签 | doc/screenshots/gallery_navigation_capsule.png |
| 导航类(滚动):平铺式 + 悬浮式底部页签 | doc/screenshots/gallery_navigation_bottom.png |
| 布局与动效:栅格系统 / 响应式断点 / 沉浸光感 / 转场动效 | doc/screenshots/gallery_layout_grid.png |
| 布局与动效(滚动):沉浸光感 THIN/THICK 材质 | doc/screenshots/gallery_layout_light.png |
| 布局与动效(滚动):一镜到底共享元素 / 动效曲线对比 | doc/screenshots/gallery_layout_motion.png |
| 布局与动效(交互):共享元素放大转场 | doc/screenshots/gallery_shared_element.png |
| 展示类:文本 / 图片 / 进度 / 徽标 / 索引条 / 滚动条 / 反馈 / 气泡 / 数据面板 / 二维码 / 文本时钟 / 空白 | doc/screenshots/gallery_display.png |
| 操作类:按钮 / 下拉 / 状态按钮 / 操作块 / 工具栏 / 核心操作栏 / 菜单 | doc/screenshots/gallery_action.png |
| 输入类:文本框 / 搜索框 / 数字加减 / 图案锁 | doc/screenshots/gallery_input.png |
| 选择类:勾选 / 单选 / 开关 / 评分 / 滑动条 / 分段按钮 / 颜色选择器 / 选择器 | doc/screenshots/gallery_selection.png |
| 容器类:列表 / 卡片 / 弹出框 / 气泡 / 半模态面板 | doc/screenshots/gallery_container.png |
| 弹出框(交互) | doc/screenshots/gallery_dialog.png |
| 半模态面板(交互) | doc/screenshots/gallery_bottom_sheet.png |
| 气泡提示(交互) | doc/screenshots/gallery_popup.png |
| 菜单(交互) | doc/screenshots/gallery_menu.png |
| 展示类(交互):索引条拖动,手指旁出放大镜 | doc/screenshots/mag_01_indexer_drag.png |
展示类(交互):magnifierBuilder 自定义泡泡 |
doc/screenshots/mag_02_indexer_custom_bubble.png |
| 展示类(交互):独立放大镜跟随色带报色值 | doc/screenshots/mag_03_magnifier_strip.png |
导航类:标题栏状态栏分段着色 / heightOf |
doc/screenshots/appbar_01_statusbar_band.png |
设计来源 #
- 华为鸿蒙设计入门文档:https://developer.huawei.com/consumer/cn/design/devstart/
- 控件概览(42 控件清单):https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/general_overview-0000001929599380
- 色彩 Token:https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/color-0000001776857164
- 沉浸光感:https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/immersivelight-0000002612101053
- 布局基础(栅格/断点):https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/design-layout-basics-0000001795579413
- 响应式布局(结构/方法):https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/design-responsive-layout-structure-0000001748539684
- 转场动效:https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/design-concepts-0000001795698445
- 子页签规范:https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/chipsgroup-0000001929788350
- 底部页签规范:https://developer.huawei.com/consumer/cn/doc/doccenter-ux-design/bottomtab-0000001956787789
- HarmonyOS-Design(设计原则 Skill 库):https://github.com/dososo/HarmonyOS-Design
- HarmonyOS 官方 AppIcon 设计源文件(Sketch)
常见问题 #
- 构建时报
npm not found? 需要把 DevEco Studio 内置工具加入 PATH(node / ohpm / hvigor),参见上方「准备环境」。 flutter build hap签名报错? 使用--no-codesign构建后hdc install,模拟器免签名运行。- 首页顶部内容被标题栏遮挡? 已修复:
OhosScaffold将OhosAppBar与body按 Column 布局(v0.1.0 之前为 Stack 叠加)。 - pub.dev 还没发布? 是的,当前以 GitHub 代码为主;发布后
fvm flutter pub add harmony_ui即可。
License #
MIT