Expo Material 3 Theme 使用教程
2025-04-18 04:27:56作者:尤辰城Agatha
1. 项目介绍
expo-material3-theme 是一个开源项目,它允许开发者从 Android 12+ 设备中检索 Material 3 动态主题,并在 expo(或纯 react-native)应用中使用它。对于不兼容的设备(iOS 或旧版本的 Android),它将返回一个备用主题。
2. 项目快速启动
安装
在托管 Expo 项目中安装
此库适用于 Expo Go,但由于它需要自定义原生代码,Expo Go 不支持,因此你将无法检索系统主题(你将得到一个备用主题)。
npx expo install @pchmn/expo-material3-theme
如果你使用的是开发构建,在添加库之后,你必须重建开发客户端(仅限 Android):
npx expo prebuild --platform android
npx expo run:android
在纯 React Native 项目中安装
对于纯 React Native 项目,确保你已经安装并配置了 expo 包。
npx expo install @pchmn/expo-material3-theme
npx pod-install
使用
以下是一个基本用法示例,用于检索用户设备的 Material 3 主题(或不支持时的备用主题):
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
import { useColorScheme, View, Button } from 'react-native';
function App() {
const colorScheme = useColorScheme();
const { theme } = useMaterial3Theme({ fallbackSourceColor: '#6750A4' });
return (
<View style={{ backgroundColor: theme[colorScheme].background }}>
<Button color={theme[colorScheme].primary}>Themed button</Button>
</View>
);
}
3. 应用案例和最佳实践
自定义主题
如果你想使用基于特定颜色的主题,而不是系统主题,只需向 useMaterial3Theme 钩子传递 sourceColor 参数。
// 主题将基于 #3E8260 颜色返回
const { theme } = useMaterial3Theme({ sourceColor: '#3E8260' });
更改主题
你可能还想通过生成新的主题或恢复默认主题(以使用户个性化你的应用)来更新主题。你可以使用 useMaterial3Theme 钩子来实现这一点:
const { theme, updateTheme, resetTheme } = useMaterial3Theme();
// 更新主题
<Button onPress={() => updateTheme('#3E8260')}>Update theme</Button>
// 重置主题
<Button onPress={resetTheme}>Reset theme</Button>
与 react-native-paper 一起使用
expo-material3-theme 提供了一个与 react-native-paper 兼容的主题,因此你可以轻松地结合使用这两个库。
import { useMaterial3Theme } from '@pchmn/expo-material3-theme';
import { useMemo } from 'react';
import { useColorScheme } from 'react-native';
import { Button, MD3DarkTheme, MD3LightTheme, Provider as PaperProvider } from 'react-native-paper';
function App() {
const colorScheme = useColorScheme();
const { theme } = useMaterial3Theme();
const paperTheme = useMemo(() => colorScheme === 'dark'
? { ...MD3DarkTheme, colors: theme.dark }
: { ...MD3LightTheme, colors: theme.light },
[colorScheme, theme]
);
return (
<PaperProvider theme={paperTheme}>
<Button>Themed react native paper button</Button>
</PaperProvider>
);
}
4. 典型生态项目
在本教程中,我们没有涉及具体的生态项目,但 expo-material3-theme 可以与各种 React Native 和 Expo 相关的项目和库一起使用,以创建具有 Material 3 设计感的应用程序。你可以探索与 react-native-paper、expo 和其他 UI 组件库的集成,以构建美观且功能丰富的应用界面。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0446
源启盛夏_AtomGit暑期开发者成长计划「源启盛夏」暑期校园开发者成长计划旨在激活校园开源力量,通过积分激励、认证扶持、资源倾斜等形式,引导高校组织和开发者完成「入驻 — 建项目 — 做贡献 — 获认证 — 得资源」的完整闭环。无论你是想带领社团入驻平台的组织者,还是希望用代码贡献证明自己的开发者,都能在这里找到属于你的成长路径。Markdown00
jiuwenswarmJiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0766
Hy3Hy3 是由腾讯混元团队研发的快慢思考融合的混合专家模型,总参数量 295B,激活参数 21B,MTP 层参数 3.8B。4 月底发布 Hy3 Preview 后,我们在 50 多个业务中获得了广泛的反馈,修复了各种体验问题,进一步提升了后训练的质量和规模。今天,我们发布 Hy3。它展现出显著强于同尺寸并比肩旗舰(参数规模往往是 Hy3 的 2~5 倍)开源模型的智能水平,显著提升了在各类产品和生产力任务中的实用价值。Python00
AscendNPU-IRAscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优C++0310
DragonOSDragonOS is an operating system developed from scratch using Rust, with Linux compatibility. It is designed for **Serverless** scenarios. 使用Rust从0自研内核,具有Linux兼容性的操作系统,面向云计算Serverless场景而设计。Rust00
热门内容推荐
最新内容推荐
项目优选
收起
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
494
515
deepin linux kernel
C
32
16
Ascend Extension for PyTorch
Python
799
1.14 K
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
780
1.57 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
965
2.27 K
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
C
844
6.18 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.21 K
1.24 K
AtomGit CLI (ag cli),AtomGit 命令行工具,参考 GitHub CLI (gh) 开发。
目前 atomgit-cli 项目已在 AtomCode 的 Coding Plan 项目列表中
Go
40
24
CANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。
Jupyter Notebook
644
279
暂无描述
Markdown
827
5.48 K