Flutter 之 CupertinoSwitch (iOS風(fēng)格)(六十六)

CupertinoSwitch 是 iOS 風(fēng)格的 switch 開關(guān),相當(dāng)于 UISwitch。

1. CupertinoSwitch

CupertinoSwitch 定義

  const CupertinoSwitch({
    Key? key,
    required this.value,
    required this.onChanged,
    this.activeColor,
    this.trackColor,
    this.thumbColor,
    this.dragStartBehavior = DragStartBehavior.start,
  })

CupertinoSwitch

屬性 介紹
value @required 當(dāng)前 switch 的開關(guān)
onChanged @required
activeColor 開關(guān)打開時,軌道顏色
trackColor 開關(guān)關(guān)閉時,軌道顏色
thumbColor 滑塊顏色
dragStartBehavior 拖拽效果,默認(rèn)為 start 更為平滑,為 down 時有明顯吸附效果,默認(rèn)為 DragStartBehavior.start

2. 示例


class _MSCupertinoSwitchDemo1State extends State<MSCupertinoSwitchDemo1> {
  bool _switchValue1 = false;
  bool _switchValue2 = false;
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar:
          CupertinoNavigationBar(middle: Text("MSCupertinoSwitchDemo1")),
      child: SafeArea(
        child: Center(
          child: Column(
            children: [
              CupertinoSwitch(
                value: _switchValue1,
                activeColor: Colors.yellow, // 選中時 軌道顏色
                thumbColor: Colors.orange, // 滑塊顏色
                trackColor: Colors.cyan[200], // 未選中時 軌道顏色
                onChanged: (value) {
                  _switchValue1 = value;
                  setState(() {});
                },
              ),
              SizedBox(height: 20),
              CupertinoSwitch(
                value: _switchValue2,
                activeColor: Colors.tealAccent, // 選中時 軌道顏色
                thumbColor: Colors.red, // 滑塊顏色
                trackColor: Colors.grey[200], // 未選中時 軌道顏色
                dragStartBehavior: DragStartBehavior.down,// 默認(rèn)start 更為平滑,為 down 時有明顯吸附效果
                onChanged: (value) {
                  _switchValue2 = value;
                  setState(() {});
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

92.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容