0

我有一个屏幕,其中包含ExpansionTile并且我们知道它ExpansionTile具有children属性。我想做的事情是我需要捕获 的触发器clickExpansionTile所以当用户单击ExpansionTile它时,它将运行 FutureBuilder 以从 API 获取数据,有没有办法做到这一点。因为直到现在……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,
                                            ))
4

1 回答 1

0

这是它的简单解决方案。

在 ExpansionTile 里面,孩子们添加 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");
 });
 }
}
于 2022-02-21T08:25:04.530 回答