簡體   English   中英

復選框不會更改值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM