Flutter 容器不会随着三元运算符中的 bool 变为 true 而改变

问题描述 投票:0回答:1
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
时,容器会发生变化

flutter flutter-animation flutter-container
1个回答
0
投票

代码不完整时很难发现问题。您应该提供完整的代码(该文件中的所有代码)以供人们帮助。

如果这是一个有状态小部件,请在更新 selectedDateTime 的位置添加 setState({selectedDateTime = "something"}),以确保当 selectedDateTime 使用新值更新时,小部件会重新渲染。

© www.soinside.com 2019 - 2024. All rights reserved.