0

I have below code which was working before but as the nature of the remaining code have changed, this is not working anymore.

I need pageController.page value to add background and borderColor to button in listView under GestureDetector.

issue is if I remove the comment from below code, I only get the first value which is in decimal points and is not like 1,2,3,4,5 in double. this results in no changes as I am comparing the currentPageValue to index in ListView.builder.

I can get the background & borderColor set on the GestureDetector onTap() but then I loose the current scroll position for listView which is another matter.

What am I doing wrong ?

Listener code :

void initState() {
    setUp();
    pageController
      ..addListener(() {
        // setState(() {
        currentPageValue = pageController.page;
        // });
        print(currentPageValue);
      });
    super.initState();
  }

output:

flutter: 0.37981
flutter: 0.734
flutter: 0.9924299999999998
flutter: 1.0

flutter: 1.166665
flutter: 1.3333300000000001
flutter: 1.4166599999999998
flutter: 1.49999
flutter: 1.5833300000000001
flutter: 1.6666600000000003
flutter: 1.75
flutter: 1.91671
flutter: 1.999995
flutter: 2.0

flutter: 1.91679
flutter: 1.833465
flutter: 1.7501250000000002
flutter: 1.66679
flutter: 1.583455
flutter: 1.50013
flutter: 1.333445
flutter: 1.250125
flutter: 1.083455
flutter: 1.0

flutter: 1.16666
flutter: 1.33332
flutter: 1.49999
flutter: 1.6666600000000003
flutter: 1.8333199999999998
flutter: 2.0

flutter: 2.3333299999999997
flutter: 2.8983
flutter: 3.0
princeoo7
  • 1,071
  • 3
  • 21
  • 44
  • Do you want only int value of current page? – Nikhil Vadoliya Aug 30 '20 at 05:50
  • @NikhilVadoliya pageController.page gives the value in `double` and the issue is I get this value but if I use setState then the first value from the `transition iteration` is only return as you can see above. Major I have notice is that this is the issue since I shifted to `FutureBuilder` in the code. previously I was calling the api at `init`. again shifted to it but then I am still stuck with the placeholder Issue which in itself is a different matter. – princeoo7 Aug 30 '20 at 08:38
  • Can you share your UI which you want? – Nikhil Vadoliya Aug 30 '20 at 09:10
  • ofcourse not @NikhilVadoliya ! also I have already mentioned `background and BorderColor` is what I am changing. – princeoo7 Aug 30 '20 at 13:47

1 Answers1

0

I'm manually checking if that value is int or not.

 _pageController = PageController()
      ..addListener(() {
        if (_pageController.page != null && _pageController.page!.isInt) {
          setState(() {
            currentPage = _pageController.page!.round();
          });
          print(currentPage);
        }
      });

And for isInt

extension NumExtensions on num {
  bool get isInt => (this % 1) == 0;
}
mirkancal
  • 4,762
  • 7
  • 37
  • 75