【问题标题】:Change the Color of a Switch - FormBuilderSwitch - flutter更改开关的颜色 - FormBuilderSwitch - flutter
【发布时间】:2021-06-09 10:32:04
【问题描述】:

如果切换正常,我想更改开关的颜色。在 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),
                ),

【问题讨论】:

    标签: flutter android-studio dart flutter-layout flutter-animation


    【解决方案1】:

    你可以使用 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,
                    ),
    

    【讨论】:

    • 谢谢。但我不想要一个新的开关,而不是 formbuilderswitch。代码中的switch怎么处理?
    • 我用 formbuilderswitch @AndreasSüßbauer 更新了它
    • 非常感谢,但它对我不起作用。颜色不变。
    • 你能详细说明你想改变什么颜色吗? @AndreasSüßbauer
    • 容器的颜色从黑色变为红色(我选择了红色,因为蓝色是开关的标准颜色),但如您所见,开关保持蓝色。例如,我也想将开关涂成红色。 imgur.com/01xY0fy
    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 2019-06-23
    • 2020-10-20
    • 2018-04-12
    • 2016-07-25
    • 2020-02-19
    • 2013-12-24
    • 2018-10-24
    相关资源
    最近更新 更多