iOS风格的切换选钮,常用于配置的切换,可指定颜色,接收状态变化回调。
相关组件
CupertinoSwitch基本使用
<br />【value】 : 是否选中 【double】<br />【activeColor】 : 激活态颜色 【Color】<br />【onChanged】 : 切换回调 【Function(double)】<br />![121.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589455368143-71585491-c3e0-4a7c-ab0f-5daf11446eeb.gif#align=left&display=inline&height=162&margin=%5Bobject%20Object%5D&name=121.gif&originHeight=162&originWidth=740&size=136312&status=done&style=none&width=740)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class CustomCupertinoSwitch extends StatefulWidget {
@override
_CustomCupertinoSwitchState createState() => _CustomCupertinoSwitchState();
}
class _CustomCupertinoSwitchState extends State<CustomCupertinoSwitch> {
final colors = [Colors.red, Colors.yellow, Colors.blue, Colors.green];
bool _checked = false;
@override
Widget build(BuildContext context) {
return Wrap(
spacing: 10,
children: colors
.map((e) => CupertinoSwitch(
value: _checked,
activeColor: e,
onChanged: (v) {
setState(() => _checked = v);
}))
.toList(),
);
}
}