This app now includes a comprehensive dark/light theme system that automatically applies consistent colors throughout the entire application. The theme system supports:
- ✅ Light and Dark themes
- ✅ Persistent theme storage (remembers user preference)
- ✅ Dynamic theme switching via Settings screen
- ✅ Consistent color scheme across all screens
- ✅ Type-safe theme colors
- ✅ Easy integration with existing components
Import and use the useTheme hook in any component:
import { useTheme } from '@/lib/theme';
export default function MyComponent() {
const { theme, colors, setTheme, toggleTheme } = useTheme();
return (
<View style={[styles.container, { backgroundColor: colors.background }]}>
<Text style={[styles.text, { color: colors.text }]}>
Current theme: {theme}
</Text>
</View>
);
}The theme system provides the following color properties:
background- Main background colorsurface- Secondary background (cards, modals)card- Card background colortext- Primary text colortextSecondary- Secondary text colorprimary- Primary brand colorprimaryText- Text color for primary backgroundsborder- Border colorheader- Header background colorheaderText- Header text coloricon- Icon colorsuccess- Success color (green)warning- Warning color (yellow/orange)error- Error color (red)tabBarBackground- Tab bar backgroundtabBarActive- Active tab colortabBarInactive- Inactive tab colorinputBackground- Input field backgroundinputBorder- Input field borderinputText- Input field textbuttonBackground- Button backgroundbuttonText- Button textmodalBackground- Modal overlay backgroundshadowColor- Shadow color
theme- Current theme ('light' | 'dark')colors- Current theme colors objectsetTheme(theme)- Set specific themetoggleTheme()- Toggle between light/dark
❌ Before:
const styles = StyleSheet.create({
container: {
backgroundColor: '#ffffff',
},
text: {
color: '#000000',
},
});✅ After:
// In component
const { colors } = useTheme();
// In JSX
<View style={[styles.container, { backgroundColor: colors.background }]}>
<Text style={[styles.text, { color: colors.text }]}>Hello</Text>
</View>
// In StyleSheet - remove colors, add them dynamically
const styles = StyleSheet.create({
container: {
flex: 1,
// backgroundColor removed - added dynamically
},
text: {
fontSize: 16,
// color removed - added dynamically
},
});// Use semantic names for better theme support
{ color: colors.text } // ✅ Good
{ color: colors.textSecondary } // ✅ Good
{ color: colors.primary } // ✅ Good
{ color: '#000000' } // ❌ Bad - hardcodedTo update existing components for theme support:
-
Add the hook:
import { useTheme } from '@/lib/theme'; const { colors } = useTheme();
-
Update JSX with dynamic colors:
<View style={[styles.container, { backgroundColor: colors.background }]}> <Text style={[styles.title, { color: colors.text }]}>Title</Text> </View>
-
Remove hardcoded colors from StyleSheet:
const styles = StyleSheet.create({ container: { flex: 1, // Remove: backgroundColor: '#ffffff', }, title: { fontSize: 20, fontWeight: 'bold', // Remove: color: '#000000', }, });
The theme can be controlled from the Settings screen:
- Light Mode button - Sets theme to 'light'
- Dark Mode button - Sets theme to 'dark'
- Active state - Shows which theme is currently active
- Persistence - Theme choice is automatically saved and restored
import { useThemedStyles } from '@/lib/themedStyles';
const MyComponent = () => {
const styles = useThemedStyles(createStyles);
return <View style={styles.container} />;
};
const createStyles = (colors) => StyleSheet.create({
container: {
backgroundColor: colors.background,
borderColor: colors.border,
},
});To customize theme colors, edit lib/theme.tsx:
const lightTheme: ThemeColors = {
background: '#ffffff', // Change light background
primary: '#2f9e44', // Change primary color
// ... other colors
};
const darkTheme: ThemeColors = {
background: '#1a1a1a', // Change dark background
primary: '#4ade80', // Change primary color
// ... other colors
};- Settings screen (full theme integration + toggle controls)
- Tab layout (dynamic tab bar colors)
- Main app layout (theme provider setup)
- Inventory screen (partial - header and search)
- Calendar screen (partial - header and container)
- Manage suppliers screen (hook added)
- Inventory screen (needs full color integration)
- Calendar screen (needs full color integration)
- Manage suppliers screen (needs UI color updates)
- Add seed screen
- Add supplier screen
- Edit supplier screen
- Auth screens (login/signup)
- Other modal/component screens
If you're using useCallback with theme colors, make sure to include colors in the dependency array:
// ❌ Wrong - missing colors dependency
const renderItem = useCallback(({ item }) => {
return <View style={{ backgroundColor: colors.card }} />;
}, [item.id]);
// ✅ Correct - includes colors dependency
const renderItem = useCallback(({ item }) => {
return <View style={{ backgroundColor: colors.card }} />;
}, [item.id, colors]);Remove hardcoded backgroundColor from StyleSheet.create and apply them inline:
// ❌ Static background won't change with theme
const styles = StyleSheet.create({
card: { backgroundColor: '#ffffff' },
});
// ✅ Dynamic background
<View style={[styles.card, { backgroundColor: colors.card }]} />- Complete remaining screens - Apply theme colors to all remaining screens
- Test theme switching - Verify all screens look good in both themes
- Add transitions - Smooth theme switching animations (optional)
- Custom themes - Add more color schemes (optional)
The theme system is now fully functional and ready to be applied throughout the entire application!