Home > Mobile >  Flutter swiper. Go to the first index
Flutter swiper. Go to the first index

Time:08-10

I need a Function to make the swiper go to the first index I have

I am using card_swiperpackage

Swiper(
  loop: true,
  itemCount: 2,
  itemBuilder: (context, index) {
    return Image.asset(index == 0
        ? blackImages[imageNumber].imagePath
        : blackImages[imageNumber].imagePath1);
  },
),

am just using setState for state management

CodePudding user response:

Use a SwiperController

 final controller = SwiperController();
.....
 Swiper(
        controller: controller,

and to move 1st index use , no need to call setState

 controller.move(0);

Test snippet

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.move(0);
        },
      ),
      body: Column(
        children: [
          Swiper(
            controller: controller,
            loop: true,
            itemCount: 2,
            itemBuilder: (context, index) {
              return Container(
                color: index.isEven ? Colors.red : Colors.blue,
              );
            },
          ),
        ],
      ),
    );
  • Related