如何让小部件或屏幕在几秒钟后出现?flutter

k10s72fa  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(131)

我正在使用共享偏好创建搜索历史记录,当您单击搜索历史记录时,将显示包含某些信息的产品配置文件。
当产品简介出现时有一颗星星,星根据它是真是假而改变颜色,问题是它总是出现假的。
它是true还是false取决于我从数据库中获得的信息或值。
我注意到的是,检索该值的方法需要几秒钟,这就是为什么该值总是设置为false。
我正在执行的方法会在init状态下给我带来值。在starbutton内部的验证中,我认为favoriteState不同于1,因此为false,因为在init状态下的方法ObtenerEstadoFavorito在屏幕或星形按钮出现时需要几秒钟
有没有办法让视图或屏幕在几秒钟后出现,或者我应该怎么做?

/////  code  ////

    // this is from class search delegate, the part of search history
     onTap: () {
    
                                        Navigator.push(context,
                                            new MaterialPageRoute(builder:
                                                (context) => new PerfilCliente(this.decodedList[i]))
                                        );
                                      },
    
    class PerfilCliente extends StatefulWidget {
      }
      @override
      State<StatefulWidget> createState() {
        return perfilclientestate();
        throw UnimplementedError();
      }
    }
    
    class perfilclientestate extends State<PerfilCliente>{
    String estadofavorito;
    bool estadoiconofavorito;
    List InfoperfilCliente=[];
    
    @override
      void initState() {
    ObtenerEstadoFavorito();
    void ObtenerEstadoFavorito(){
     searchdelegateperfilclienteservices.ObtenerEstadoFavorito(infovalidacionloginmodel2.getIdUsuario(),widget.perfilseleccionado['2']).then((value) =>
            setState(() {
              InfoperfilCliente.addAll(value);
              estadofavorito = InfoperfilCliente[0].IdEstadoFavorito;
    )
        );}
    }
    }
    
    Widget PerfilUsuario(){
    return Container(
    child: StarButton(
     isStarred:  estadofavorito == "1" ? estadoiconofavorito = true : estadoiconofavorito = false,
                        iconSize: 40,
     valueChanged: (value){
                          setState(() {
                            estadoiconofavorito = value;
                          });
    }
    )
    );
    }
    
     @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
              backgroundColor: Colors.blueAccent,
              title: Text("name"),
              centerTitle: true
          ),
          body: PerfilUsuario(),
    
        );
        throw UnimplementedError();
      }
    }
kxxlusnw

kxxlusnw1#

如果我没理解错的话,从数据库中获取数据是需要时间的,所以默认值会先出现。
在本例中,我们可以使用future.delay函数将事件延迟一段时间。future.delay用法很简单:

Future.delayed(const Duration(seconds: 4), () {
  //write the event you want it to happen after duration of seconds

})

你可以先获取数据,然后在函数上使用future.delay,使你的数据出现,我认为这个PerfilUsuario()是这样的:

void PerfilUsuario() {
    Future.delayed(Duration(seconds: 2), () {
      return Container(
          child: StarButton(
              isStarred: estadofavorito == "1"
                  ? estadoiconofavorito = true
                  : estadoiconofavorito = false,
              iconSize: 40,
              valueChanged: (value) {
                setState(() {
                  estadoiconofavorito = value;
                });
              }));
    });
  }

相关问题