[英]Change the Color of a Switch - FormBuilderSwitch - flutter
如果正常,如果切換,我想更改開關的顏色。 在 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
小部件觸發開關 function
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,
),
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.