I am using react native Navigation and I put a switch inside the header to toggle between light and dark theme while using touchableOpacity onPress prop. No error logs, and when I press the switch, the touchableOpacity onpress is not firing up. I did share my App.js code, I would appreciate if you can see and point where I am doing wrong.
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState} from 'react';
import {StatusBar, View, TouchableOpacity} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import Home from './src/screens/Home';
import Details from './src/screens/Details';
import {createStackNavigator} from '@react-navigation/stack';
import {DarkTheme, Text, Title, TouchableRipple, Switch} from 'react-native-paper';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
const Stack = createStackNavigator();
const App = () => {
const [isDartheme, setDarkTheme] = useState(false);
const togglemethod = () => {
console.log("Called!");
setDarkTheme(!isDartheme);
};
return (
<>
<NavigationContainer>
<StatusBar barStyle="dark-content" />
<Stack.Navigator screenOptions={{headerTitleAlign: 'center'}}>
<Stack.Screen
name="Home"
component={Home}
options={{
headerTitle: (props) => (
<View style={{ flexDirection: 'row'}}>
<View>
<Title style={{paddingLeft: 130}}>
<Text>Home</Text>
</Title>
</View>
<View >
<TouchableOpacity
onPress={ () => {togglemethod() }
}
centered ={true}
>
<MaterialCommunityIcons
name={isDartheme?'moon-waning-crescent': 'white-balance-sunny'}
size = {25}
color= "blue"
style={{paddingLeft: 110, paddingBottom:5, width: '200%'}}
>
<Switch
value={isDartheme}
color="red"
style={{paddingLeft: 8}}
/>
</MaterialCommunityIcons>
</TouchableOpacity>
</View>
</View>
),
}}
/>
</Stack.Navigator>
</NavigationContainer>
</>
);
};
export default App;