React native 用记号标记本机拨动开关

React native 用记号标记本机拨动开关,react-native,React Native,我想要一个在react native中的切换开关,它应该在打开时显示勾号,在关闭时显示十字标记 应该是 [反应本机切换开关] 请帮助我如何获得它。使用下面的代码,用您的本地图像更新图像,如果您有任何疑问,请告诉我 /** *拨动开关 *用于react native的切换开关组件,适用于iOS和Android * https://github.com/aminebenkeroum/toggle-switch-react-native *电子邮件:amine。benkeroum@gmail.com

我想要一个在react native中的切换开关,它应该在打开时显示勾号,在关闭时显示十字标记

应该是

[反应本机切换开关]


请帮助我如何获得它。

使用下面的代码,用您的本地图像更新图像,如果您有任何疑问,请告诉我

/**
*拨动开关
*用于react native的切换开关组件,适用于iOS和Android
* https://github.com/aminebenkeroum/toggle-switch-react-native
*电子邮件:amine。benkeroum@gmail.com
*博客:https://medium.com/@阿明本克鲁姆/
*@benkeroumamine
*/
从“React”导入React;
进口{
样式表,
文本,
看法
可触摸不透明度,
有生气的
形象,,
}从“反应本机”;
从“道具类型”导入道具类型
将*作为AppImage从“~/assets”导入;
导出默认类ToggleSwitch扩展React.Component{
静态计算尺寸(尺寸){
开关(尺寸){
“小型”案例:
返回({
宽度:50,填充:10,圆圈宽度:15,圆圈宽度:15,平动度:22,
});
“大型”案例:
返回({
宽度:100,填充:20,圆圈宽度:30,圆圈宽度:30,平动度:38,
});
违约:
返回({
宽度:60,填充:12,圆圈宽度:18,圆圈宽度:18,平动度:26,
});
}
}
静态类型={
isOn:PropTypes.bool.isRequired,
标签:PropTypes.string,
onColor:PropTypes.string.isRequired,
offColor:PropTypes.string.isRequired,
大小:PropTypes.string,
labelStyle:PropTypes.object,
onToggle:PropTypes.func.isRequired,
图标:PropTypes.object,
}
静态defaultProps={
伊森:错,
onColor:“#634fc9”,
offColor:“#ecf0f1”,
尺寸:'中等',
标签样式:{},
图标:空,
}
offsetX=新的动画值(0);
尺寸=切换开关。计算尺寸(此.props.size);
CreateTogleSwitchStyle=()=>({
为内容辩护:“中心”,
宽度:此为.dimensions.width,
边界半径:20,
填充:this.dimensions.padding,
背景颜色:(this.props.isOn)?this.props.onColor:this.props.offColor,
})
CreateInsideCrcelestyle=()=>({
对齐项目:“居中”,
为内容辩护:“中心”,
差额:4,
位置:'绝对',
背景颜色:“白色”,
转换:[{translateX:this.offsetX}],
宽度:this.dimensions.circleWidth,
高度:此.dimensions.circhlehight,
边界半径:(this.dimensions.circleWidth/2),
});
render(){
const toValue=this.props.isOn
?this.dimensions.width-this.dimensions.translateX
: 0;
时间(
这个.offsetX,,
{
toValue,
持续时间:300,
},
).start();
返回(
{(this.props.label)
?{this.props.label}
:null
}
{
this.props.onToggle(!this.props.isOn);
}}
>
{this.props.icon}
);
}
}
const styles=StyleSheet.create({
容器:{
flexDirection:'行',
对齐项目:“居中”,
},
标签样式:{
marginHorizontal:10,
},

});尝试修改此代码以获得预期的切换按钮我已经尝试了您的代码,但当我单击按钮时,我收到一条错误消息,如undefined不是对象this.props.isOnI已更新我的答案。如果有任何查询或问题,请发表评论。嗨,尼蒂斯,我已经按照你的更新了我的cose,但是我在这行上得到了一个错误,onToggle={(isOn)=>this.setState({!this.state.isOn}}}),所以我把它改为onToggle={(isOn)=>this.setState({!isOn}},但现在我得到的非空AssetOns只能在.ts文件中使用,请帮助我如何修复此问题?不,不要像您描述的那样使用它,像我更新了答案一样使用它。我所做的只是从
onToggle={(isOn)=>this.setState({!this.state.isOn}中删除
isOn
。因此,它可以完美地工作,并更新数据和视图。