简体   繁体   English

更改开关的颜色 - FormBuilderSwitch - flutter

[英]Change the Color of a Switch - FormBuilderSwitch - flutter

I want to change the Color for the Switch if normal an if switched.如果正常,如果切换,我想更改开关的颜色。 In the Material.dart I find some stuff but cannot write it right.在 Material.dart 我找到了一些东西,但写不正确。 If you could do some examples that would be great.如果你能做一些例子那就太好了。 Thank you谢谢

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

You can use bool to do that你可以使用 bool 来做到这一点

Example:例子:

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

Widget to trigger switch function小部件触发开关 function

FormBuilderSwitch(
                        onChanged: (bool) {
                          setState(() {
                            bool = !bool;
                            switched = bool;
                          });
                        },
                      )

and here's an example of color change based on bool这是一个基于 bool 的颜色变化示例

Container(
   color: switched? Colors.white : Colors.blue,
)

Update:更新:

Here's the code这是代码

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,
            ),

The outputs输出

https://i.stack.imgur.com/x3j35.png https://i.stack.imgur.com/x3j35.png

https://i.stack.imgur.com/XUBpy.png https://i.stack.imgur.com/XUBpy.png

Update:更新:

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,
                ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM