0

Im using a library react native deck swiper to be able to swipe cards. I want my screen to have a linear gradient background color, but the issue is it doesn't seem to inherit the linear gradient color if there's a background color set. the swiper library has a backgroundColor prop but with a default of blue, therefore not rendering the linear gradient. So my question is, can this be overridden/can the linear gradient turn into a background color? my code below for reference

 <View style={styles.Scontainer}>
      <LinearGradient
        colors={[
          "#F7BBB2",
          "#FFC9B5",
          "#FFDDC7",
          "#FFF6D4",
          "#FFFDF2",
        ]}
        style={styles.background}
        //  Linear Gradient
        start={{ x: 0, y: 0 }}
        end={{ x: 1, y: 1 }}
      >

        <Swiper
          cards={data}
          renderCard={(card) => {
            return (
              <View style={styles.card}>
                <Text style={styles.text}>Hello world</Text>
              </View>
            );
          }}
          cardIndex={0}
          infinite={true}
          horizontalSwipe={false}
          goBackToPreviousCardOnSwipeBottom={true}
          showSecondCard={true}
          disableBottomSwipe={true}
          // backgroundColor= "linearGradient? -> style prop
        ></Swiper>
      </LinearGradient>
    </View>

isaac
  • 81
  • 1
  • 10

1 Answers1

0

What I did and have worked (at least in my case) is to add a Def for a Linear Gradient with an id and assign that gradient as the background color, something like this:

import { Defs, LinearGradient } from 'react-native-svg';

...

return (
  <>
    <Defs>
      <LinearGradient
        id="myGradient" // important to set an id!
        colors={[
          "#F7BBB2",
          "#FFC9B5",
          "#FFDDC7",
          "#FFF6D4",
          "#FFFDF2",
        ]}
        style={styles.background}
        //  Linear Gradient
        start={{ x: 0, y: 0 }}
        end={{ x: 1, y: 1 }}
      />
    </Defs>
    <Swiper
          cards={data}
          renderCard={(card) => {
            return (
              <View style={styles.card}>
                <Text style={styles.text}>Hello world</Text>
              </View>
            );
          }}
          cardIndex={0}
          infinite={true}
          horizontalSwipe={false}
          goBackToPreviousCardOnSwipeBottom={true}
          showSecondCard={true}
          disableBottomSwipe={true}
          backgroundColor="url(#myGradient)" // we use the id here
        />
  </>
)
Dharman
  • 30,962
  • 25
  • 85
  • 135