几秒钟后如何返回小部件?

时间:2019-07-31 01:30:24

标签: flutter dart

        return
          Container(
              color: Colors.transparent,
              child:
              Column(
                children: <Widget>[
                  Container(
                      color: Colors.transparent,
                      height: 30,
                      child:
                      RaisedButton(
                        child:
                        Column(
                          children: <Widget>[
                            Text("Test"),
                          ],
                        ),
                        color: Colors.transparent,
                        elevation: 0,
                        splashColor: Colors.transparent,
                        //onPressed: () {
                        //  Navigator.push(context, MaterialPageRoute(builder: (context) => ToSchoolScreen()));
                        //},
                      )
                  ),
                ],
              )
          );

我上面有这样的代码。 我想在几秒钟后返回这个容器。 但是,如果我像这样直接使用Future

        return
            Future.delayed(Duration(milliseconds: 500), () {
              Container(
                  color: Colors.transparent,
                  child:
                  Column(
                    children: <Widget>[
                      Container(
                          color: Colors.transparent,
                          height: 30,
                          child:
                          RaisedButton(
                            child:
                            Column(
                              children: <Widget>[
                                Text("Test"),
                              ],
                            ),
                            color: Colors.transparent,
                            elevation: 0,
                            splashColor: Colors.transparent,
                            //onPressed: () {
                            //  Navigator.push(context, MaterialPageRoute(builder: (context) => ToSchoolScreen()));
                            //},
                          )
                      ),
                    ],
                  )
              );
            });

我遇到错误type `Future<dynamic> ' is not a subtype of type 'Widget'。 如何解决这个问题?

4 个答案:

答案 0 :(得分:3)


FutureBuilder(
    future: Future.delayed(Duration(milliseconds: 500)),
    builder: (context, snapshot) {
// Checks whether the future is resolved, ie the duration is over
        if (snapshot.connectionState == ConnectionState.done) 
            return MyWidget();
        else 
            return Container(); // Return empty container to avoid build errors
    }
);

答案 1 :(得分:1)

您可以使用FutureBuilder作为父窗口小部件,并使用future函数在几秒钟后返回数据。 请阅读文档以供将来的构建者使用 https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

答案 2 :(得分:-1)

 Future.delayed(Duration(milliseconds: 500), () {
testing();
})

创建小部件方法

Widget testing(){
 return
          Container(
              color: Colors.transparent,
              child:
              Column(
                children: <Widget>[
                  Container(
                      color: Colors.transparent,
                      height: 30,
                      child:
                      RaisedButton(
                        child:
                        Column(
                          children: <Widget>[
                            Text("Test"),
                          ],
                        ),
                        color: Colors.transparent,
                        elevation: 0,
                        splashColor: Colors.transparent,
                        //onPressed: () {
                        //  Navigator.push(context, MaterialPageRoute(builder: (context) => ToSchoolScreen()));
                        //},
                      )
                  ),
                ],
              )
          );
}

答案 3 :(得分:-1)

检查我的代码

FutureBuilder(
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.hasError) {
                    return Text('${snapshot.error}');
                  } else if (snapshot.hasData) {
                    var datta = jsonDecode(snapshot.data.toString())['response'];
                    if (datta.runtimeType == String) {
                      return Container();
                    } else {
                      List<dynamic> data = jsonDecode(snapshot.data.toString())['response'];
                      List<Widget> child = [Container()];
                      if (data.length != 0 && data.length != null && data != []) {
                        data.forEach((f) {
                          Widget wg = TeklifCard(
                            key: UniqueKey(),
                            bransID: 8,
                            onSatinAl: () {
                              Navigator.push(context, MaterialPageRoute(builder: (context) => KartBilgisi()));
                            },
                            prim: '${f['Prim']}₺',
                            teklifNo: '${f['TeklifNoWS']}',
                            sKodu: '${f['SKodu']}',
                          );
                          child.add(wg);
                        });
                      }


                      return Column(
                        key: Key('${Random().nextInt(45847)}'),
                        mainAxisAlignment: MainAxisAlignment.start,
                        mainAxisSize: MainAxisSize.max,
                        children: <Widget>[
                          Expanded(
                            child: SingleChildScrollView(
                              physics: ClampingScrollPhysics(),
                              child: Column(
                                mainAxisSize: MainAxisSize.max,
                                children: child,
                              ),
                            ),
                          ),
                        ],
                      );
                    }
                  } else {
                    return Center(
                      child: Text('Hata'),
                    );
                  }
                } else {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
              },
              future: getOffers(),
            ),