Karishma Kavankar
Karishma Kavankar

Reputation: 100

Sticky footer in react native like flipkart

I want to develop a sticky footer that can be scrolled up. Once some part of UI is scrolled then the footer can also be moved up and the rest of the UI is visible. just like the Flipkart product detail page appears.

Upvotes: 0

Views: 534

Answers (1)

Pramod
Pramod

Reputation: 1940

  1. create a sticky button at bottom
  2. Get you element scroll position in srollview
  3. Get screen height
  4. Calculate approx height from where your element is visible in scrollview
  5. Hide the sticky button when your element is visible

Working example: https://snack.expo.io/@msbot01/playful-bagel (view in iphone or android mode)

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React,{Component} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  Image,
  Flatlist,
  Dimensions
} from 'react-native';

var height = Dimensions.get('window').height

export default class App extends Component<Props> {
  constructor(props) {
    super(props);
    this.state = {
      visible: true
    };
  }

  componentDidMount(){
    console.log('height::'+height)
  }





  render() {
      return (

         <View style={{ flex: 1}}>
          <ScrollView contentContainerStyle={{flexGrow:1}} 
            ref={(s) => this.scrollview = s}
            onScroll= {(event) => {
              // console.log(event.nativeEvent.contentOffset.y)

                  if (event.nativeEvent.contentOffset.y >=173) {
                  this.setState({
                    visible:false
                  })
                  }else{
                     this.setState({
                        visible:true
                    })
                  }
              }}

              onScrollEndDrag={(event) => {
                if (event.nativeEvent.contentOffset.y >=173) {
                  this.setState({
                    visible:false
                  })
                  }else{
                     this.setState({
                        visible:true
                    })
                  }
              }}
            >
           <View>
            <Text style={{fontSize:30, height:50 }}>1</Text>
            <Text style={{fontSize:30, height:50 }}>2</Text>
            <Text style={{fontSize:30, height:50 }}>3</Text>
            <Text style={{fontSize:30, height:50 }}>4</Text>
            <Text style={{fontSize:30, height:50 }}>5</Text>
            <Text style={{fontSize:30, height:50 }}>6</Text>
            <Text style={{fontSize:30, height:50 }}>7</Text>
            <Text style={{fontSize:30, height:50 }}>8</Text>
            <Text style={{fontSize:30, height:50 }}>9</Text>
            <Text style={{fontSize:30, height:50 }}>10</Text>
            <Text style={{fontSize:30, height:50 }}>11</Text>
            <Text style={{fontSize:30, height:50 }}>12</Text>
            <Text style={{fontSize:30, height:50 }}>13</Text>
            <Text style={{fontSize:30, height:50 }}>14</Text>
            <Text style={{fontSize:30, height:50 }}>15</Text>
            <Text style={{fontSize:30, height:50 }}>16</Text>
            <Text style={{fontSize:30, height:50 }}>17</Text>
            <Text style={{fontSize:30, height:50 }}>18</Text>
            <Text style={{fontSize:30, height:50 }}>19</Text>
            <Text style={{fontSize:30, height:50 }}>20</Text>


              <View style={{height:50, width:'100%', backgroundColor: 'red'}}>
                <Text>Button</Text>
              </View>

            <Text style={{fontSize:30, height:50 }}>1</Text>
            <Text style={{fontSize:30, height:50 }}>2</Text>
            <Text style={{fontSize:30, height:50 }}>3</Text>
            <Text style={{fontSize:30, height:50 }}>4</Text>
            <Text style={{fontSize:30, height:50 }}>5</Text>
            <Text style={{fontSize:30, height:50 }}>6</Text>
            <Text style={{fontSize:30, height:50 }}>7</Text>
            <Text style={{fontSize:30, height:50 }}>8</Text>
            <Text style={{fontSize:30, height:50 }}>9</Text>
            <Text style={{fontSize:30, height:50 }}>10</Text>
            <Text style={{fontSize:30, height:50 }}>11</Text>
            <Text style={{fontSize:30, height:50 }}>12</Text>
            <Text style={{fontSize:30, height:50 }}>13</Text>
            <Text style={{fontSize:30, height:50 }}>14</Text>
            <Text style={{fontSize:30, height:50 }}>15</Text>
            <Text style={{fontSize:30, height:50 }}>16</Text>
            <Text style={{fontSize:30, height:50 }}>17</Text>
            <Text style={{fontSize:30, height:50 }}>18</Text>
            <Text style={{fontSize:30, height:50 }}>19</Text>
            <Text style={{fontSize:30, height:50 }}>20</Text>
            <Text style={{fontSize:30, height:50 }}>1</Text>
            <Text style={{fontSize:30, height:50 }}>2</Text>
            <Text style={{fontSize:30, height:50 }}>3</Text>
            <Text style={{fontSize:30, height:50 }}>4</Text>
            <Text style={{fontSize:30, height:50 }}>5</Text>
            <Text style={{fontSize:30, height:50 }}>6</Text>
            <Text style={{fontSize:30, height:50 }}>7</Text>
            <Text style={{fontSize:30, height:50 }}>8</Text>
            <Text style={{fontSize:30, height:50 }}>9</Text>
            <Text style={{fontSize:30, height:50 }}>10</Text>
            <Text style={{fontSize:30, height:50 }}>11</Text>
            <Text style={{fontSize:30, height:50 }}>12</Text>
            <Text style={{fontSize:30, height:50 }}>13</Text>
            <Text style={{fontSize:30, height:50 }}>14</Text>
            <Text style={{fontSize:30, height:50 }}>15</Text>
            <Text style={{fontSize:30, height:50 }}>16</Text>
            <Text style={{fontSize:30, height:50 }}>17</Text>
            <Text style={{fontSize:30, height:50 }}>18</Text>
            <Text style={{fontSize:30, height:50 }}>19</Text>
            <Text style={{fontSize:30, height:50 }}>20</Text>
            <Text style={{fontSize:30, height:50 }}>1</Text>
            <Text style={{fontSize:30, height:50 }}>2</Text>
            <Text style={{fontSize:30, height:50 }}>3</Text>
            <Text style={{fontSize:30, height:50 }}>4</Text>
            <Text style={{fontSize:30, height:50 }}>5</Text>
            <Text style={{fontSize:30, height:50 }}>6</Text>
            <Text style={{fontSize:30, height:50 }}>7</Text>
            <Text style={{fontSize:30, height:50 }}>8</Text>
            <Text style={{fontSize:30, height:50 }}>9</Text>
            <Text style={{fontSize:30, height:50 }}>10</Text>
            <Text style={{fontSize:30, height:50 }}>11</Text>
            <Text style={{fontSize:30, height:50 }}>12</Text>
            <Text style={{fontSize:30, height:50 }}>13</Text>
            <Text style={{fontSize:30, height:50 }}>14</Text>
            <Text style={{fontSize:30, height:50 }}>15</Text>
            <Text style={{fontSize:30, height:50 }}>16</Text>
            <Text style={{fontSize:30, height:50 }}>17</Text>
            <Text style={{fontSize:30, height:50 }}>18</Text>
            <Text style={{fontSize:30, height:50 }}>19</Text>
            <Text style={{fontSize:30, height:50 }}>20</Text>

          </View>


           </ScrollView>

            {this.state.visible==true?
              <View style={{height:50, width:'100%', backgroundColor: 'red', position:'absolute', bottom:0}}>
                <Text>Button</Text>
            </View>:null}



        </View>


    );
  }
}

Upvotes: 1

Related Questions