Skip to content

Commit 1edc580

Browse files
committed
Improvement: Simplified slide indicator customization with new SlideIndicatorOptions class
Refactored all slide indicator properties into a new SlideIndicatorOptions class. This centralizes configuration, improving code readability, maintainability, and future extensibility.
1 parent 6923792 commit 1edc580

File tree

6 files changed

+204
-175
lines changed

6 files changed

+204
-175
lines changed

lib/flutter_carousel_widget.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export 'package:flutter_carousel_widget/src/helpers/flutter_expandable_carousel_
1111
export 'package:flutter_carousel_widget/src/indicators/circular_slide_indicator.dart';
1212
export 'package:flutter_carousel_widget/src/indicators/circular_static_indicator.dart';
1313
export 'package:flutter_carousel_widget/src/indicators/circular_wave_slide_indicator.dart';
14+
export 'package:flutter_carousel_widget/src/indicators/models/slide_indicator_options_model.dart';
1415
export 'package:flutter_carousel_widget/src/indicators/sequential_fill_indicator.dart';
1516
export 'package:flutter_carousel_widget/src/indicators/slide_indicator.dart';

lib/src/indicators/circular_slide_indicator.dart

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,41 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_carousel_widget/src/indicators/models/slide_indicator_options_model.dart';
23

34
import 'slide_indicator.dart';
45

56
class CircularSlideIndicator implements SlideIndicator {
67
const CircularSlideIndicator(
7-
{this.itemSpacing = 20,
8-
this.indicatorRadius = 6,
9-
this.indicatorBorderWidth = 1,
10-
this.indicatorBorderColor,
11-
this.padding,
12-
this.alignment = Alignment.bottomCenter,
13-
this.currentIndicatorColor,
14-
this.indicatorBackgroundColor,
15-
this.haloDecoration,
16-
this.haloPadding});
8+
{this.slideIndicatorOptions = const SlideIndicatorOptions()});
179

18-
final AlignmentGeometry alignment;
19-
final Color? currentIndicatorColor;
20-
final Color? indicatorBackgroundColor;
21-
final Color? indicatorBorderColor;
22-
final double indicatorBorderWidth;
23-
final double indicatorRadius;
24-
final double itemSpacing;
25-
final EdgeInsets? padding;
26-
final BoxDecoration? haloDecoration;
27-
final EdgeInsets? haloPadding;
10+
final SlideIndicatorOptions slideIndicatorOptions;
2811

2912
@override
3013
Widget build(int currentPage, double pageDelta, int itemCount) {
31-
var activeColor = const Color(0xFFFFFFFF);
32-
var backgroundColor = const Color(0x66FFFFFF);
33-
34-
// if (SchedulerBinding.instance.window.platformBrightness ==
35-
// Brightness.light) {
36-
// activeColor = const Color(0xFF000000);
37-
// backgroundColor = const Color.fromARGB(255, 163, 159, 159);
38-
// }
39-
4014
return Container(
41-
alignment: alignment,
42-
padding: padding,
15+
alignment: slideIndicatorOptions.alignment,
16+
padding: slideIndicatorOptions.padding,
4317
child: Container(
44-
decoration: haloDecoration,
45-
padding: haloPadding,
18+
decoration: slideIndicatorOptions.enableHalo
19+
? slideIndicatorOptions.haloDecoration
20+
: null,
21+
padding: slideIndicatorOptions.enableHalo
22+
? slideIndicatorOptions.haloPadding
23+
: null,
4624
child: SizedBox(
47-
width: itemCount * itemSpacing,
48-
height: indicatorRadius * 2,
25+
width: itemCount * slideIndicatorOptions.itemSpacing,
26+
height: slideIndicatorOptions.indicatorRadius * 2,
4927
child: CustomPaint(
5028
painter: CircularIndicatorPainter(
51-
currentIndicatorColor: currentIndicatorColor ?? activeColor,
29+
currentIndicatorColor:
30+
slideIndicatorOptions.currentIndicatorColor,
5231
indicatorBackgroundColor:
53-
indicatorBackgroundColor ?? backgroundColor,
32+
slideIndicatorOptions.indicatorBackgroundColor,
5433
currentPage: currentPage,
5534
pageDelta: pageDelta,
5635
itemCount: itemCount,
57-
radius: indicatorRadius,
58-
indicatorBorderColor: indicatorBorderColor,
59-
borderWidth: indicatorBorderWidth,
36+
radius: slideIndicatorOptions.indicatorRadius,
37+
indicatorBorderColor: slideIndicatorOptions.indicatorBorderColor,
38+
borderWidth: slideIndicatorOptions.indicatorBorderWidth,
6039
),
6140
),
6241
),

lib/src/indicators/circular_static_indicator.dart

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,37 @@
11
import 'package:flutter/material.dart';
2-
3-
import 'slide_indicator.dart';
2+
import 'package:flutter_carousel_widget/flutter_carousel_widget.dart';
43

54
class CircularStaticIndicator extends SlideIndicator {
6-
CircularStaticIndicator(
7-
{this.itemSpacing = 20,
8-
this.indicatorRadius = 6,
9-
this.padding,
10-
this.alignment = Alignment.bottomCenter,
11-
this.currentIndicatorColor,
12-
this.indicatorBackgroundColor,
13-
this.enableAnimation = false,
14-
this.indicatorBorderWidth = 1,
15-
this.indicatorBorderColor,
16-
this.haloDecoration,
17-
this.haloPadding});
5+
CircularStaticIndicator({
6+
this.slideIndicatorOptions = const SlideIndicatorOptions(),
7+
});
188

19-
final AlignmentGeometry alignment;
20-
final Color? currentIndicatorColor;
21-
final bool enableAnimation;
22-
final Color? indicatorBackgroundColor;
23-
final Color? indicatorBorderColor;
24-
final double indicatorBorderWidth;
25-
final double indicatorRadius;
26-
final double itemSpacing;
27-
final EdgeInsets? padding;
28-
final BoxDecoration? haloDecoration;
29-
final EdgeInsets? haloPadding;
9+
final SlideIndicatorOptions slideIndicatorOptions;
3010

3111
@override
3212
Widget build(int currentPage, double pageDelta, int itemCount) {
33-
var activeColor = const Color(0xFFFFFFFF);
34-
var backgroundColor = const Color(0x66FFFFFF);
35-
36-
// if (SchedulerBinding.instance.window.platformBrightness ==
37-
// Brightness.light) {
38-
// activeColor = const Color(0xFF000000);
39-
// backgroundColor = const Color(0xFF878484);
40-
// }
41-
4213
return Container(
43-
alignment: alignment,
44-
padding: padding,
14+
alignment: slideIndicatorOptions.alignment,
15+
padding: slideIndicatorOptions.padding,
4516
child: Container(
46-
decoration: haloDecoration,
47-
padding: haloPadding,
17+
decoration: slideIndicatorOptions.haloDecoration,
18+
padding: slideIndicatorOptions.haloPadding,
4819
child: SizedBox(
49-
width: itemCount * itemSpacing,
50-
height: indicatorRadius * 2,
20+
width: itemCount * slideIndicatorOptions.itemSpacing,
21+
height: slideIndicatorOptions.indicatorRadius * 2,
5122
child: CustomPaint(
5223
painter: CircularStaticIndicatorPainter(
53-
currentIndicatorColor: currentIndicatorColor ?? activeColor,
24+
currentIndicatorColor:
25+
slideIndicatorOptions.currentIndicatorColor,
5426
indicatorBackgroundColor:
55-
indicatorBackgroundColor ?? backgroundColor,
27+
slideIndicatorOptions.indicatorBackgroundColor,
5628
currentPage: currentPage,
5729
pageDelta: pageDelta,
5830
itemCount: itemCount,
59-
radius: indicatorRadius,
60-
enableAnimation: enableAnimation,
61-
indicatorBorderColor: indicatorBorderColor,
62-
borderWidth: indicatorBorderWidth,
31+
radius: slideIndicatorOptions.indicatorRadius,
32+
enableAnimation: slideIndicatorOptions.enableAnimation,
33+
indicatorBorderColor: slideIndicatorOptions.indicatorBorderColor,
34+
borderWidth: slideIndicatorOptions.indicatorBorderWidth,
6335
),
6436
),
6537
),

lib/src/indicators/circular_wave_slide_indicator.dart

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,40 @@
11
import 'package:flutter/material.dart';
2-
3-
import 'slide_indicator.dart';
2+
import 'package:flutter_carousel_widget/flutter_carousel_widget.dart';
43

54
class CircularWaveSlideIndicator implements SlideIndicator {
6-
CircularWaveSlideIndicator(
7-
{this.itemSpacing = 20,
8-
this.indicatorRadius = 6,
9-
this.padding,
10-
this.alignment = Alignment.bottomCenter,
11-
this.currentIndicatorColor,
12-
this.indicatorBackgroundColor,
13-
this.indicatorBorderWidth = 1,
14-
this.indicatorBorderColor,
15-
this.haloDecoration,
16-
this.haloPadding});
5+
CircularWaveSlideIndicator({
6+
this.slideIndicatorOptions = const SlideIndicatorOptions(),
7+
});
178

18-
final AlignmentGeometry alignment;
19-
final Color? currentIndicatorColor;
20-
final Color? indicatorBackgroundColor;
21-
final Color? indicatorBorderColor;
22-
final double indicatorBorderWidth;
23-
final double indicatorRadius;
24-
final double itemSpacing;
25-
final EdgeInsets? padding;
26-
final BoxDecoration? haloDecoration;
27-
final EdgeInsets? haloPadding;
9+
final SlideIndicatorOptions slideIndicatorOptions;
2810

2911
@override
3012
Widget build(int currentPage, double pageDelta, int itemCount) {
31-
var activeColor = const Color(0xFFFFFFFF);
32-
var backgroundColor = const Color(0x66FFFFFF);
33-
34-
// if (SchedulerBinding.instance.window.platformBrightness ==
35-
// Brightness.light) {
36-
// activeColor = const Color(0xFF000000);
37-
// backgroundColor = const Color(0xFF878484);
38-
// }
39-
4013
return Container(
41-
alignment: alignment,
42-
padding: padding,
14+
alignment: slideIndicatorOptions.alignment,
15+
padding: slideIndicatorOptions.padding,
4316
child: Container(
44-
decoration: haloDecoration,
45-
padding: haloPadding,
17+
decoration: slideIndicatorOptions.enableHalo
18+
? slideIndicatorOptions.haloDecoration
19+
: null,
20+
padding: slideIndicatorOptions.enableHalo
21+
? slideIndicatorOptions.haloPadding
22+
: null,
4623
child: SizedBox(
47-
width: itemCount * itemSpacing,
48-
height: indicatorRadius * 2,
24+
width: itemCount * slideIndicatorOptions.itemSpacing,
25+
height: slideIndicatorOptions.indicatorRadius * 2,
4926
child: CustomPaint(
5027
painter: CircularWaveIndicatorPainter(
51-
currentIndicatorColor: currentIndicatorColor ?? activeColor,
28+
currentIndicatorColor:
29+
slideIndicatorOptions.currentIndicatorColor,
5230
indicatorBackgroundColor:
53-
indicatorBackgroundColor ?? backgroundColor,
31+
slideIndicatorOptions.indicatorBackgroundColor,
5432
currentPage: currentPage,
5533
pageDelta: pageDelta,
5634
itemCount: itemCount,
57-
radius: indicatorRadius,
58-
indicatorBorderColor: indicatorBorderColor,
59-
borderWidth: indicatorBorderWidth,
35+
radius: slideIndicatorOptions.indicatorRadius,
36+
indicatorBorderColor: slideIndicatorOptions.indicatorBorderColor,
37+
borderWidth: slideIndicatorOptions.indicatorBorderWidth,
6038
),
6139
),
6240
),
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/material.dart';
3+
4+
/// A class that holds the options for the slide indicators.
5+
class SlideIndicatorOptions {
6+
const SlideIndicatorOptions({
7+
this.alignment = Alignment.bottomCenter,
8+
this.currentIndicatorColor = const Color(0xFFFFFFFF),
9+
this.indicatorBackgroundColor = const Color(0x66FFFFFF),
10+
this.indicatorBorderColor,
11+
this.indicatorBorderWidth = 1,
12+
this.indicatorRadius = 6,
13+
this.itemSpacing = 20,
14+
this.padding,
15+
this.haloDecoration = const BoxDecoration(
16+
color: Color(0x33000000),
17+
borderRadius: BorderRadius.all(Radius.circular(15.0)),
18+
),
19+
this.haloPadding = const EdgeInsets.all(8.0),
20+
this.enableHalo = false,
21+
this.enableAnimation = false,
22+
}) : assert(indicatorRadius > 0),
23+
assert(itemSpacing >= 0),
24+
assert(indicatorBorderWidth >= 0);
25+
26+
/// The alignment of the indicator.
27+
final AlignmentGeometry alignment;
28+
29+
/// The color of the currently active item indicator.
30+
final Color currentIndicatorColor;
31+
32+
/// The background color of all inactive item indicators.
33+
final Color indicatorBackgroundColor;
34+
35+
/// The border color of all item indicators.
36+
final Color? indicatorBorderColor;
37+
38+
/// The border width of all item indicators.
39+
final double indicatorBorderWidth;
40+
41+
/// The radius of all item indicators.
42+
final double indicatorRadius;
43+
44+
/// The spacing between each item indicator.
45+
final double itemSpacing;
46+
47+
/// The padding of the indicator.
48+
final EdgeInsets? padding;
49+
50+
/// The decoration of the indicator halo.
51+
final BoxDecoration haloDecoration;
52+
53+
/// The padding of the indicator halo.
54+
final EdgeInsets haloPadding;
55+
56+
/// Whether to enable the indicator halo.
57+
final bool enableHalo;
58+
59+
/// Whether to enable the animation. Only used in [CircularStaticIndicator] and [andSequentialFillIndicator].
60+
final bool enableAnimation;
61+
62+
/// Returns a copy of this [SlideIndicatorOptions] but with the given fields replaced with the new values.
63+
SlideIndicatorOptions copyWith({
64+
AlignmentGeometry? alignment,
65+
Color? currentIndicatorColor,
66+
Color? indicatorBackgroundColor,
67+
Color? indicatorBorderColor,
68+
double? indicatorBorderWidth,
69+
double? indicatorRadius,
70+
double? itemSpacing,
71+
EdgeInsets? padding,
72+
BoxDecoration? haloDecoration,
73+
EdgeInsets? haloPadding,
74+
bool? enableHalo,
75+
}) {
76+
return SlideIndicatorOptions(
77+
alignment: alignment ?? this.alignment,
78+
currentIndicatorColor:
79+
currentIndicatorColor ?? this.currentIndicatorColor,
80+
indicatorBackgroundColor:
81+
indicatorBackgroundColor ?? this.indicatorBackgroundColor,
82+
indicatorBorderColor: indicatorBorderColor ?? this.indicatorBorderColor,
83+
indicatorBorderWidth: indicatorBorderWidth ?? this.indicatorBorderWidth,
84+
indicatorRadius: indicatorRadius ?? this.indicatorRadius,
85+
itemSpacing: itemSpacing ?? this.itemSpacing,
86+
padding: padding ?? this.padding,
87+
haloDecoration: haloDecoration ?? this.haloDecoration,
88+
haloPadding: haloPadding ?? this.haloPadding,
89+
enableHalo: enableHalo ?? this.enableHalo,
90+
);
91+
}
92+
93+
@override
94+
bool operator ==(Object other) {
95+
if (identical(this, other)) return true;
96+
97+
return other is SlideIndicatorOptions &&
98+
other.alignment == alignment &&
99+
other.currentIndicatorColor == currentIndicatorColor &&
100+
other.indicatorBackgroundColor == indicatorBackgroundColor &&
101+
other.indicatorBorderColor == indicatorBorderColor &&
102+
other.indicatorBorderWidth == indicatorBorderWidth &&
103+
other.indicatorRadius == indicatorRadius &&
104+
other.itemSpacing == itemSpacing &&
105+
other.padding == padding &&
106+
other.haloDecoration == haloDecoration &&
107+
other.haloPadding == haloPadding &&
108+
other.enableHalo == enableHalo;
109+
}
110+
111+
@override
112+
int get hashCode {
113+
return Object.hash(
114+
alignment,
115+
currentIndicatorColor,
116+
indicatorBackgroundColor,
117+
indicatorBorderColor,
118+
indicatorBorderWidth,
119+
indicatorRadius,
120+
itemSpacing,
121+
padding,
122+
haloDecoration,
123+
haloPadding,
124+
enableHalo,
125+
);
126+
}
127+
}

0 commit comments

Comments
 (0)