import { StyleSheet, ScrollView, TouchableOpacity, View, Text, useColorScheme as useSystemColorScheme } from 'react-native'; import { Stack } from 'expo-router'; import { useState, useEffect, useMemo } from 'react'; import { useTheme, useSettingsActions } from '@/stores'; import { useHaptics } from '@/hooks'; import Colors from '@/constants/Colors'; export default function ThemeTestScreen() { const currentTheme = useTheme(); const { setTheme } = useSettingsActions(); const haptics = useHaptics(); const systemColorScheme = useSystemColorScheme(); // 强制重新渲染的状态 const [renderKey, setRenderKey] = useState(0); // 直接计算实际应用的主题 - 不使用 useColorScheme hook const actualTheme: 'light' | 'dark' = useMemo(() => { return currentTheme === 'auto' ? (systemColorScheme === 'dark' ? 'dark' : 'light') : currentTheme; }, [currentTheme, systemColorScheme]); // 使用 useMemo 确保颜色对象在主题改变时重新计算 const colors = useMemo(() => { console.log('🎨 Recalculating colors for theme:', actualTheme); return Colors[actualTheme] as Record; }, [actualTheme]); // 监听主题变化 useEffect(() => { console.log('🎨 Theme changed:', { currentTheme, systemColorScheme, actualTheme, renderKey }); setRenderKey(prev => prev + 1); }, [currentTheme, systemColorScheme, actualTheme]); const handleThemeChange = (newTheme: 'light' | 'dark' | 'auto') => { haptics.selection(); console.log('🎨 Changing theme to:', newTheme); setTheme(newTheme); }; return ( <> {/* 主题信息 */} 当前主题信息 用户设置: {currentTheme} 系统主题: {systemColorScheme || 'light'} 实际应用: {actualTheme} 渲染次数: {renderKey} {/* 主题切换按钮 */} 切换主题 handleThemeChange('light')} > ☀️ 浅色 handleThemeChange('dark')} > 🌙 深色 handleThemeChange('auto')} > 🔄 自动 {/* 文本颜色展示 */} 文本颜色 Primary Text - {colors.text} Secondary Text - {colors.textSecondary} Tertiary Text - {colors.textTertiary} {/* 背景颜色展示 */} 背景颜色 Primary Background - {colors.background} Secondary Background - {colors.backgroundSecondary} Tertiary Background - {colors.backgroundTertiary} {/* 主题颜色展示 */} 主题颜色 Primary - {colors.primary} Secondary - {colors.secondary} Success - {colors.success} Warning - {colors.warning} Error - {colors.error} Info - {colors.info} {/* UI 元素展示 */} UI 元素 这是一个卡片组件 卡片背景色: {colors.card} 输入框样式预览 按钮示例 ); } const styles = StyleSheet.create({ container: { flex: 1, }, section: { margin: 16, padding: 16, borderRadius: 12, }, title: { fontSize: 20, fontWeight: 'bold', marginBottom: 16, }, infoText: { fontSize: 16, marginBottom: 8, }, buttonRow: { flexDirection: 'row', gap: 12, }, themeButton: { flex: 1, paddingVertical: 12, paddingHorizontal: 16, borderRadius: 8, borderWidth: 2, alignItems: 'center', }, buttonText: { fontSize: 16, fontWeight: '600', }, colorText: { fontSize: 14, marginBottom: 8, fontFamily: 'monospace', }, colorBox: { padding: 16, borderRadius: 8, marginBottom: 8, alignItems: 'center', }, colorGrid: { gap: 8, }, card: { padding: 16, borderRadius: 8, borderWidth: 1, marginBottom: 12, }, cardText: { fontSize: 16, fontWeight: '600', marginBottom: 4, }, cardSubtext: { fontSize: 12, fontFamily: 'monospace', }, input: { padding: 12, borderRadius: 8, borderWidth: 1, marginBottom: 12, }, inputText: { fontSize: 14, }, separator: { height: 1, marginVertical: 12, }, button: { padding: 16, borderRadius: 8, alignItems: 'center', }, });