Skip to content

Commit eae8094

Browse files
committed
Merge branch 'master' of github.com-nixrajput:nixrajput/flutter_carousel_widget
2 parents 72e1874 + 5bf6518 commit eae8094

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

README.md

Lines changed: 40 additions & 0 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)
@@ -241,6 +242,45 @@ 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+
final step = 100 / (itemCount - 1);
269+
final percentage = step * (pageDelta + currentPage);
270+
return Center(
271+
child: Text(
272+
'${percentage.toStringAsFixed(decimalPlaces)}%',
273+
style: style ??
274+
const TextStyle(
275+
fontSize: 24,
276+
fontWeight: FontWeight.w600,
277+
),
278+
),
279+
);
280+
}
281+
}
282+
```
283+
244284
## Contributing
245285

246286
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.

lib/flutter_carousel_widget.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export 'package:flutter_carousel_widget/src/indicators/circular_slide_indicator.
99
export 'package:flutter_carousel_widget/src/indicators/circular_static_indicator.dart';
1010
export 'package:flutter_carousel_widget/src/indicators/circular_wave_slide_indicator.dart';
1111
export 'package:flutter_carousel_widget/src/indicators/sequential_fill_indicator.dart';
12+
export 'package:flutter_carousel_widget/src/indicators/slide_indicator.dart';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import 'package:flutter/material.dart';
22

3+
/// Abstraction used as a contract for building a slide indicator widget.
4+
///
5+
/// See also:
6+
///
7+
/// * [CircularSlideIndicator]
8+
/// * [CircularStaticIndicator]
9+
/// * [CircularWaveSlideIndicator]
10+
/// * [SequentialFillIndicator]
311
abstract class SlideIndicator {
12+
/// Builder method returning the slide indicator widget.
13+
///
14+
/// The method accepts the [currentPage] on which the carousel currently
15+
/// resides, the [pageDelta] or the difference between the current page and
16+
/// its resting position and [itemCount] which is the total number of items.
417
Widget build(int currentPage, double pageDelta, int itemCount);
518
}

0 commit comments

Comments
 (0)