TouchableOpacity onpress无法与导航一起使用

时间:2020-07-21 13:47:56

标签: react-native react-native-android react-native-navigation touchableopacity react-native-paper

我正在使用react native导航,并在标题内放置了一个开关,以在使用touchableOpacity onPress道具时在明暗主题之间切换。没有错误日志,当我按下开关时,touchableOpacity onpress不会启动。我确实分享了我的App.js代码,希望您能看到并指出我做错的地方。

/**
 * 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;

2 个答案:

答案 0 :(得分:0)

您正在调用的方法上有一个错字,将onPress从togglemlethod更改为togglemethod

答案 1 :(得分:0)

这是代码的有效版本,

  1. 您不应将开关放置在图标内,否则会导致错误
  2. 您可以使用开关本身来触发切换,

您将不得不根据需要更改样式,但这在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>