[英]Checkbox doesn't change the value
我对复选框有一个大问题。 如果我点击复选框,他会显示 _value 更改但在设置 state _value 恢复到第一个值之后。 如果我在文本字段中引入数据,然后我尝试选中该框,我的文本就会消失。
这是第一个 state 的值: bool _value = false;
这是复选框所在容器的代码:
padding: EdgeInsets.only(top: 35.0, left: 20.0, right: 20.0),
child:( _futureUser == null)
? Column(
children: <Widget>[
Container(
child:Row(children:<Widget>[
Text('Account type:',style:TextStyle(
fontSize: 16,
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),),
SizedBox(width: 13,),
CupertinoSlidingSegmentedControl(
// padding: EdgeInsets.fromLTRB(100, 0, 100, 0),
groupValue: segmentedControlGroupValue,
children: myTabs,
onValueChanged: (i){
setState(() {
if( i == 0) _role = "patient";
else if( i == 1 )_role = "doctor";
segmentedControlGroupValue = i;
print(_role);
});
},
)])),
// SizedBox(height: 10.0,),
TextField(
controller: _namecontroller,
decoration: InputDecoration(
labelText: 'Name',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
// hintText: 'Nume',
// hintStyle: ,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.redAccent))),
),
SizedBox(height: 10.0),
TextField(
controller: _usernamecontroller,
decoration: InputDecoration(
labelText: 'Username ',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.redAccent))),
// onChanged: (val) => newUser.email = val,
),
SizedBox(height: 10.0),
TextField(
controller: _email,
decoration: InputDecoration(
labelText: 'Phone ',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.redAccent))),
// onChanged: (val) => newUser.email = val,
),
SizedBox(height: 10.0),
TextField(
controller: _passwordcontroller,
decoration: InputDecoration(
labelText: 'Password ',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.redAccent))),
obscureText: true,
// onChanged: (val) => newUser.password = val,
),
SizedBox(height: 10.0),
TextField(
controller: _passwordConfirmcontroller,
decoration: InputDecoration(
labelText: 'Confirm your Password ',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color:Colors.redAccent))),
obscureText: true,
// onChanged: (val) => newUser.passwordConfirm = val,
),
SizedBox(height: 10.0,),
Checkbox(
value: _value,
onChanged: (bool newValue) {
print(_value);
setState(() {
_value = newValue;
print(_value);
});
},
),
SizedBox(height: 20.0),
我不能立即说出问题可能是什么,但现在有很多可以改进的地方,这会让你更容易找到它::)
1.我建议在每个))}
和类似结构中添加尾随逗号,使它们看起来像,),),]
,然后点击自动格式化程序(在 VSCode Alt+Shift+F 中),这应该会使事情更容易阅读.
2.虽然这
if( i == 0) _role = "patient";
else if( i == 1 )_role = "doctor";
从技术上讲,它是否经常出现不可预见的问题,我建议在这些命令周围添加{}
以确保它不会适得其反。
3. SetState
也应该包含尽可能少的逻辑。 虽然它可以处理一些计算,但最好只将新值传递给SetState
,这样只有与它相关的部分才会被重新加载。
4. 发生了什么事
( _futureUser == null)
? Column(
在此期间是否可以更改并切换到该声明的替代部分? (只是根据我们在这里看到的内容进行推断:))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.