我正在尝试将其添加到我的表单中。
正在添加药物的我有四种类型的药物:注射器,瓶子,药丸或片剂。到目前为止,我已经完成了用GestureDetector包装的图标行,以查看用户是否选择了一种类型,但是我的inputVariable正在更新,但是ui并未更改..我不确定是什么问题..任何帮助将不胜感激谢谢!
这是我的代码:
// Code in Form
Padding(
padding: EdgeInsets.only(top: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
MedicineTypeColumn(
type: MedicineType.Bottle,
name: "Bottle",
icon: FontAwesomeIcons.prescriptionBottle,
isSelected: a == MedicineType.Bottle ? true : false),
MedicineTypeColumn(
type: MedicineType.Pill,
name: "Pill",
icon: FontAwesomeIcons.capsules,
isSelected: a == MedicineType.Pill ? true : false),
MedicineTypeColumn(
type: MedicineType.Syringe,
name: "Syringe",
icon: FontAwesomeIcons.syringe,
isSelected: a == MedicineType.Syringe ? true : false),
MedicineTypeColumn(
type: MedicineType.Tablet,
name: "Tablet",
icon: FontAwesomeIcons.ban,
isSelected: a == MedicineType.Tablet ? true : false),
],
),
),
// New Widget for MedicineTypes
class MedicineTypeColumn extends StatefulWidget {
final MedicineType type;
final String name;
final IconData icon;
final bool isSelected;
MedicineTypeColumn(
{Key key,
@required this.type,
@required this.name,
@required this.icon,
@required this.isSelected})
: super(key: key);
@override
_MedicineTypeColumnState createState() => _MedicineTypeColumnState();
}
class _MedicineTypeColumnState extends State<MedicineTypeColumn> {
@override
Widget build(BuildContext context) {
List<Medicine> medTemp = Medicine.medList;
return GestureDetector(
onTap: () {
setState(() {
medTemp[0].medicineType = widget.type.toString();
print(medTemp[0].medicineType);
print(medTemp[0].medicineType.runtimeType);
});
},
child: Column(
children: <Widget>[
Container(
width: 85,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: widget.isSelected ? Colors.green : Colors.white,
),
child: Center(
child: Padding(
padding: EdgeInsets.only(top: 14.0),
child: Icon(
widget.icon,
size: 45,
color: widget.isSelected ? Colors.white : Color(0xFF3EB16F),
),
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: Container(
width: 80,
height: 30,
decoration: BoxDecoration(
color:
widget.isSelected ? Color(0xFF3EB16F) : Colors.transparent,
borderRadius: BorderRadius.circular(12),
),
child: Center(
child: Text(
widget.name,
style: TextStyle(
fontSize: 16,
color: widget.isSelected ? Colors.white : Color(0xFF3EB16F),
fontWeight: FontWeight.w500,
),
),
),
),
)
],
),
);
}
}
记录显示inputVariable正在更新。
I/flutter ( 4614): MedicineType.Pill
I/flutter ( 4614): String
答案 0 :(得分:1)
您的GestureDetector应该在MedicineTypeColumn之外。您可以使MedicineTypeColumn无状态窗口小部件,并在具有表单的其他窗口小部件中管理状态。在该行的每个项目上,添加手势检测器:
// Code in Form
GestureDetector(
onTap: () {
setState(() {
a = MedicineType.Bottle;
});
},
child: MedicineTypeColumn(
type: MedicineType.Bottle,
name: "Bottle",
icon: FontAwesomeIcons.prescriptionBottle,
isSelected: a == MedicineType.Bottle,
),
)