更改开关的颜色 - FormBuilderSwitch - flutter

Change the Color of a Switch - FormBuilderSwitch - flutter

我想更改 Switch if normal 和 if switched 的颜色。在 Material.dart 中,我找到了一些东西,但写不正确。如果你能做一些例子,那就太好了。谢谢

FormBuilderSwitch(
                  name: "public",
                  title: Text("Wird noch ein Mitspieler gesucht?"),
                  initialValue: widget.event?.public ?? false,
                  controlAffinity: ListTileControlAffinity.leading,
                  decoration: InputDecoration(border: InputBorder.none),
                ),

您可以使用 bool 来做到这一点

示例:

bool switched = false; //Based on which state you want it to be on init

触发开关功能的小部件

FormBuilderSwitch(
                        onChanged: (bool) {
                          setState(() {
                            bool = !bool;
                            switched = 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,
                ),