gpt4 book ai didi

flutter - 如何自定义Flutter Material 3 Switch?

转载 作者:行者123 更新时间:2023-12-03 07:58:00 30 4
gpt4 key购买 nike

如何自定义 Material 3 Switch?

enter image description here

enter image description here

正如你所看到的,我已经将大部分开关自定义为红色,这里唯一缺少的是“边框”,由于某种原因它被设置为白色,我看不到可以修改为红色的参数.

我还阅读了 Switch 类的 Flutter 文档:https://api.flutter.dev/flutter/material/Switch-class.html但 2 个示例使用 Material 2,唯一的 Material 3 示例不会修改默认边框。

这是当前的小部件配置:

Switch(
// focusColor: Colors.blue,
trackColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return Colors.red;
}
return Colors.red.withOpacity(.1);
},
),
overlayColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.red;
}
return Colors.white.withOpacity(.1);
},
),
// hoverColor: Colors.green,
thumbColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return HSLColor.fromColor(Colors.red)
.withLightness(0.2)
.toColor();
}
return Colors.red;
},
),
splashRadius: 24,
// inactiveTrackColor: Colors.pink,
// activeTrackColor: Colors.green,
thumbIcon: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return const Icon(Icons.check, color: Colors.red);
}
return null;
},
),
// activeColor: Colors.lime,
// inactiveThumbColor: Colors.orange,
value: _value,
onChanged: (bool value) => setState(() => _value = !_value),
)

注释的参数是我已经尝试过的。

我做了一个 DartPad,你可以在其中看到演示:https://dartpad.dev/?id=6c4ab5c547f7b7a5a0821d7dc9962485 .

最佳答案

将主题更改为

theme: ThemeData.dark(
useMaterial3: true,
).copyWith(
colorScheme:
Theme.of(context).colorScheme.copyWith(outline: Colors.red),
scaffoldBackgroundColor: Colors.black,
),

输出 enter image description here

关于flutter - 如何自定义Flutter Material 3 Switch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75526521/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com