你可以使用 bool 来做到这一点
例子:
bool switched = false; //Based on which state you want it to be on init
小部件触发开关功能
FormBuilderSwitch(
onChanged: (bool) {
setState(() {
bool = !bool;
switched = bool;
});
},
)
这是一个基于 bool 的颜色变化示例
Container(
color: switched? Colors.white : Colors.blue,
)
更新:
这是代码
FormBuilderSwitch(
name: "public",
title: Text("Wird noch ein Mitspieler gesucht?"),
initialValue: false,
controlAffinity: ListTileControlAffinity.leading,
decoration: InputDecoration(border: InputBorder.none),
onChanged: (bool) {
setState(() {
bool = !bool;
switched = bool;
});
},
),
Container(
height: 20,
width: 20,
color: switched ? Colors.black54 : Colors.blue,
),
输出
https://i.stack.imgur.com/x3j35.png
https://i.stack.imgur.com/XUBpy.png
更新:
FormBuilderSwitch(
name: "public",
title: Text("Wird noch ein Mitspieler gesucht?"),
initialValue: false,
controlAffinity: ListTileControlAffinity.leading,
activeColor: Colors.red
decoration: InputDecoration(border: InputBorder.none),
onChanged: (bool) {
setState(() {
bool = !bool;
switched = bool;
});
},
),
Container(
height: 20,
width: 20,
color: switched ? Colors.red : Colors.black,
),