React native:更改所选项目颜色

问题描述 投票:1回答:3

我的抽屉导航是一个堆栈导航,这意味着我创建一个自定义抽屉作为堆栈导航,如下所示:

class DrawerComponent extends React.Component {

  navigateToScreen = (route) => (
  () => {
    const navigateAction = NavigationActions.navigate({
      routeName: route
    });
    this.props.navigation.dispatch(navigateAction);
});


  render() {

    return (
          <ScrollView style={Styles.containerDrawer}>
            <View style={Styles.logoContainer}>
              <Image source={Images.logo}
                style={Styles.imageStyle}
                resizeMode={'contain'}
              />
            </View>

            <View style={Styles.blocksContainer}>
              <View style={Styles.firstBlock}>
                <TouchableOpacity
                  style={[Styles.buttonMenue, Styles.elevationButton, Styles.bgButton, Styles.centerContent]}
                  onPress= {this.navigateToScreen('Messages')}
                >
                  <IconMCI
                    name="message-text-outline" size={wp('10%')} color= '#000'
                    style={Styles.iconStyle}
                  />
                  <Text style={Styles.textButton}>الرسائل</Text>
                </TouchableOpacity>
                <TouchableOpacity
                  style={[Styles.buttonMenue, Styles.elevationButton, Styles.bgButton, Styles.centerContent]}
                  onPress= {this.navigateToScreen('Home')}
                >
                  <IconSI
                    name="home" size={wp('10%')} color= '#000'
                    style={Styles.iconStyle}
                  />
                  <Text style={Styles.textButton}>الإستقبال</Text>

                </TouchableOpacity>
              </View>

我的问题是我无法改变所选项目的风格。

这是我的抽屉,它看起来像这样:enter image description here

react-native navigation-drawer
3个回答
0
投票

您可以使用状态来处理项目的点击事件并根据它更改类别,下面提供了一个示例

    class DrawerComponent extends React.Component {

      state={
      clicked:true;
      }

      navigateToScreen = (route) => (
      () => {
        const navigateAction = NavigationActions.navigate({
          routeName: route
        });
        this.props.navigation.dispatch(navigateAction);
    });


      render() {

        return (
             ...

                <View style={Styles.blocksContainer}>
                  <View style={Styles.firstBlock}>
                    <TouchableOpacity
                      style={!this.state.clicked?[Styles.buttonMenue, Styles.elevationButton, Styles.bgButton, Styles.centerContent]:['custom-style']}
                      onPress= {()=>{
    this.setState({clicked:true})
    this.navigateToScreen('Messages')
    }}
                    >
 ...
                    </TouchableOpacity>

                  </View>

0
投票

这是更多的信息:

import React, {Component} from 'react';
import { createStackNavigator, createDrawerNavigator, createAppContainer } from 'react-navigation';
import HomeStackNavigation from './HomeStackNavigation'
import Messages from '../Components/Messages/Messages';
import DrawerComponent from '../Components/Drawer/Drawer';
import {Platform, Dimensions} from 'react-native';
// import ScoreList from '../Components/ScoreList/ScoreList';

var width = Dimensions.get('window').width;

const DrawerNavigation = createDrawerNavigator({
    Home: { // entree (route name) : on peut la nommer comme on veut mais on prefere lui donner le meme nom que notre screen qu'on va afficher
      screen: HomeStackNavigation, // le screen qu'on va afficher IL DOIT ETRE UN STACK
    },
},
{
  drawerWidth: width*0.83,
  contentComponent: props =>
  {
    return(<DrawerComponent  {...props}/>)
  },
  drawerPosition: 'left',
},
);

export default createAppContainer(DrawerNavigation)

0
投票

你的this.props.activeItemKey方法中的render将为你提供当前的项目密钥,如Messages,或Home在你的情况下...你可以用来相应地设置活动项目的样式。

第二

如果可能的话,将您的StackNavigator在routeConfigs中呈现的屏幕移动到您的Drawer:

const DrawerNavigation = createDrawerNavigator({
  Home: { screen: HomeScreen },
  Screen2: { screen: StackScreen2 },
  Screen3: { screen: StackScreen3 },
});
© www.soinside.com 2019 - 2024. All rights reserved.