Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change one button color when onPress in list

Tags:

react-native

So i want to make a list with a heart button when i tap on one heart button.

i want only one button changed color. and save the current button color state.

My Problem is When i tap on one heart button, all button changed color.

Screenshot: enter image description here

this is my code

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Button,
} from 'react-native';

import { ListItem } from 'react-native-elements'
import { Icon } from 'react-native-elements'
import Data from './src/data/sampledata.json';

export default class App extends Component<Props> {
  constructor(props) {
    super(props);

    this.state = {
      buttonColor: '#979797', // default button color goes here, grey is default
    };
  }

  onButtonPress = () => {
    if(this.state.buttonColor=='#ff002b')
    {
      this.setState({ buttonColor: '#979797' }) // grey
    }
    else {
      this.setState({ buttonColor: '#ff002b' }) // red
    }
  }

  render() {
    return (
      <View style={styles.container}>
        {
          Data.map((item, i) => (
            <ListItem
              key={item.index}
              title={item.dataItem}
              rightIcon={
                <Icon
                raised
                name='heart'
                type='font-awesome'
                color={this.state.buttonColor}
                onPress={this.onButtonPress}
              />
            }
            />
          ))
        }
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
});
like image 306
Gus Nando Avatar asked May 19 '18 04:05

Gus Nando


People also ask

Is there a way to change the background color of onpress?

Currently, its possible to make it more transparent, but not change the color. Button is wrapped around TouchableOpacity, so setting onPress background color is not possible at the moment. I recommend the use of TouchableHighlight for your desired effect. Sorry, something went wrong. Hi, would like to change background color when button press too.

How do I change the color of a button while pressed?

05-14-2016 09:05 AM If you want to change the color of the button while it's pressed, you can change the PressedColor (for the font color) and PressedFill (for the background color) of the button.

How to change the color of elevatedbutton in flutter?

ElevatedButton is the ready-to-go button for many Flutter developers. In this blog post let’s check how to change the color of the elevated button at the time of pressing. You can change the background color of the ElevatedButton using MaterialStateProperty class. You can change the color of the button based on the states too.

Is it possible to change the background color of the button?

Currently, its possible to make it more transparent, but not change the color. Button is wrapped around TouchableOpacity, so setting onPress background color is not possible at the moment.


1 Answers

One way to achieve this is as follows:

What you can do is keep track of the selected items in an array. Using getUpdatedSelectedItemsArray function you can get an array that contains all the selected item like so

const getUpdatedSelectedItemsArray = (selectedItems, id) => {
    const forDeletion = [id]; //can also be used for multiple ids
    if (selectedItems.includes(id)) {
        //if id already exists delete it
        return selectedItems.filter(item => !forDeletion.includes(item));
    } 
    //otherwise push it
    selectedItems.push(id);
    return selectedItems;
};

you can call the above function from this.onButtonPress(item) which takes item as an argument.

//Creating state

this.state = {
      buttonColor: '#979797', 
      selectedItems: []
};

Calling getUpdatedSelectedItemsArray() will give you updated array which you can set in your selectedItems state.

Lastly you will have check while giving the color to icon(item), that if item.id exists in this.state.selectedItems then show red otherwise grey, like so

Data.map((item, i) => (
            <ListItem
              key={item.index}
              title={item.dataItem}
              rightIcon={
                <Icon
                raised
                name='heart'
                type='font-awesome'
                color={this.checkIfIDExists(item.id) ? 'red' : 'grey'}
                onPress={this.onButtonPress}
              />
            }
            />
))

P.S this.checkIfIDExists(item.id) is a function the returns true if id exists in selectedItems array.

like image 75
Shivam Avatar answered Oct 12 '22 07:10

Shivam