Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I change the axis programmatically in flutter?

I have 2 screens. Screen A is the settings screen where I change the axis (horizontal/vertical). Change the axis in the carousel slider, which is on screen B.
I wrote a method that handles the toggle button, but I don’t understand how I can get the necessary changes on screen B.

Screen A:

class _ChooseSettingsScreenState extends State<ChooseSettingsScreen> {

  bool swipeTrue = true;//vertical

  @override
  Widget build(BuildContext context) {

...

   GFToggle(
    onChanged: (swipeTrue){
      mySwipeHandler(context);
    },
    value: false,
    type: GFToggleType.ios,
    enabledTrackColor: Colors.black26,
    disabledTrackColor: Colors.white70,
    enabledThumbColor: Colors.blueAccent,
    disabledThumbColor: Colors.blueAccent,
  )

...
  Axis mySwipeHandler (BuildContext context) {
    if (swipeTrue == true){
      setState(() {
        swipeTrue = false;
      });
      print("chose vertical");
      return Axis.vertical;
    } else  {
      setState(() {
        swipeTrue = true;
      });
      print("chose horizontal");
      return Axis.horizontal;
    }
  }
...

Screen B:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

child: CarouselSlider.builder(
         itemCount: quoteList.length,//Changed
         options: CarouselOptions(
         viewportFraction: 1.0,
         pageSnapping: true,
         reverse: false,
         initialPage: 0,
         scrollDirection: Axis.horizontal,
         onPageChanged: (index, value){
           HapticFeedback.lightImpact();
         setState((){});
         }
        ),

I need to change Axis.horizontal to Axis.vertical and back via toggle button (Screen A)
Help me please friends!

>Solution :

You can make condition like this:

Define swipeTrue variable global:

child: CarouselSlider.builder(
     itemCount: quoteList.length,//Changed
     options: CarouselOptions(
     viewportFraction: 1.0,
     pageSnapping: true,
     reverse: false,
     initialPage: 0,
     scrollDirection: swipeTrue==true?Axis.vertical : Axis.horizontal,
     onPageChanged: (index, value){
       HapticFeedback.lightImpact();
     setState((){});
     }
    ),
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading