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 组件库的集成,以构建美观且功能丰富的应用界面。
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
LongCat-AudioDiT-1BLongCat-AudioDiT 是一款基于扩散模型的文本转语音(TTS)模型,代表了当前该领域的最高水平(SOTA),它直接在波形潜空间中进行操作。00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
HY-Embodied-0.5这是一套专为现实世界具身智能打造的基础模型。该系列模型采用创新的混合Transformer(Mixture-of-Transformers, MoT) 架构,通过潜在令牌实现模态特异性计算,显著提升了细粒度感知能力。Jinja00
FreeSql功能强大的对象关系映射(O/RM)组件,支持 .NET Core 2.1+、.NET Framework 4.0+、Xamarin 以及 AOT。C#00
项目优选
收起
deepin linux kernel
C
27
14
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
659
4.26 K
Ascend Extension for PyTorch
Python
503
608
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
939
862
Oohos_react_native
React Native鸿蒙化仓库
JavaScript
334
378
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
390
285
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
123
195
openGauss kernel ~ openGauss is an open source relational database management system
C++
180
258
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.54 K
893
昇腾LLM分布式训练框架
Python
142
168