我有一个Radio
,我想用一个条件禁用它,如:
if (enabled == true){
*enable radio button*
} else {
*disable radio button*
}
编辑我将添加类的完整代码,因为我只添加了其中的一部分。
下面是完整的代码:
import 'package:flutter/material.dart';
class RadioButton extends StatelessWidget {
final String label;
final int groupValue;
final int value;
final ValueChanged<int> onChanged;
const RadioButton(
{super.key,
required this.label,
required this.groupValue,
required this.value,
required this.onChanged});
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Radio<int>(
groupValue: groupValue,
value: value,
onChanged: (int? newValue) {
onChanged(newValue!);
}),
),
Text(label),
],
),
),
],
),
],
);
}
}
禁用Radio
按钮,将onChanged
设置为null
:
return Radio(
value: 1,
groupValue: 1,
onChanged: null,
);
编辑:根据条件禁用:
var isDisabled = true;
Radio(
value: 1,
groupValue: 1,
onChanged: isDisabled ? null : (value) {},
来自onChanged
的文档:
如果为空,单选按钮将显示为禁用
你可以记住这个设置null
的技巧,因为它也禁用了其他Icons
/小部件。