How to disable swiper button dynamically in flutter_swiper?

1.5k views Asked by At

I am using flutter_swiper to swipe images. I have two swipers as you can see i want to disable those two swiper arrows dynamically how can i do that?

enter image description here

I cant disable from start as i want to disable it when some condition is satisfied

here is some sample code:-

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


  var eImage = [
    "img/eyes/1.png",
    "img/eyes/2.png",
    "img/eyes/3.png",
  ];

  double height = 200;
  int itemNo;
  double eh = 200;
  double ew = 200;
  double nh = 100;
  double nw = 300;
  double lh = 100;
  double lw = 100;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage("img/wal.jpg"),
              fit: BoxFit.fill,
            ),
          ),
          child: Container(
            child: Stack(

                            children: <Widget>[
                              Align(
                                alignment: Alignment(0, -0.6),
                                child: Container(
                                  width: ew,
                                  height: eh,
                                  //color: Colors.purple,
                                  child: new SizedBox(
                                    child: Swiper(
                                      itemBuilder:
                                          (BuildContext context, int index) {
                                        return Image.asset(
                                          eImage[index],
                                        );
                                      },
                                      itemCount: eImage.length,
                                      itemWidth: 200,
                                      itemHeight: 200,
                                      control: new SwiperControl(),
                                      layout: SwiperLayout.DEFAULT,
                                      customLayoutOption: CustomLayoutOption(
                                          startIndex: 1, stateCount: 3) ///<--- here i am trying to start from 1st index
                                          .addRotate([
                                        0 / 180,
                                        0.0,
                                        0 / 180
                                      ]).addTranslate([
                                        Offset(-400.0, 0.0),
                                        Offset(0.0, 0.0),
                                        Offset(370, -40.0),
                                      ]),
                                    ),
                                    height: 200,
                                    width: 350,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ))));
                      }
  }

Now i want to disable those arrows with help of a function which will be triggered at particular event

1

There are 1 answers

0
KUNAL HIRANI On BEST ANSWER

I have found solution by disabling iconPrevious: and iconNext: at run time with help of a boolean flag. If flag is false then enable buttons and disable if flag is true.

Example:

var disable=false;

Here this is flag which will allow swiper button to get displayed if the value is false

Now, set flag in controller

control: new SwiperControl(
                                 iconPrevious: disable?null:Icons.arrow_back_ios,
                                 iconNext:disable?null:Icons.arrow_forward_ios,
                               ),

Change the flag using set state in button.

 next(){
    setState(() {
      disable=true;
    });
}