* 鸿蒙 Example —— 覆盖 react-native-radial-gradient 的全部公开 API 分支(对齐 01-analysis-prd.md §三):
* ① 多色渐变(colors::multiColorOrdered) → colors ≥2,均分停靠
* ② 自定义停靠位(stops::matchedLength) → colors + stops(长度匹配)
* ③ 单色 5 档渐隐(colors::singleColorFiveStopFade)→ colors 单元素(原生侧 populateColors 扩展)
* ④ 自定义中心/半径(center::explicitXY / radius::explicitPositive)
* ⑤ 缺省对照(colors::emptyOrUndefined / center::invalidLengthFallbackToViewCenter /
* radius::lessThanOneFallbackToMinHalf)→ 回落上游初值 [RED,BLUE] + 组件中心 + min/2 半径
* ⑥ 四色均分
* ⑦⑧ 完全透明两种写法(rgba(...,0) vs 'transparent')→ 考察 alpha 位是否被 ArkUI 丢弃
* ⑨ stops 长度不符(stops::mismatchedLengthFallbackEven)→ 丢弃改均分
* ⑩ stops 越界/不递增 → 不崩溃、仍可辨渐变
* ⑪ radius < 1(radius::lessThanOneFallbackToMinHalf)→ 回落 min(w,h)/2
* ⑫ children 覆盖在渐变层之上(render::childrenOverlay)
*
* 每格都用真实 <RadialGradient> 渲染(library 真实实现,非 example 内重复实现)。
*/
import * as React from 'react';
import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, View } from 'react-native';
import RadialGradient from '@hxa-rn/react-native-radial-gradient/index.harmony';
function Cell(props: {
testID: string;
label: string;
colors?: Array<string | number>;
center?: number[];
radius?: number;
stops?: number[];
}): React.JSX.Element {
return (
<View style={styles.cell}>
<RadialGradient
testID={props.testID}
style={styles.gradient}
colors={props.colors ?? []}
center={props.center as number[]}
radius={props.radius as number}
stops={props.stops as number[]}>
<View style={styles.badge}>
<Text style={styles.badgeText}>RN·OHOS</Text>
</View>
</RadialGradient>
<Text style={styles.caption}>{props.label}</Text>
</View>
);
}
export default function App(): React.JSX.Element {
return (
<SafeAreaView style={styles.root}>
<StatusBar barStyle="light-content" backgroundColor="#0E0E12" />
<ScrollView
testID="gallery"
contentContainerStyle={styles.container}
style={styles.root}>
<Text testID="app-title" style={styles.title}>
react-native-radial-gradient
</Text>
{/* ① 多色渐变(均分停靠) */}
<Cell
testID="grad-multi"
label="multi (green→blue→red)"
colors={['#00E676', '#2979FF', '#FF1744']}
/>
{/* ② 自定义停靠位 */}
<Cell
testID="grad-stops"
label="stops [0, 0.3, 1]"
colors={['#FFD600', '#FF6D00', '#6200EA']}
stops={[0, 0.3, 1]}
/>
{/* ③ 单色 → 原生 populateColors 5 档渐隐 */}
<Cell testID="grad-single" label="single (5-shade fade)" colors={['#E91E63']} />
{/* ④ 自定义中心 + 半径 */}
<Cell
testID="grad-center"
label="center [30,30] r=60"
colors={['#FFFFFF', '#1DE9B6', '#00B8D4']}
center={[30, 30]}
radius={60}
/>
{/* ⑤ 缺省对照:无 colors → 上游初值 [RED,BLUE],中心 & 半径回落布局尺寸 */}
<Cell testID="grad-default" label="default (RED→BLUE)" />
{/* ⑥ 四色均分 —— 四段应由内向外依次出现,贴近点落在 0 / 1/3 / 2/3 / 1 */}
<Cell
testID="grad-4color"
label="4color (R→Y→G→B, even)"
colors={['#FF0000', '#FFFF00', '#00FF00', '#0000FF']}
/>
{/* ⑦ 末色为 alpha=0 的品红 → wire 上是 0x00E91E63(与不透明品红只差 alpha 位) */}
<Cell
testID="grad-alpha0-rgb"
label="alpha0-rgb (magenta→rgba(...,0))"
colors={['#E91E63', 'rgba(233,30,99,0)']}
/>
{/* ⑧ 末色为 'transparent' → wire 上是 0x00000000(低位是黑) */}
<Cell
testID="grad-alpha0-zero"
label="alpha0-zero (magenta→transparent)"
colors={['#E91E63', 'transparent']}
/>
{/* ⑨ 停靠位长度 2、色数 3 → 整串丢弃、改均分;对照 = grad-multi(同色序、无 stops) */}
<Cell
testID="grad-stops-mismatch"
label="stops len2 vs 3 colors (→even)"
colors={['#00E676', '#2979FF', '#FF1744']}
stops={[0, 0.3]}
/>
{/* ⑩ 越界且不递增 → 只验「不崩溃且仍有可辨渐变」,不对具体过渡位置作断言 */}
<Cell
testID="grad-stops-invalid"
label="stops [-0.5, 1.5, 0.2]"
colors={['#00E676', '#2979FF', '#FF1744']}
stops={[-0.5, 1.5, 0.2]}
/>
{/* ⑪ radius 0.5 < 阈值 1 → 忽略、回落 min(w,h)/2;对照 = grad-multi(同色序、无 radius) */}
<Cell
testID="grad-radius-tiny"
label="radius 0.5 (→default min/2)"
colors={['#00E676', '#2979FF', '#FF1744']}
radius={0.5}
/>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
root: { flex: 1, backgroundColor: '#0E0E12' },
container: { alignItems: 'center', paddingVertical: 32, paddingBottom: 64 },
title: { fontSize: 18, fontWeight: '600', marginBottom: 24, color: '#FFF', textAlign: 'center', paddingHorizontal: 16, alignSelf: 'stretch' },
cell: { alignItems: 'center', marginBottom: 32 },
gradient: { width: 140, height: 140, borderRadius: 12 },
caption: { marginTop: 12, fontSize: 13, color: '#AAA' },
badge: {
position: 'absolute',
bottom: 8,
right: 8,
backgroundColor: 'rgba(0,0,0,0.35)',
paddingHorizontal: 8,
paddingVertical: 3,
borderRadius: 6,
},
badgeText: { color: '#FFF', fontSize: 11, fontWeight: '600' },
});