简体   繁体   English

反应导航中的自定义抽屉

[英]Custom Drawer in react navigation

In my react native project I have custom drawer with screens like screen1 and screen2.在我的 react native 项目中,我有自定义抽屉,其中包含 screen1 和 screen2 等屏幕。 Screen1 is used for stack navigator and in Screen 2 it contains tab navigator. Screen1 用于堆栈导航器,在 Screen 2 中它包含选项卡导航器。 How to enable this kind of nesting navigation.如何启用这种嵌套导航。

I am using react navigation 5 in order to build custom drawer.我正在使用反应导航 5 来构建自定义抽屉。

Here is my code:这是我的代码:

  import { DrawerContentScrollView, DrawerItem } from "@react-navigation/drawer";
  .............
  render = () => {
    <DrawerContentScrollView {...props}>
    <View style={styles.menuContainer}>
    <View style={[styles.menuItemsCard, { backgroundColor: "#fff2df" }]}>
    <View style={[styles.circleContainer, { backgroundColor: "#FFC56F" }]}>
    <Icon travel name="suitcase" type="font-awesome" color="#fbae41" />
    </View>
            <DrawerItem
             label="Screen1"
             labelStyle={{ color: "#fbae41", fontSize: 10 }}
                            onPress={() => {
                                props.navigation.navigate("PatientInfo");
                            }}
                        />
            </View>
            <View style={[styles.itemCard, { backgroundColor: "#EFFFD5" }]}>
            <View style={[styles.circleContainer, { backgroundColor: "#b5ff39" }]}>
            <Icon Medical name="briefcase" type="font-awesome" color="#609806"></Icon>
            </View>
                        <DrawerItem
                            label="Screen2"
                            labelStyle={{ color: "#609806" }}
                            onPress={() => {
                                props.navigation.navigate("Diagnose");
                            }}
                        />
                    </View>
                </View>
    </DrawerContentScrollView>
  }

mainNaviagtor :主导航器

const MainNavigator = () => {
return (
    <Drawer.Navigator drawerContent={props => <Menu {...props} />} drawerStyle={{ width: 
 "100%" }}>
        <Stack.Screen name="Screen1" component={PatientInfoScreen} screenOptions={headerOptions} />
         <Stack.Screen name="Screen2" component={DiagnoseScreen} /> //Here i need tab navigator

        
    </Drawer.Navigator>
);
};

My older code: (react navigation 4.x)我的旧代码:(反应导航 4.x)

createDrawerNavigator(
{
    Screen1: {
        screen: createStackNavigator(
            {
                PatientInfo: { screen: PatientInfoScreen }
            },
            headerOptions
        )
    },

    Screen2: {
        screen: createBottomTabNavigator({
            Diagnose: {
                screen: diagnoseNavigation
            },
            Causes: { screen: causeNavigation },
        })
    },
})

Output: Output:

在此处输入图像描述

How should I solve this issue?我应该如何解决这个问题? I also referred to the https://reactnavigation.org/docs/nesting-navigators/ .我还提到了https://reactnavigation.org/docs/nesting-navigators/

Any one please help me to solve this issue.任何人请帮我解决这个问题。

Working Example: Expo Snack工作示例: Expo 小吃

Final Output:最终 Output:

在此处输入图像描述

Full source Code:完整源代码:

import * as React from 'react';
import {
  View,
  Text,
  StyleSheet,
  useWindowDimensions,
  Button,
} from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { Feather } from '@expo/vector-icons';
import { createStackNavigator } from '@react-navigation/stack';
import {
  createDrawerNavigator,
  DrawerContentScrollView,
  DrawerItemList,
  DrawerItem,
} from '@react-navigation/drawer';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';

const Drawer = createDrawerNavigator();
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();

function TabNav() {
  return (
    <Tab.Navigator>
      <Tab.Screen
        name="TabOne"
        component={() => (
          <View style={styles.container}>
            <Text>TabOne</Text>
          </View>
        )}
      />
      <Tab.Screen
        name="TabTwo"
        component={() => (
          <View style={styles.container}>
            <Text>TabTwo</Text>
          </View>
        )}
      />
    </Tab.Navigator>
  );
}

const StackNav = () => {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Feed" component={Feed} />
      <Stack.Screen name="Article" component={Article} />
    </Stack.Navigator>
  );
};

function CustomDrawerContent(props) {
  const width = useWindowDimensions().width * 0.3;

  return (
    <DrawerContentScrollView {...props}>
      <View style={styles.menuContainer}>
        <View
          style={[
            styles.menuItemsCard,
            { backgroundColor: '#fff2df', width: width, height: width },
          ]}>
          <>
            <View
              style={[styles.circleContainer, { backgroundColor: '#FFC56F' }]}>
              <Feather travel name="briefcase" size={25} color="#fbae41" />
              <DrawerItem
                label="Screen1"
                labelStyle={{ color: '#fbae41', fontSize: 10 }}
                onPress={() => {
                  props.navigation.navigate('Screen1');
                }}
              />
            </View>
          </>
          <DrawerItem
            style={{
              position: 'absolute',
              left: 0,
              width: width,
              height: width,
            }}
            label="Screen2"
            labelStyle={{ color: '#609806' }}
            onPress={() => {
              props.navigation.navigate('Screen1');
            }}
          />
        </View>
        <View
          style={[
            styles.menuItemsCard,
            { backgroundColor: '#EFFFD5', width: width, height: width },
          ]}>
          <View
            style={[styles.circleContainer, { backgroundColor: '#b5ff39' }]}>
            <Feather Medical name="briefcase" size={25} color="#609806" />
          </View>

          <DrawerItem
            style={{
              position: 'absolute',
              left: 0,
              width: width,
              height: width,
            }}
            label="Screen2"
            labelStyle={{ color: '#609806' }}
            onPress={() => {
              props.navigation.navigate('StackNav');
            }}
          />
        </View>
      </View>
    </DrawerContentScrollView>
  );
}

function MyDrawer() {
  return (
    <Drawer.Navigator
      drawerContent={(props) => <CustomDrawerContent {...props} />}>
      <Drawer.Screen name="Screen1" component={StackNav} />
      <Drawer.Screen name="StackNav" component={TabNav} />
    </Drawer.Navigator>
  );
}

export default function App() {
  return (
    <NavigationContainer>
      <MyDrawer />
    </NavigationContainer>
  );
}

function Feed({ navigation }) {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Feed Screen</Text>
      <Button
        title={'Article'}
        onPress={() => navigation.navigate('Article')}
      />
    </View>
  );
}

function Article({ navigation }) {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Article Screen</Text>
      <Button title={'Feed'} onPress={() => navigation.navigate('Feed')} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  menuContainer: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-evenly',
  },
  menuItemsCard: {
    flexDirection: 'column',
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 10,
  },
  circleContainer: {
    width: 50,
    height: 50,
    borderRadius: 25,
    padding: 10,
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM