Bỏ qua để đến nội dung

Trưng bày sản phẩm

Ảnh sản phẩm cắt sát viền, xen kẽ dáng dọc và ngang: khối liquid rót từ dáng này sang dáng kia.

Demo chạy thật. Vuốt, chạm thử.Mở toàn màn hình ↗
  1. Ảnh PNG nền trong suốt là thứ hiệu ứng liquid sinh ra để dùng: khối lỏng lấy viền của sản phẩm này rồi đông lại thành sản phẩm kế tiếp.
  2. mediaHeight và mediaPadding chừa chỗ cho sản phẩm; mediaFit mặc định là contain.
  3. getPlaceholderColor phủ vùng ảnh bằng màu của sản phẩm, nhấp nháy nhẹ, trong lúc ảnh từ mạng tải; ảnh hiện dần lên trên.
  4. renderFooter nhận item hiện tại: nút lấy màu của nó.

Dùng

Đây chính là mã đang chạy trong khung điện thoại. Chép về, đổi dữ liệu là dùng được.

example/app/product.tsx
import { MorphCarousel } from 'expo-morph-carousel';
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
import { showcase } from '../data/products';
import { column } from '../data/layout';
/**
* The layout from the SwiftUI "morphing carousel": a card with the product
* image on top, the title and description sliding underneath, dots and a
* button at the bottom. The image melts into the next one while the text
* slides.
*/
export default function ProductScreen() {
return (
// "automatic": on iOS the native header is translucent and the screen
// starts under it; the scroll view insets its content below the header.
<ScrollView contentInsetAdjustmentBehavior="automatic" contentContainerStyle={[styles.screen, column]}>
<Text style={styles.section}>Demo</Text>
<MorphCarousel
testID="product-carousel"
data={showcase}
keyExtractor={(p) => p.id}
getImage={(p) => p.image}
getPlaceholderColor={(p) => p.color}
getAccessibilityLabel={(p) => p.title}
accessibilityLabel="Products"
mediaHeight={300}
// Tapping works too: the left third goes back, the rest forward.
tapToNavigate
mediaPadding={28}
style={styles.card}
renderItem={({ item }) => (
<View style={styles.page}>
<Text style={styles.title}>{item.title}</Text>
<Text style={styles.description}>{item.description}</Text>
</View>
)}
renderFooter={({ item }) => (
<Pressable style={[styles.button, { backgroundColor: item?.color ?? '#1c1c1e' }]}>
<Text style={styles.buttonText}>Continue</Text>
</Pressable>
)}
/>
</ScrollView>
);
}
const styles = StyleSheet.create({
screen: { padding: 16 },
section: { fontSize: 13, color: '#6e6e73', marginBottom: 8, marginLeft: 4 },
card: { height: 560, borderRadius: 28, backgroundColor: '#fff' },
page: { flex: 1, alignItems: 'center', paddingHorizontal: 28, paddingTop: 12, gap: 10 },
title: { fontSize: 24, fontWeight: '700', color: '#1c1c1e' },
description: { fontSize: 15, lineHeight: 21, color: '#6e6e73', textAlign: 'center' },
button: {
alignSelf: 'stretch',
marginHorizontal: 24,
paddingVertical: 14,
borderRadius: 14,
alignItems: 'center',
},
buttonText: { color: '#fff', fontSize: 16, fontWeight: '600' },
});