Mohammad_Asef
Mohammad_Asef

Reputation: 336

Flutter: setState does not work on pagewise

I use page wise dependency to show my data from different pages and a widget for filtering items. I want to load page wise data by the value of selectedChoise variable but when I set a new value by setState for selectedChoise page wise does not changed. here is my code:

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;
                }),
          )
        ],
      ),
    );
  }
}

Upvotes: 0

Views: 121

Answers (1)

Mohammad_Asef
Mohammad_Asef

Reputation: 336

I solve that by PagewiseloadController and this is the code:

  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,
              ),
            ),
          ),

Upvotes: 0

Related Questions