【发布时间】:2023-01-10 15:20:46
【问题描述】:
【问题讨论】:
-
Sindu 请查看我的回答here 希望对你有帮助
-
@RavindraS.Patil 答案看起来不错。但我真的在寻找与我添加的图片完全一样的东西。谢谢你的努力
-
@Rahul 这看起来不错。会尝试
【问题讨论】:
截至今天,无法自定义CupertinoSwitch在扑开箱即用,但是嘿! pub.dev 中有许多插件可以满足您的需求,例如flutter_switch。
使用以下代码,您可以实现您想要的效果:
FlutterSwitch(
showOnOff: true,
value: v,
activeIcon: Text("SELL"),
activeText: "BUY",
inactiveIcon: Text("BUY"),
inactiveText: "SELL",
inactiveColor: Colors.blue,
activeTextFontWeight: FontWeight.normal,
inactiveTextFontWeight: FontWeight.normal,
onToggle: (val) {
setState(() {
v = val;
});
},
)
看起来像这样:
这当然只是一个示例,您可以进一步自定义以获得更漂亮的结果。
【讨论】:
我创建了自定义小部件,但其中没有动画。我希望这符合您的需求 =))
GestureDetector(
onTap: () {
setState(() {
val = !val;
});
},
child: Container(
width: size.width * 0.35,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: kSecondaryColor),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Container(
width: 60,
height: 30,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(30),
color: val
? Colors.white
: kSecondaryColor),
child: Center(
child: Text(
'BUY',
style: TextStyle(
fontWeight: FontWeight.bold,
color: val
? Colors.black
: Colors.white),
)),
),
Container(
width: 60,
height: 30,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(30),
color: val
? kSecondaryColor
: Colors.white),
child: Center(
child: Text(
'SELL',
style: TextStyle(
fontWeight: FontWeight.bold,
color: val
? Colors.white
: Colors.black),
)),
),
],
),
),
),
),
```[![image][1]][1]
[1]: https://i.stack.imgur.com/4Tr7k.png
【讨论】: