Flutter 来回获取class字段值
Flutter get class field value back and forth
我正在尝试创建一个自定义复选框小部件,但我无法从另一个 class 获取该复选框的 bool 值:
所以我在有状态小部件注册中有一个表单,在这个表单中我调用我的 CustomCheckBox 小部件(也是一个有状态小部件)。
问题:当我单击复选框时,它的值在 CustomCheckBox 小部件中更改为 true,但是在 Signup 小部件中提交表单后,该值始终为 false(接缝是两个小部件之间没有反向通信)
我的 CustomCheckBox 代码:
import 'package:flutter/material.dart';
import 'package:tunimmo/Constants/palette.dart';
class CustomCheckBox extends StatefulWidget {
bool checked;
final String label;
CustomCheckBox({this.checked, this.label});
@override
_CustomCheckBoxState createState() => _CustomCheckBoxState();
}
class _CustomCheckBoxState extends State<CustomCheckBox> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 15.0, left: 30, right: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Container(
height: 30,
decoration: BoxDecoration(
color: myPrimaryColor,
),
child: Text(" "),
),
Text(' ${widget.label}', style: Theme.of(context).textTheme.bodyText1),
],
),
Center(
child: InkWell(
onTap: () {
setState(() {
widget.checked = !widget.checked;
});
},
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: widget.checked ? myPrimaryColor : myWhiteColor,
border: Border.all(
color: myPrimaryColor,
width: 3,
),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: widget.checked
? Icon(
Icons.check,
size: 25.0,
color: myWhiteColor,
)
: Icon(
Icons.check_box_outline_blank,
size: 25.0,
color: myWhiteColor,
),
),
),
)),
],
),
);
}
}
在 Signup 小部件中,我只是调用构造函数并传递一个 bool 字段(预期为 send/get CustomCheckBox 小部件中的值)和一个字符串标签。
PS: 我的表单中有多个复选框。
请指教!
问题是您不是在屏幕(注册)中更改选中的值,而是在自定义复选框中更改,要解决此问题,请在 SignUp 中定义 checked
变量(如果尚未定义),然后定义 Function在 checkbox
小部件中回调如下:
final Function(bool)checkChanged;
CustomCheckBox({this.checked, this.label,this.checkedChanged});
然后在ontap中调用,给值变化
onTap: () {
widget.checkedChanged(!widget.checked);
setState(() {
widget.checked = !widget.checked;
});
}
调用 CustomCheckbox 时执行以下操作:
CustomCheckBox(
checked:false,
label:'a label',
checkedChanged:(val){
checked=val;
}
)
并在提交表单时使用 checked
变量,这应该可以解决您的问题。
我正在尝试创建一个自定义复选框小部件,但我无法从另一个 class 获取该复选框的 bool 值: 所以我在有状态小部件注册中有一个表单,在这个表单中我调用我的 CustomCheckBox 小部件(也是一个有状态小部件)。
问题:当我单击复选框时,它的值在 CustomCheckBox 小部件中更改为 true,但是在 Signup 小部件中提交表单后,该值始终为 false(接缝是两个小部件之间没有反向通信)
我的 CustomCheckBox 代码:
import 'package:flutter/material.dart';
import 'package:tunimmo/Constants/palette.dart';
class CustomCheckBox extends StatefulWidget {
bool checked;
final String label;
CustomCheckBox({this.checked, this.label});
@override
_CustomCheckBoxState createState() => _CustomCheckBoxState();
}
class _CustomCheckBoxState extends State<CustomCheckBox> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 15.0, left: 30, right: 30),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Container(
height: 30,
decoration: BoxDecoration(
color: myPrimaryColor,
),
child: Text(" "),
),
Text(' ${widget.label}', style: Theme.of(context).textTheme.bodyText1),
],
),
Center(
child: InkWell(
onTap: () {
setState(() {
widget.checked = !widget.checked;
});
},
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: widget.checked ? myPrimaryColor : myWhiteColor,
border: Border.all(
color: myPrimaryColor,
width: 3,
),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: widget.checked
? Icon(
Icons.check,
size: 25.0,
color: myWhiteColor,
)
: Icon(
Icons.check_box_outline_blank,
size: 25.0,
color: myWhiteColor,
),
),
),
)),
],
),
);
}
}
在 Signup 小部件中,我只是调用构造函数并传递一个 bool 字段(预期为 send/get CustomCheckBox 小部件中的值)和一个字符串标签。
PS: 我的表单中有多个复选框。
请指教!
问题是您不是在屏幕(注册)中更改选中的值,而是在自定义复选框中更改,要解决此问题,请在 SignUp 中定义 checked
变量(如果尚未定义),然后定义 Function在 checkbox
小部件中回调如下:
final Function(bool)checkChanged;
CustomCheckBox({this.checked, this.label,this.checkedChanged});
然后在ontap中调用,给值变化
onTap: () {
widget.checkedChanged(!widget.checked);
setState(() {
widget.checked = !widget.checked;
});
}
调用 CustomCheckbox 时执行以下操作:
CustomCheckBox(
checked:false,
label:'a label',
checkedChanged:(val){
checked=val;
}
)
并在提交表单时使用 checked
变量,这应该可以解决您的问题。