Home > Mobile >  How do I change the axis programmatically in flutter?
How do I change the axis programmatically in flutter?

Time:01-23

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:

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!

CodePudding user response:

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((){});
     }
    ),
  • Related