【发布时间】:2021-11-10 11:41:56
【问题描述】:
【问题讨论】:
-
分享你的代码
【问题讨论】:
虽然默认颜色是白色,因为
null 时默认为 [CupertinoColors.white]。
我们可以使用thumbColor根据活动状态分配颜色。
CupertinoSwitch(
activeColor: Colors.white,
thumbColor: on ? Colors.white : Colors.amber,
trackColor: Colors.white,
value: on,
onChanged: (value) => setState(() {
on = !on;
}),
)
演示小部件
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class MyAppH extends StatefulWidget {
const MyAppH({Key? key}) : super(key: key);
@override
State<MyAppH> createState() => _MyAppHState();
}
class _MyAppHState extends State<MyAppH> {
bool on = true;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CupertinoSwitch(
activeColor: Colors.white,
thumbColor: on ? Colors.white : Colors.amber,
trackColor: Colors.white,
value: on,
onChanged: (value) => setState(() {
on = !on;
}),
),
),
);
}
}
【讨论】:
import 'package:flutter/cupertino.dart';