在React Native上出现了奇怪的阴影效果。

huangapple go评论95阅读模式
英文:

Weird shadow effect on react native

问题

以下是您要翻译的部分:

"The print-screens are from how it looks in my iPhone

Im trying to add shadowbox effects to my react native app. I managed to add it to buttons and inputs just fine, they look like this:

在React Native上出现了奇怪的阴影效果。

But for some reason, every time I try to add it to a view, this happens:

在React Native上出现了奇怪的阴影效果。

Shaddow Effect:

  1. export const globalSheet = StyleSheet.create({
  2. shadowBox: {
  3. shadowColor: '#000',
  4. shadowOffset: { width: 4, height: 4 },
  5. shadowOpacity: 1,
  6. shadowRadius: 0,
  7. elevation: 2, // for Android
  8. borderWidth: 2,
  9. borderColor: 'black'
  10. }
  11. });

Button Component:

  1. export const GenericButtonContainer = styled.Pressable<
  2. GenericButtonProps
  3. >`
  4. background-color: ${p => {
  5. const mainColor = p.pressed
  6. ? globalStyles.purple
  7. : globalStyles.button.backgroundColor
  8. return p.unavailabe
  9. ? '#75757B'
  10. : mainColor
  11. }
  12. };
  13. border-radius: ${globalStyles.borderRadius}px;
  14. align-items: center;
  15. justify-content: center;
  16. padding-left: ${globalStyles.button.paddingHorizontal}px;
  17. padding-right: ${globalStyles.button.paddingHorizontal}px;
  18. padding-top: ${globalStyles.button.paddingVertical}px;
  19. padding-bottom: ${globalStyles.button.paddingVertical}px;
  20. opacity: ${p => p.unavailabe ? 0.4 : 1};
  21. border: 2px solid black;
  22. `
  23. export default function GenericButton({ children, unavailabe, style, ...pressableProps }: Props) {
  24. const [pressed, setPressed] = React.useState(false);
  25. return (
  26. <GenericButtonContainer
  27. {...pressableProps}
  28. style={({ pressed }: PressableStateCallbackType) => [
  29. globalSheet.shadowBox,
  30. ...[(typeof style === 'function' ? style({ pressed }) : style)],
  31. ]}
  32. disabled={unavailabe}
  33. unavailabe={unavailabe}
  34. onPressIn={() => setPressed(true)}
  35. onPressOut={() => setPressed(false)}
  36. pressed={pressed}
  37. >
  38. {children}
  39. </GenericButtonContainer>
  40. )
  41. }

Bugged component:

  1. import { globalStyles } from "src/components/globals/styles";
  2. import { styled } from "styled-components/native";
  3. export const Container = styled.View`
  4. flex-grow: 1;
  5. `
  6. export const ContentContainer = styled.View`
  7. flex-direction: row;
  8. align-items: center;
  9. padding: 5px;
  10. border: 2px solid black;
  11. border-radius: ${globalStyles.borderRadius}px;
  12. `
  13. export default function ProfileMatches() {
  14. return (
  15. <Container>
  16. <MyText H4>Partidas</MyText>
  17. <ContentContainer style={globalSheet.shadowBox}>
  18. <Controller />
  19. <MyText>0</MyText>
  20. </ContentContainer>
  21. </Container>
  22. )
  23. }

Here, Controller is just the imported svg and MyText is the following:

  1. export default function MyText({
  2. H1: useH1,
  3. H2: useH2,
  4. H3: useH3,
  5. H4: useH4,
  6. HP: useHP,
  7. truncate: shouldTruncate,
  8. size,
  9. children,
  10. ...props
  11. }: Props) {
  12. const truncateSize = size ? size : 13
  13. const data = truncate
  14. ? truncate(children as string, truncateSize)
  15. : children
  16. if (useH1) return <H1 {...props}>{data}</H1>
  17. if (useH2) return <H2 {...props}>{data}</H2>
  18. if (useH3) return <H3 {...props}>{data}</H3>
  19. if (useH4) return <H4 {...props}>{data}</H4>
  20. if (useHP) return <HP {...props}>{data}</HP>
  21. return <Text {...props}>{data}</Text>
  22. }

Where each HX is just a styled text component. It didnt work when MyText was just a text component without styles as well."

请注意,我已经翻译了代码的部分,其他部分都保持原文。

英文:

The print-screens are from how it looks in my iPhone

Im trying to add shadowbox effects to my react native app. I managed to add it to buttons and inputs just fine, they look like this:

在React Native上出现了奇怪的阴影效果。

But for some reason, every time I try to add it to a view, this happens:

在React Native上出现了奇怪的阴影效果。

Shaddow Effect:

  1. export const globalSheet = StyleSheet.create({
  2. shadowBox: {
  3. shadowColor: &#39;#000&#39;,
  4. shadowOffset: { width: 4, height: 4 },
  5. shadowOpacity: 1,
  6. shadowRadius: 0,
  7. elevation: 2, // for Android
  8. borderWidth: 2,
  9. borderColor: &#39;black&#39;
  10. }
  11. });

Button Component:

  1. export const GenericButtonContainer = styled.Pressable&lt;
  2. GenericButtonProps
  3. &gt;`
  4. background-color: ${p =&gt; {
  5. const mainColor = p.pressed
  6. ? globalStyles.purple
  7. : globalStyles.button.backgroundColor
  8. return p.unavailabe
  9. ? &#39;#75757B&#39;
  10. : mainColor
  11. }
  12. };
  13. border-radius: ${globalStyles.borderRadius}px;
  14. align-items: center;
  15. justify-content: center;
  16. padding-left: ${globalStyles.button.paddingHorizontal}px;
  17. padding-right: ${globalStyles.button.paddingHorizontal}px;
  18. padding-top: ${globalStyles.button.paddingVertical}px;
  19. padding-bottom: ${globalStyles.button.paddingVertical}px;
  20. opacity: ${p =&gt; p.unavailabe ? 0.4 : 1};
  21. border: 2px solid black;
  22. `
  23. export default function GenericButton({ children, unavailabe, style, ...pressableProps }: Props) {
  24. const [pressed, setPressed] = React.useState(false);
  25. return (
  26. &lt;GenericButtonContainer
  27. {...pressableProps}
  28. style={({ pressed }: PressableStateCallbackType) =&gt; [
  29. globalSheet.shadowBox,
  30. ...[(typeof style === &#39;function&#39; ? style({ pressed }) : style)],
  31. ]}
  32. disabled={unavailabe}
  33. unavailabe={unavailabe}
  34. onPressIn={() =&gt; setPressed(true)}
  35. onPressOut={() =&gt; setPressed(false)}
  36. pressed={pressed}
  37. &gt;
  38. {children}
  39. &lt;/GenericButtonContainer&gt;
  40. )
  41. }

Bugged component:

  1. import { globalStyles } from &quot;src/components/globals/styles&quot;;
  2. import { styled } from &quot;styled-components/native&quot;;
  3. export const Container = styled.View`
  4. flex-grow: 1;
  5. `
  6. export const ContentContainer = styled.View`
  7. flex-direction: row;
  8. align-items: center;
  9. padding: 5px;
  10. border: 2px solid black;
  11. border-radius: ${globalStyles.borderRadius}px;
  12. `
  13. export default function ProfileMatches() {
  14. return (
  15. &lt;Container&gt;
  16. &lt;MyText H4&gt;Partidas&lt;/MyText&gt;
  17. &lt;ContentContainer style={globalSheet.shadowBox}&gt;
  18. &lt;Controller /&gt;
  19. &lt;MyText&gt;0&lt;/MyText&gt;
  20. &lt;/ContentContainer&gt;
  21. &lt;/Container&gt;
  22. )
  23. }

Here, Controller is just the imported svg and MyText is the following:

  1. export default function MyText({
  2. H1: useH1,
  3. H2: useH2,
  4. H3: useH3,
  5. H4: useH4,
  6. HP: useHP,
  7. truncate: shouldTruncate,
  8. size,
  9. children,
  10. ...props
  11. }: Props) {
  12. const truncateSize = size ? size : 13
  13. const data = truncate
  14. ? truncate(children as string, truncateSize)
  15. : children
  16. if (useH1) return &lt;H1 {...props}&gt;{data}&lt;/H1&gt;
  17. if (useH2) return &lt;H2 {...props}&gt;{data}&lt;/H2&gt;
  18. if (useH3) return &lt;H3 {...props}&gt;{data}&lt;/H3&gt;
  19. if (useH4) return &lt;H4 {...props}&gt;{data}&lt;/H4&gt;
  20. if (useHP) return &lt;HP {...props}&gt;{data}&lt;/HP&gt;
  21. return &lt;Text {...props}&gt;{data}&lt;/Text&gt;
  22. }

Where each HX is just a styled text component. It didnt work when MyText was just a text component without styles as well.

答案1

得分: 1

如上所述,唯一缺少的是背景颜色。

英文:

As commented above, all that was missing was a background color.

huangapple
  • 本文由 发表于 2023年7月20日 19:17:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定