gpt4 book ai didi

flutter - 复选框不会更改值

转载 作者:行者123 更新时间:2023-12-05 07:10:10 25 4
gpt4 key购买 nike

我对复选框有很大的疑问。如果我点击复选框,他会显示 _value 发生变化,但在设置状态后 _value 恢复到第一个值。如果我在文本字段中引入数据,然后尝试选中该框,我的文本就会消失。

这是值的第一个状态: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(

可以同时更改那个语句并切换到该语句的替代部分吗? (只是根据我们在这里看到的进行推断 :) )

关于flutter - 复选框不会更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61334207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com