简体   繁体   English

Futurebuilder 里面的 expansiontile flutter

[英]futurebuilder inside expansiontile flutter

I have a screen that consist of ExpansionTile and as we know that ExpansionTile has children property.我有一个由ExpansionTile组成的屏幕,我们知道ExpansionTilechildren属性。 The thing that I would like to do is I need to catch the trigger click of ExpansionTile , so when user click the ExpansionTile it will run FutureBuilder to get a data from API, is there a way to do that.我想做的是我需要捕捉ExpansionTile的触发click ,所以当用户点击ExpansionTile时,它将运行 FutureBuilder 以从 API 获取数据,有没有办法做到这一点。 Because until now... ExpansionTile always runs together with the children Here is part of the code因为直到现在...... ExpansionTile总是和孩子们一起运行这是代码的一部分

ExpansionTile(
children:[] //here.. I would like to call the data from api, when user click the expansiontile
                                            title: Text(
                                                "${dataAll["data"][i]["lowongan"]}",
                                                style: GoogleFonts.poppins(
                                                    fontWeight: FontWeight.w600,
                                                    color: isHovered ||
                                                            listLokerModel.value
                                                                .lokerState[i]
                                                        ? dark_button
                                                        : Colors.white,
                                                    fontSize: 20)),
                                            subtitle: Text(
                                                "${dataAll["data"][i]["start"]} - ${dataAll["data"][i]["end"]}",
                                                style: GoogleFonts.poppins(
                                                    color: isHovered ||
                                                            listLokerModel.value
                                                                .lokerState[i]
                                                        ? dark_button
                                                        : Colors.white,
                                                    fontSize: 16)),
                                            trailing: Icon(
                                              Icons.keyboard_arrow_down_sharp,
                                              color: isHovered ||
                                                      listLokerModel
                                                          .value.lokerState[i]
                                                  ? dark_button
                                                  : Colors.white,
                                            ))

Here's simple solution to it.这是它的简单解决方案。

Inside ExpansionTile, children add FutureBuilder, refer below code, you will get an idea.在 ExpansionTile 里面,children 添加 FutureBuilder,参考下面的代码,你就会明白了。

class MyWidget extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  return ExpansionTile(title:Text('hello'),
   children:[
    FutureBuilder(
     builder: (ctx, snapshot) {

      if (snapshot.connectionState == ConnectionState.done) {
      // If we got an error
      if (snapshot.hasError) {
        return Center(
          child: Text(
            '${snapshot.error} occured',
            style: TextStyle(fontSize: 18),
          ),
        );

        // if we got our data
      } else if (snapshot.hasData) {
        // Extracting data from snapshot object
        final data = snapshot.data as String;
        return Center(
          child: Text(
            '$data',
            style: TextStyle(fontSize: 18),
          ),
        );
      }
    }

    // Displaying LoadingSpinner to indicate waiting state
    return Center(
      child: CircularProgressIndicator(),
    );
  },

  // Future that needs to be resolved
  // inorder to display something on the Canvas
  future: getData(),
  ),
]);
}
 Future<String> getData() {
  return Future.delayed(Duration(seconds: 2), () {
   return "I am data";
     // throw Exception("Custom Error");
 });
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM