0

I want a pageValue from main.dart to set value in carousel.dart and vice versa?

main.dart

double pageValue = 0;

class HomePage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Column(
          children: <Widget>[
            ...
            Text("Page Value: $pageValue"),

          ],
        ),
      );
    });
   }
  }

carousel_images.dart

class CarouselImages extends StatefulWidget {
...
...
   @override
   _CarouselImagesState createState() => _CarouselImagesState();
}


class _CarouselImagesState extends State<CarouselImages> {
   PageController _pageController;
   double _currentPageValue = 0.0;


   @override
   void initState() {
     super.initState();
     _pageController = PageController(viewportFraction: 0.9);
     _pageController.addListener(() {
     setState(() {
       _currentPageValue = _pageController.page;
       print("VALUE OF PAGE : $_currentPageValue");
     });
    });
   }

   @override
   void dispose() {
    super.dispose();
    _pageController.dispose();
  }
...
...
}

I want to get a _currentPageValue from carousel.dart to in main.dart, and also want pageValue from main.dart to set value of _currentPageValue in carousel.dart?

Aakash Solanki
  • 565
  • 1
  • 6
  • 18

1 Answers1

1

You can get a bloc above them in the widget tree, have that int in the state, send event with given int on change, and use a blocbuilder to automatically update it on state change