Flutter:setState无法在页面上运行

时间:2020-09-27 11:54:20

标签: flutter flutter-dependencies

我使用逐页依赖性来显示来自不同页面的数据,以及用于过滤项目的小部件。我想通过selectedChoise变量的值加载逐页数据,但是当我通过setState为selectedChoise逐页设置新值时,不会更改。 这是我的代码:

class PostsTab extends StatefulWidget {
  const PostsTab({
    Key key,
  }) : super(key: key);

  @override
  _PostsTabState createState() => _PostsTabState();
}

class _PostsTabState extends State<PostsTab> {
  int selectedChoice = 0;

  @override
  Widget build(BuildContext context) {
    final delegate = S.of(context);
    return Expanded(
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(5),
            child: AlisPupopMenuButton(
              height: 40.0,
              options: [
                delegate.Filter,
                "امتحانات آینده",
                "امتحانات جاری",
              ],
              selectedIndex: selectedChoice,
              onSelect: (i, v) {
                setState(() {
                  selectedChoice = i;
                });
                print("this is selected");
              },
              // width: MediaQuery.of(context).size.width / 2.5,
            ),
          ),
          Expanded(
            child: PagewiseListView(
                pageSize: 5,
                itemBuilder: (_, exam, i) {
                  return ExamItem(exam);
                },
                pageFuture: (pageIndex) {
                  if (selectedChoice == 0) {
                    return ExamServices().getExam(pageIndex + 1);
                  }
                  if (selectedChoice == 1) {
                    return ExamServices().getFutureExamPost(pageIndex + 1);
                  }
                  if (selectedChoice == 2) {
                    return ExamServices().getCurentExamPost(pageIndex + 1);
                  }
                  return null;
                }),
          )
        ],
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

我通过PagewiseloadController解决这个问题,这是代码:

  Expanded(
            child: PagewiseListView(
              // pageSize: 5,
              itemBuilder: (_, exam, i) {
                return ExamItem(exam);
              },
              pageLoadController: PagewiseLoadController(
                pageFuture: (pageIndex) {
                  if (selectedChoice == 0) {
                    return ExamServices().getExam(pageIndex + 1);
                  }
                  if (selectedChoice == 1) {
                    return ExamServices().getFutureExamPost(pageIndex + 1);
                  }
                  if (selectedChoice == 2) {
                    return ExamServices().getCurentExamPost(pageIndex + 1);
                  }
                  return null;
                },
                pageSize: 5,
              ),
            ),
          ),