flutter - 如何在颤动中自定义单选按钮?
解决方案
我写了一个模仿单选按钮行为的可重复使用的小部件:
自定义收音机小部件
class CustomRadioWidget<T> extends StatelessWidget {
final T value;
final T groupValue;
final ValueChanged<T> onChanged;
final double width;
final double height;
CustomRadioWidget({this.value, this.groupValue, this.onChanged, this.width = 32, this.height = 32});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: GestureDetector(
onTap: () {
onChanged(this.value);
},
child: Container(
height: this.height,
width: this.width,
decoration: ShapeDecoration(
shape: CircleBorder(),
gradient: LinearGradient(
colors: [
Color(0xFF49EF3E),
Color(0xFF06D89A),
],
),
),
child: Center(
child: Container(
height: this.height - 8,
width: this.width - 8,
decoration: ShapeDecoration(
shape: CircleBorder(),
gradient: LinearGradient(
colors: value == groupValue ? [
Color(0xFFE13684),
Color(0xFFFF6EEC),
] : [
Theme.of(context).scaffoldBackgroundColor,
Theme.of(context).scaffoldBackgroundColor,
],
),
),
),
),
),
),
);
}
}
您可以将其添加到您的代码中,例如:
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CustomRadioWidget(
value: "0",
groupValue: _radValue,
onChanged: (String value) {
setState(() {
_radValue = value;
});
},
),
CustomRadioWidget(
value: "1",
groupValue: _radValue,
onChanged: (String value) {
setState(() {
_radValue = value;
});
},
),
],
),
输出如下所示:
推荐阅读
- java - 使用 JPA 持久性更新和恢复更改
- python - 递归与迭代时间复杂度
- javascript - 为什么 Function 的实例化没有存储在 Function.prototype 中的所有属性?
- python - 烧瓶的烧瓶邮件扩展在应用程序工厂结构中不起作用
- c - RPI 零 - 裸机 - PWM
- javascript - Javascript连接多个字符串变量并在span中显示
- vb.net - Dropbox API Upload 等待异步函数
- django - “方法不允许(发布):/”在家里使用联系我们表格时出错
- mongodb - 无法在 MongoDB 4.0 事务中运行 2 个 findAndUpdate 查询
- reactjs - 是否有我可以在接口中使用的接口类型?