如何将自定义抽屉项目动态设置为“聚焦”?

问题描述

我在这里的最终目标是根据其他位置设置的一些状态,为一些抽屉项目提供自定义背景色。我知道为了给抽屉项目赋予独特的背景色,我需要设置自定义抽屉内容。但是,我遇到一个问题,无法使自定义抽屉图标知道它们是否在焦点上。

我本来以为我可以做一个const [bHomeFocused,setbHomeFocused] = useState(false)(等等)并在onPress上设置状态,然后在其上设置focused属性,但是当一大堆抽屉物品出现时在我看来,这听起来像是一个笨拙的解决方案。

我敢肯定我缺少一个简单的答案,因为非定制DrawerItems本质上具有此功能...

import { Drawer } from 'react-native-paper'
import { createDrawerNavigator,DrawerNavigationProp,DrawerItem,DrawerContentScrollView,DrawerItemList,DrawerContentComponentProps,DrawerContentOptions } from '@react-navigation/drawer';

function CustomDrawerContent(props: DrawerContentComponentProps<DrawerContentOptions>) {

    return (
        <DrawerContentScrollView {...props}>
            <Drawer.Section>
                <DrawerItem
                    label="Dashboard"
                    labelStyle={{ color: colorTheme.normalText }}
                    icon={() => <Icon name="book" type="feather" size={26} color={colorTheme.normalText} />}
                    activeBackgroundColor={colorTheme.panel}
                    inactiveBackgroundColor={colorTheme.cyan}
                    onPress={() => {
                        props.navigation.navigate('Dashboard')
                    }}
                />
            </Drawer.Section>
            <DrawerItem
                label="Home"
                labelStyle={{ color: colorTheme.normalText }}
                icon={() => <Icon name="home" type="feather" size={26} color={colorTheme.normalText} />}
                activeBackgroundColor={colorTheme.panel}
                inactiveBackgroundColor={colorTheme.red}
                onPress={() => {
                    props.navigation.navigate('HomeStack')
                }}
            />
        </DrawerContentScrollView>
    );
}

export type DrawerParamList = {
    Dashboard: undefined;
    HomeStack: undefined;
};

export type DrawerProps<T extends keyof DrawerParamList> = {
    navigation: DrawerNavigationProp<DrawerParamList,T>;
    route: RouteProp<DrawerParamList,T>;
};

const AppDrawer = createDrawerNavigator<DrawerParamList>();

export default function MainDrawer({ route,navigation }: TopLevelStackProps<"MainDrawer">) {

    return (
        <AppDrawer.Navigator
            drawerStyle={globalStyles.drawer}
            drawerContent={
                (props) => <CustomDrawerContent {...props} />
            }
            drawerContentOptions={{
                labelStyle: { color: colorTheme.normalText },activeBackgroundColor: colorTheme.panel,inactiveBackgroundColor: colorTheme.background,}}
        >
            <AppDrawer.Screen
                name="Dashboard"
                component={Dashboard}
                options={{
                    unmountOnBlur: true,}}
            />
            <AppDrawer.Screen
                name="HomeStack"
                component={HomeStack}
                options={{
                    unmountOnBlur: true,}}
            />
        </AppDrawer.Navigator>
    );
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)