{"id":43742,"date":"2024-10-29T11:37:08","date_gmt":"2024-10-29T11:37:08","guid":{"rendered":"https:\/\/www.carmatec.com\/?p=43742"},"modified":"2024-10-30T06:42:03","modified_gmt":"2024-10-30T06:42:03","slug":"implementacion-de-un-decorador-de-escala-en-una-lista-plana-arrastrable-en-react-native","status":"publish","type":"post","link":"https:\/\/www.carmatec.com\/es_mx\/blog\/implementing-a-scale-decorator-in-a-draggable-flatlist-in-react-native\/","title":{"rendered":"Implementaci\u00f3n de un decorador de escala en una lista plana arrastrable en React Native"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"43742\" class=\"elementor elementor-43742\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-af25a77 e-flex e-con-boxed e-con e-parent\" data-id=\"af25a77\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-51e0f7a elementor-widget elementor-widget-text-editor\" data-id=\"51e0f7a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Creating a visually interactive and user-friendly experience is key in <a href=\"\/mobile-app-development-company\/\">mobile app development<\/a>. React Native&#8217;s FlatList is highly versatile and efficient, commonly used for rendering large lists. With the added benefit of drag-and-drop functionality, you can make your FlatList even more engaging. In this article, we&#8217;ll explore how to implement a draggable FlatList in React Native, complete with a scale decorator to make items pop and respond to touch.<\/p><h2><strong>Prerequisites<\/strong><\/h2><p>Before starting, ensure you have basic knowledge of React Native and some familiarity with animations in <a href=\"\/react-native-app-development-company\/\">React Native<\/a>. You&#8217;ll also need to install the following libraries:<\/p><ol><li><strong>react-native-reanimated<\/strong> &#8211; for animations.<\/li><li><strong>react-native-gesture-handler<\/strong> &#8211; for handling touch gestures.<\/li><li><strong>react-native-draggable-flatlist<\/strong> &#8211; for draggable list functionality.<\/li><\/ol><pre>npm install react-native-reanimated react-native-gesture-handler react-native-draggable-flatlist<\/pre><h5><strong>Step 1: Set Up Draggable FlatList<\/strong><\/h5><p>Begin by importing the necessary components and setting up a simple draggable <code>FlatList<\/code>.<\/p><pre>import React from 'react';\nimport { StyleSheet, View, Text } from 'react-native';\nimport DraggableFlatList, { ScaleDecorator } from 'react-native-draggable-flatlist';\n\nconst data = Array.from({ length: 10 }, (v, i) =&gt; ({\n    key: `item-${i}`,\n    label: `Item ${i + 1}`,\n}));\n\nconst DraggableList = () =&gt; {\n    const [items, setItems] = React.useState(data);\n    const renderItem = ({ item, drag, isActive }) =&gt; {\n        return (\n            &lt;ScaleDecorator activeScale={1.1}&gt;\n                &lt;View\n                    style={[\n                        styles.itemContainer,\n                        { backgroundColor: isActive ? '#e0f7fa' : '#fafafa' },\n                    ]}\n                &gt;\n                    &lt;Text style={styles.text} onLongPress={drag}&gt;\n                        {item.label}\n                    &lt;\/Text&gt;\n                &lt;\/View&gt;\n            &lt;\/ScaleDecorator&gt;\n        );\n    };\n    return (\n        &lt;DraggableFlatList\n            data={items}\n            onDragEnd={({ data }) =&gt; setItems(data)}\n            keyExtractor={(item) =&gt; item.key}\n            renderItem={renderItem}\n        \/&gt;\n    );\n};\nexport default DraggableList;<\/pre><p>Here\u2019s what\u2019s happening:<\/p><ol><li><strong>Data Array<\/strong>: We set up an array of items to render in the list.<\/li><li><strong><code>DraggableFlatList<\/code> Component<\/strong>: This renders a list where items can be dragged.<\/li><li><strong>Render Item<\/strong>: Each item displays text and uses <code>drag<\/code> from the render prop to enable dragging on long-press.<\/li><li><strong>ScaleDecorator<\/strong>: Wraps each list item and provides a scaling effect when active.<\/li><\/ol><h5><strong>Step 2: Apply the Scale Decorator<\/strong><\/h5><p>The <code>ScaleDecorator<\/code> from <code>react-native-draggable-flatlist<\/code> is a powerful tool for providing visual feedback on item selection. Setting <code>activeScale<\/code> controls the scale size when an item is being dragged.<\/p><pre>&lt;ScaleDecorator activeScale={1.1}&gt;<\/pre><p>In this example, setting <code>activeScale={1.1}<\/code> scales the item to 110% of its original size when active. Adjust the scale factor as needed for your UI.<\/p><h5><strong>Step 3: Add Custom Styles<\/strong><\/h5><p>Enhance the appearance and interaction by adding some custom styles.<\/p><pre>const styles = StyleSheet.create({\n  itemContainer: {\n \u00a0\u00a0 padding: 20,\n \u00a0\u00a0 marginVertical: 8,\n \u00a0\u00a0 marginHorizontal: 16,\n \u00a0\u00a0 borderRadius: 8,\n \u00a0\u00a0 borderWidth: 1,\n \u00a0\u00a0 borderColor: '#ddd',\n  },\n  text: {\n \u00a0\u00a0 fontSize: 18,\n \u00a0\u00a0 fontWeight: 'bold',\n  },\n});<\/pre><p>These styles define padding, margin, and basic colors, giving each item a card-like appearance.<\/p><h5><strong>Step 4: Animations with <\/strong><strong><code>react-native-reanimated<\/code><\/strong><strong> (Optional)<\/strong><\/h5><p>If you\u2019d like more control over scaling or want to animate other properties during dragging, consider integrating <code>react-native-reanimated<\/code>. Here\u2019s a quick example of how to achieve a smooth scale-in and scale-out effect with <code>Reanimated<\/code>.<\/p><ol><li>Install Reanimated and import it.<\/li><li>Wrap each item in an <code>Animated.View<\/code>, where the scale value changes based on dragging state.<\/li><\/ol><p>import Animated, { useAnimatedStyle, withSpring } from &#8216;react-native-reanimated&#8217;;<\/p><pre>const renderItem = ({ item, drag, isActive }) =&gt; {\n  const scale = isActive ? withSpring(1.1) : withSpring(1);\n  const animatedStyle = useAnimatedStyle(() =&gt; ({\n \u00a0\u00a0 transform: [{ scale }],\n\u00a0 }));\n\n  return (\n \u00a0\u00a0 &lt;Animated.View style={[styles.itemContainer, animatedStyle]}&gt;\n \u00a0\u00a0\u00a0\u00a0 &lt;Text style={styles.text} onLongPress={drag}&gt;\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {item.label}\n \u00a0\u00a0\u00a0\u00a0 &lt;\/Text&gt;\n \u00a0\u00a0 &lt;\/Animated.View&gt;\n  );\n};<\/pre><h4><strong>Testing and Troubleshooting<\/strong><\/h4><p>Run the app on a device or emulator, and test the drag-and-drop functionality by pressing and holding an item, then moving it around.<\/p><p><strong>Tips<\/strong><\/p><ul><li>Experiment with <code>activeScale<\/code> in <code>ScaleDecorator<\/code> for the ideal scale.<\/li><li>Adjust item padding and margin to create a layout that suits your design.<\/li><li>Use <code>withSpring()<\/code> in Reanimated for a smooth scale-in-out transition.<\/li><\/ul><h2><strong>Conclusion<\/strong><\/h2><p>Adding a draggable <code>FlatList<\/code> with a scale decorator in <a href=\"\/hire-developers\/hire-react-native-developer\/\">React Native<\/a> enhances user interaction by giving visual feedback and creating a seamless drag-and-drop experience. Whether for a to-do list, task manager, or even a reordering UI, the scale decorator makes the app feel polished and responsive.<\/p><p>By combining <code>react-native-draggable-flatlist, ScaleDecorator,<\/code> and optional Reanimated animations, you can create a dynamic and interactive list that stands out. Now, you\u2019re ready to implement an engaging, drag-friendly list that users will love!<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Creating a visually interactive and user-friendly experience is key in mobile app development. React Native&#8217;s FlatList is highly versatile and efficient, commonly used for rendering large lists. With the added benefit of drag-and-drop functionality, you can make your FlatList even more engaging. In this article, we&#8217;ll explore how to implement a draggable FlatList in React [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":43760,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-43742","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/posts\/43742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/comments?post=43742"}],"version-history":[{"count":0,"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/posts\/43742\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/media\/43760"}],"wp:attachment":[{"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/media?parent=43742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/categories?post=43742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.carmatec.com\/es_mx\/wp-json\/wp\/v2\/tags?post=43742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}