/** * 主题系统使用示例 * * 展示四种不同的主题样式使用方式 */ import { ScrollView, View, Text, TouchableOpacity } from 'react-native'; import { Stack } from 'expo-router'; import { useColorScheme, useThemeColors, useThemeInfo, commonStyles, createThemeStyles, ThemedText, ThemedView, } from '@/theme'; // 方式 3: 创建自定义主题样式(推荐用于复杂组件) const customStyles = createThemeStyles((colors) => ({ header: { backgroundColor: colors.primary, padding: 20, borderRadius: 12, marginBottom: 16, }, headerText: { color: '#FFFFFF', fontSize: 24, fontWeight: 'bold', textAlign: 'center', }, section: { backgroundColor: colors.card, padding: 16, borderRadius: 8, marginBottom: 16, borderWidth: 1, borderColor: colors.border, }, sectionTitle: { color: colors.text, fontSize: 18, fontWeight: '600', marginBottom: 12, }, codeBlock: { backgroundColor: colors.backgroundTertiary, padding: 12, borderRadius: 6, marginTop: 8, }, codeText: { color: colors.textSecondary, fontSize: 12, fontFamily: 'monospace', }, })); export default function ThemeExampleScreen() { const theme = useColorScheme(); const colors = useThemeColors(); const { isDark } = useThemeInfo(); const s = commonStyles[theme]; // 通用样式类 const custom = customStyles[theme]; // 自定义样式 return ( <> {/* 自定义样式示例 */} 主题系统使用示例 当前主题: {theme} {isDark ? '🌙' : '☀️'} {/* 方式 1: 使用主题组件 */} 方式 1: 使用主题组件 这是标题 这是副标题 这是普通文本 {`import { ThemedText, ThemedView } from '@/theme'; 标题 `} {/* 方式 2: 使用通用样式类 */} 方式 2: 使用通用样式类(类似 CSS 类名) 卡片标题 卡片描述文本 主要按钮 次要按钮 {`import { useColorScheme, commonStyles } from '@/theme'; const theme = useColorScheme(); const s = commonStyles[theme]; 标题 按钮 `} {/* 方式 3: 使用自定义主题样式 */} 方式 3: 使用自定义主题样式(推荐) 本页面的 header 和 section 就是使用自定义主题样式创建的 {`import { createThemeStyles } from '@/theme'; const styles = createThemeStyles((colors) => ({ header: { backgroundColor: colors.primary, padding: 20, }, headerText: { color: '#FFFFFF', fontSize: 24, }, })); const theme = useColorScheme(); 标题 `} {/* 方式 4: 使用 Hooks + 内联样式 */} 方式 4: 使用 Hooks + 内联样式(动态场景) 这是使用 useThemeColors() 动态获取颜色的文本 适合需要动态计算样式的场景 {`import { useThemeColors } from '@/theme'; const colors = useThemeColors(); 动态文本 `} {/* 颜色展示 */} 主题颜色展示 {Object.entries(colors).map(([key, value]) => ( {key} {value} ))} {/* 底部间距 */} ); }