【问题标题】:react-navigation/material-bottom-tabs how to change badge color?react-navigation/material-bottom-tabs 如何更改徽章颜色?
【发布时间】:2020-10-11 21:13:05
【问题描述】:

我已经试过了

const MyTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    notification: '#e1d2f5',
  },
};

但此代码仅更改徽章的背景颜色。 我需要知道如何更改文本颜色。

【问题讨论】:

    标签: react-native react-navigation react-navigation-v5


    【解决方案1】:

    您只能将颜色更改为“黑色”或“白色”,具体取决于主题是浅色还是深色。 这是底部标签栏的徽章代码

    import * as React from 'react';
    import { Animated, StyleSheet, StyleProp, TextStyle } from 'react-native';
    import color from 'color';
    import { useTheme } from '@react-navigation/native';
    
    type Props = {
      /**
       * Whether the badge is visible
       */
      visible: boolean;
      /**
       * Content of the `Badge`.
       */
      children?: string | number;
      /**
       * Size of the `Badge`.
       */
      size?: number;
      /**
       * Style object for the tab bar container.
       */
      style?: Animated.WithAnimatedValue<StyleProp<TextStyle>>;
    };
    
    export default function Badge({
      visible = true,
      size = 18,
      children,
      style,
      ...rest
    }: Props) {
      const [opacity] = React.useState(() => new Animated.Value(visible ? 1 : 0));
      const [rendered, setRendered] = React.useState(visible ? true : false);
    
      const theme = useTheme();
    
      React.useEffect(() => {
        if (!rendered) {
          return;
        }
    
        Animated.timing(opacity, {
          toValue: visible ? 1 : 0,
          duration: 150,
          useNativeDriver: true,
        }).start(({ finished }) => {
          if (finished && !visible) {
            setRendered(false);
          }
        });
      }, [opacity, rendered, visible]);
    
      if (visible && !rendered) {
        setRendered(true);
      }
    
      if (!visible && !rendered) {
        return null;
      }
    
      // @ts-expect-error: backgroundColor definitely exists
      const { backgroundColor = theme.colors.notification, ...restStyle } =
        StyleSheet.flatten(style) || {};
      // <<<<<<<<<<<<<<<<<<<<<<<<< here <<<<<<<<<<<<<<<<<<<<<<<<<<
      const textColor = color(backgroundColor).isLight() ? 'black' : 'white';
    
      const borderRadius = size / 2;
      const fontSize = Math.floor((size * 3) / 4);
    
      return (
        <Animated.Text
          numberOfLines={1}
          style={[
            {
              opacity,
              transform: [
                {
                  scale: opacity.interpolate({
                    inputRange: [0, 1],
                    outputRange: [0.5, 1],
                  }),
                },
              ],
              backgroundColor,
              color: textColor,// <<<<<<<<<<<<<<<<<<<<<<<<< and here <<<<<<<<<<<<<<<<<<<<<<<<<<
              fontSize,
              lineHeight: size - 1,
              height: size,
              minWidth: size,
              borderRadius,
            },
            styles.container,
            restStyle,
          ]}
          {...rest}
        >
          {children}
        </Animated.Text>
      );
    }
    
    const styles = StyleSheet.create({
      container: {
        alignSelf: 'flex-end',
        textAlign: 'center',
        paddingHorizontal: 4,
        overflow: 'hidden',
      },
    });
    
    

    【讨论】: