在三元运算符中,当bool变为true时,Flutter容器不会改变

7xzttuei  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(143)
selectedDateTime != null //if true
                        ? Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Colors.grey, Colors.black87],
                        ),
                        color: Colors.white70,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      height: 50,
                      width: 300,
                      child: Row(
                        children: [
                          Container(
                            height: 40,
                            width: 30,
                            child: Image.asset('lib/icons/hourglass.png'),
                          ),
                          SizedBox(width: 15,),
                          Text(selectedDateTime!.day.toString()+"/"+selectedDateTime!.month.toString()+selectedDateTime!.year.toString()
                            +" - "+selectedDateTime!.hour.toString()+":"+selectedDateTime!.minute.toString()
                            , style: GoogleFonts.outfit(fontSize: 20),),
                          SizedBox(width: 45,),
                          GestureDetector(
                            onTap: (){
                              setState(() {
                                selectedDateTime=null;
                              });
                            },
                            child: Container(
                              height: 40,
                              width: 30,
                              child: Image.asset('lib/icons/remove.png'),
                            ),
                          ),
                        ],
                      ),
                    )//if false
                        : Container(
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Colors.deepPurple, Colors.greenAccent],
                        ),
                        color: Colors.white70,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      height: 50,
                      width: 300,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Text('Set Reminder', style: GoogleFonts.outfit(fontSize: 20),),
                          Container(
                            height: 40,
                            width: 40,
                            child: Image.asset('lib/icons/clock.png'),
                          ),
                        ],
                      ),
                    ),
                  ),

字符串
当我更改selecteddatetime变量时,它不会在第一次更改容器,但当我单击它上面的textfield时,容器会发生变化

rqenqsqc

rqenqsqc1#

当代码不完整时,很难找到问题。你应该提供一个完整的代码(该文件中的所有代码),以帮助人们。
如果这是一个有状态的小部件,请在更新selectedDateTime的位置添加setState({selectedDateTime =“something”}),以确保当selectedDateTime更新为新值时,小部件会重新呈现。

相关问题