Skip to content

Commit 2ecea4a

Browse files
committed
Document slide indicators in the readme
1 parent ec33337 commit 2ecea4a

File tree

1 file changed

+135
-93
lines changed

1 file changed

+135
-93
lines changed

README.md

Lines changed: 135 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ A customizable carousel slider widget in Flutter which supports infinite scrolli
3333
- [`.previousPage({Duration duration, Curve curve})`](#previouspageduration-duration-curve-curve)
3434
- [`.jumpToPage(int page)`](#jumptopageint-page)
3535
- [`.animateToPage(int page, {Duration duration, Curve curve})`](#animatetopageint-page-duration-duration-curve-curve)
36+
- [Slide indicators](#slide-indicators)
3637
- [Contributing](#contributing)
3738
- [License](#license)
3839
- [Sponsor Me](#sponsor-me)
@@ -68,7 +69,7 @@ Add `flutter_carousel_widget` as a dependency in your `pubspec.yaml` file:
6869

6970
```dart
7071
dependencies:
71-
flutter_carousel_widget: ^latest_version
72+
flutter_carousel_widget: ^latest_version
7273
```
7374

7475
And import it:
@@ -85,25 +86,25 @@ Flutter Carousel is a carousel widget which supports infinite scrolling, auto sc
8586

8687
```dart
8788
FlutterCarousel(
88-
options: CarouselOptions(
89-
height: 400.0,
90-
showIndicator: true,
91-
slideIndicator: CircularSlideIndicator(),
92-
),
93-
items: [1,2,3,4,5].map((i) {
94-
return Builder(
95-
builder: (BuildContext context) {
96-
return Container(
97-
width: MediaQuery.of(context).size.width,
98-
margin: EdgeInsets.symmetric(horizontal: 5.0),
99-
decoration: BoxDecoration(
100-
color: Colors.amber
101-
),
102-
child: Text('text $i', style: TextStyle(fontSize: 16.0),)
103-
);
104-
},
105-
);
106-
}).toList(),
89+
options: CarouselOptions(
90+
height: 400.0,
91+
showIndicator: true,
92+
slideIndicator: CircularSlideIndicator(),
93+
),
94+
items: [1,2,3,4,5].map((i) {
95+
return Builder(
96+
builder: (BuildContext context) {
97+
return Container(
98+
width: MediaQuery.of(context).size.width,
99+
margin: EdgeInsets.symmetric(horizontal: 5.0),
100+
decoration: BoxDecoration(
101+
color: Colors.amber
102+
),
103+
child: Text('text $i', style: TextStyle(fontSize: 16.0),)
104+
);
105+
},
106+
);
107+
}).toList(),
107108
)
108109
```
109110

@@ -113,57 +114,57 @@ Expandable Carousel is a carousel widget which automatically expands to the size
113114

114115
```dart
115116
ExpandableCarousel(
116-
options: CarouselOptions(
117-
autoPlay: true,
118-
autoPlayInterval: const Duration(seconds: 2),
119-
),
120-
items: [1,2,3,4,5].map((i) {
121-
return Builder(
122-
builder: (BuildContext context) {
123-
return Container(
124-
width: MediaQuery.of(context).size.width,
125-
margin: EdgeInsets.symmetric(horizontal: 5.0),
126-
decoration: BoxDecoration(
127-
color: Colors.amber
128-
),
129-
child: Text('text $i', style: TextStyle(fontSize: 16.0),)
130-
);
131-
},
132-
);
133-
}).toList(),
117+
options: CarouselOptions(
118+
autoPlay: true,
119+
autoPlayInterval: const Duration(seconds: 2),
120+
),
121+
items: [1,2,3,4,5].map((i) {
122+
return Builder(
123+
builder: (BuildContext context) {
124+
return Container(
125+
width: MediaQuery.of(context).size.width,
126+
margin: EdgeInsets.symmetric(horizontal: 5.0),
127+
decoration: BoxDecoration(
128+
color: Colors.amber
129+
),
130+
child: Text('text $i', style: TextStyle(fontSize: 16.0),)
131+
);
132+
},
133+
);
134+
}).toList(),
134135
)
135136
```
136137

137138
### Option Customization
138139

139140
```dart
140141
FlutterCarousel(
141-
items: items,
142-
options: CarouselOptions(
143-
height: 400.0,
144-
aspectRatio: 16 / 9,
145-
viewportFraction: 1.0,
146-
initialPage: 0,
147-
enableInfiniteScroll: true,
148-
reverse: false,
149-
autoPlay: false,
150-
autoPlayInterval: const Duration(seconds: 2),
151-
autoPlayAnimationDuration: const Duration(milliseconds: 800),
152-
autoPlayCurve: Curves.fastOutSlowIn,
153-
enlargeCenterPage: false,
154-
controller: CarouselController(),
155-
onPageChanged: callbackFunction,
156-
pageSnapping: true,
157-
scrollDirection: Axis.horizontal,
158-
pauseAutoPlayOnTouch: true,
159-
pauseAutoPlayOnManualNavigate: true,
160-
pauseAutoPlayInFiniteScroll: false,
161-
enlargeStrategy: CenterPageEnlargeStrategy.scale,
162-
disableCenter: false,
163-
showIndicator: true,
164-
floatingIndicator = true,
165-
slideIndicator: CircularSlideIndicator(),
166-
)
142+
items: items,
143+
options: CarouselOptions(
144+
height: 400.0,
145+
aspectRatio: 16 / 9,
146+
viewportFraction: 1.0,
147+
initialPage: 0,
148+
enableInfiniteScroll: true,
149+
reverse: false,
150+
autoPlay: false,
151+
autoPlayInterval: const Duration(seconds: 2),
152+
autoPlayAnimationDuration: const Duration(milliseconds: 800),
153+
autoPlayCurve: Curves.fastOutSlowIn,
154+
enlargeCenterPage: false,
155+
controller: CarouselController(),
156+
onPageChanged: callbackFunction,
157+
pageSnapping: true,
158+
scrollDirection: Axis.horizontal,
159+
pauseAutoPlayOnTouch: true,
160+
pauseAutoPlayOnManualNavigate: true,
161+
pauseAutoPlayInFiniteScroll: false,
162+
enlargeStrategy: CenterPageEnlargeStrategy.scale,
163+
disableCenter: false,
164+
showIndicator: true,
165+
floatingIndicator = true,
166+
slideIndicator: CircularSlideIndicator(),
167+
)
167168
)
168169
```
169170

@@ -173,21 +174,21 @@ This method will save memory by building items once it becomes necessary. This w
173174

174175
```dart
175176
FlutterCarousel.builder(
176-
itemCount: 15,
177-
itemBuilder: (BuildContext context, int itemIndex, int pageViewIndex) =>
178-
Container(
179-
child: Text(itemIndex.toString()),
180-
),
177+
itemCount: 15,
178+
itemBuilder: (BuildContext context, int itemIndex, int pageViewIndex) =>
179+
Container(
180+
child: Text(itemIndex.toString()),
181+
),
181182
)
182183
```
183184

184185
```dart
185186
ExpandableCarousel.builder(
186-
itemCount: 15,
187-
itemBuilder: (BuildContext context, int itemIndex, int pageViewIndex) =>
188-
Container(
189-
child: Text(itemIndex.toString()),
190-
),
187+
itemCount: 15,
188+
itemBuilder: (BuildContext context, int itemIndex, int pageViewIndex) =>
189+
Container(
190+
child: Text(itemIndex.toString()),
191+
),
191192
)
192193
```
193194

@@ -199,26 +200,26 @@ In order to manually control the pageview's position, you can create your own `C
199200
class CarouselDemo extends StatelessWidget {
200201
CarouselController buttonCarouselController = CarouselController();
201202
202-
@override
203+
@override
203204
Widget build(BuildContext context) => Column(
204-
children: [
205-
FlutterCarousel(
206-
items: child,
207-
options: CarouselOptions(
208-
autoPlay: false,
209-
controller: buttonCarouselController,
210-
enlargeCenterPage: true,
211-
viewportFraction: 0.9,
212-
aspectRatio: 2.0,
213-
initialPage: 2,
214-
),
215-
),
216-
RaisedButton(
217-
onPressed: () => buttonCarouselController.nextPage(
218-
duration: Duration(milliseconds: 300), curve: Curves.linear),
219-
child: Text('→'),
220-
)
221-
]
205+
children: [
206+
FlutterCarousel(
207+
items: child,
208+
options: CarouselOptions(
209+
autoPlay: false,
210+
controller: buttonCarouselController,
211+
enlargeCenterPage: true,
212+
viewportFraction: 0.9,
213+
aspectRatio: 2.0,
214+
initialPage: 2,
215+
),
216+
),
217+
RaisedButton(
218+
onPressed: () => buttonCarouselController.nextPage(
219+
duration: Duration(milliseconds: 300), curve: Curves.linear),
220+
child: Text('→'),
221+
)
222+
]
222223
);
223224
}
224225
```
@@ -241,6 +242,47 @@ Jump to the given page.
241242

242243
Animate to the given page.
243244

245+
## Slide indicators
246+
247+
The `flutter_carousel_widget` package comes with a few [predefined slide indicators](https://github.com/nixrajput/flutter_carousel_widget/tree/master/lib/src/indicators) with their own unique behaviors. This helps drastically and brings focus towards the actual implementation of your carousel widget.
248+
249+
However, there might be cases where you want to control the look or behavior of the slide indicator or implement a totally new one. You can do that by implementing the `SlideIndicator` contract.
250+
251+
The following example implements an indicator which tells the percentage of the slide the user is on:
252+
```dart
253+
class SlidePercentageIndicator implements SlideIndicator {
254+
SlidePercentageIndicator({
255+
this.decimalPlaces = 0,
256+
this.style,
257+
});
258+
259+
/// The number of decimal places to show in the output
260+
final int decimalPlaces;
261+
262+
/// The text style to be used by the percentage text
263+
final TextStyle? style;
264+
265+
@override
266+
Widget build(int currentPage, double pageDelta, int itemCount) {
267+
if (itemCount < 2) return const SizedBox.shrink();
268+
269+
final step = 100 / (itemCount - 1);
270+
final percentage = step * (pageDelta + currentPage);
271+
272+
return Center(
273+
child: Text(
274+
'${percentage.toStringAsFixed(decimalPlaces)}%',
275+
style: style ??
276+
const TextStyle(
277+
fontSize: 24,
278+
fontWeight: FontWeight.w600,
279+
),
280+
),
281+
);
282+
}
283+
}
284+
```
285+
244286
## Contributing
245287

246288
If you would like to contribute to this project, feel free to fork the repository, make your changes, and submit a pull request. Please follow the guidelines in the [CONTRIBUTING.md](CONTRIBUTING.md) file.

0 commit comments

Comments
 (0)