Create a project
To get started, create a new Flutter project and add the necessary dependency. We will be using the following dependency:
- flutter_swiper : The best swiper for flutter , with multiple layouts, infinite loop. Compatible with Android & iOS.
Flutter Swiper is a package for creating beautiful, responsive, and customizable swipeable carousels in your Flutter applications. Whether you’re building an e-commerce app, a news app, or just a simple image gallery, Flutter Swiper provides a powerful and flexible way to display your content in a swipeable carousel.
In this article, we’ll take a closer look at the features of Flutter Swiper and how to use it to build swipeable carousels in your Flutter app.
Add flutter_swiper to your Flutter App
Add the flutter_swiper
dependency to your Flutter app’s pubspec.yaml
file :
dependencies:
flutter_swiper: ^1.1.6
Then, run flutter pub get
to install the dependencies.
Initialize flutter_swiper in your Flutter App
Once you’ve added the package, you can import it in your Dart code, to create a basic swipeable carousel, you can use the Swiper widget.
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Swiper(
itemCount: 3,
itemBuilder: (BuildContext context, int index) {
return Image.network(
'https://loremflickr.com/640/360',
fit: BoxFit.cover,
);
},
));
}
}
In this example, we’re using the Swiper widget to create a carousel with three items. The itemBuilder function is called for each item in the carousel and returns an Image widget with a different image URL based on the index of the item.
You can customize the style of your carousel by passing in properties to the Swiper widget :
Swiper(
itemCount: 3,
itemWidth: 300,
itemHeight: 200,
layout: SwiperLayout.STACK,
itemBuilder: (BuildContext context, int index) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
image: NetworkImage(
'https://picsum.photos/id/$index/400/300',
),
fit: BoxFit.cover,
),
),
child: Center(
child: Text(
'Item $index',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
);
},
)
In this example, we’re setting the itemWidth and itemHeight properties to specify the size of each item in the carousel. We’re also using the layout property to specify the layout of the carousel, and the decoration property to customize the appearance of each item in the carousel. Finally, we’re using a Text widget to display the index of each item in the center of the item.
Conclusion
In conclusion, Flutter Swiper is a powerful and flexible package that allows you to easily create beautiful and responsive swipeable carousels in your Flutter applications. With its customizable styles, navigation controls, support for multiple items, and dynamic data support, you can create carousels that meet the needs of your application.
Whether you’re building a news app, an e-commerce app, or just a simple image gallery, Flutter Swiper provides a simple and easy-to-use solution for displaying your content in a swipeable carousel. With its growing community and active development, Flutter Swiper is a great package to add to your Flutter toolkit.
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.