0

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;

hanan
  • 532
  • 2
  • 7
  • 23
  • import TouchableOpacity from react-native instead of react-native-gesture-handler – Guruparan Giritharan Jul 21 '20 at 14:11
  • the issue still persists. – hanan Jul 21 '20 at 14:48
  • I have even edited the question. just others not assume as you. I am still having trouble to solve this issue. – hanan Jul 21 '20 at 14:49
  • 1
    But what are you effectively clicking, the switch? You should handle it within the switch then, by the method `onValueChange`, can you please try it? Or try removing the switch and see if it's working. – Konstantin Jul 21 '20 at 15:08
  • Yes modified to like this ```onValueChange = {() => console.log("pressed!")}``` just to see "pressed!" output in the console but nothing outputted. now I am thinking more that the issue is in the stack. please help me with this. – hanan Jul 21 '20 at 15:16

2 Answers2

0

There is a typo on the method that you are calling change the onPress from togglemlethod to togglemethod

  • I am still facing the issue, yeah it is true there was a typo. I corrected and still not firing up. did you try to run it on your own? – hanan Jul 21 '20 at 15:03
  • I have even edited the question. just others not to assume as you. I am still having trouble to solve this issue. – hanan Jul 21 '20 at 15:05
0

Here is the working version of the code,

  1. You should not place the Switch inside the icon which results in an error
  2. You can use the switch itself to trigger the toggle,

You will have to change the styles according to your need, but this works fine on android.

 <Stack.Navigator screenOptions={{ headerTitleAlign: 'center' }}>
        <Stack.Screen
          name="Home"
          component={HomeScreen}
          options={{
            headerTitle: (props) => (
              <View style={{ flexDirection: 'row' }}>
                <View>
                  <Title style={{ paddingLeft: 130 }}>
                    <Text>Home</Text>
                  </Title>
                </View>

                <View>
                  <TouchableOpacity
                    onPress={() => {
                      togglemethod();
                    }}
                    centered={true}
                    style={{ flexDirection: 'row',alignItems:'center' }}>
                    <Switch
                      value={isDartheme}
                      color="red"
                      onValueChange={() => togglemethod()}
                    />
                    <MaterialCommunityIcons
                      name={
                        isDartheme
                          ? 'moon-waning-crescent'
                          : 'white-balance-sunny'
                      }
                      size={25}
                      color="blue"
                      style={{
                        paddingBottom: 5,
                      }}></MaterialCommunityIcons>
                  </TouchableOpacity>
                </View>
              </View>
            ),
          }}
        />
      </Stack.Navigator>
Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50