Skip to content

Commit 46456cc

Browse files
Kaviyarasansf4298Kaviyarasansf4298
authored andcommitted
FLUT-897866-[others][feature]: positive and negative slider
1 parent a7388ab commit 46456cc

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

lib/main.dart

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:syncfusion_flutter_sliders/sliders.dart';
3+
import 'package:syncfusion_flutter_core/theme.dart';
4+
5+
void main() {
6+
runApp(const MyApp());
7+
}
8+
9+
class MyApp extends StatelessWidget {
10+
const MyApp({super.key});
11+
12+
@override
13+
Widget build(BuildContext context) {
14+
return const MaterialApp(
15+
debugShowCheckedModeBanner: false,
16+
home: MyHomePage(),
17+
);
18+
}
19+
}
20+
21+
class MyHomePage extends StatefulWidget {
22+
const MyHomePage({super.key});
23+
24+
@override
25+
State<MyHomePage> createState() => _MyHomePageState();
26+
}
27+
28+
class _MyHomePageState extends State<MyHomePage> {
29+
double _value = 0.0;
30+
SfSlider _buildHorizontalSlider() {
31+
return SfSlider(
32+
showTicks: true,
33+
showLabels: true,
34+
min: -10,
35+
max: 10,
36+
interval: 5,
37+
value: _value,
38+
thumbShape: _CustomThumbShape(type: SliderType.horizontal, value: _value),
39+
onChanged: (dynamic value) {
40+
setState(
41+
() {
42+
_value = value;
43+
},
44+
);
45+
},
46+
);
47+
}
48+
49+
SfSlider _buildVerticalSlider() {
50+
return SfSlider.vertical(
51+
showTicks: true,
52+
showLabels: true,
53+
min: -10,
54+
max: 10,
55+
interval: 5,
56+
value: _value,
57+
thumbShape: _CustomThumbShape(type: SliderType.vertical, value: _value),
58+
onChanged: (dynamic value) {
59+
setState(
60+
() {
61+
_value = value;
62+
},
63+
);
64+
},
65+
);
66+
}
67+
68+
@override
69+
Widget build(BuildContext context) {
70+
final SfColorScheme colorScheme = SfTheme.colorScheme(context);
71+
return Scaffold(
72+
body: Center(
73+
child: SfSliderTheme(
74+
data: SfSliderThemeData(
75+
activeTrackColor: colorScheme.primary[61],
76+
trackCornerRadius: 7.5,
77+
activeTrackHeight: 10,
78+
inactiveTrackHeight: 10,
79+
),
80+
child: _buildHorizontalSlider(),
81+
),
82+
),
83+
);
84+
}
85+
}
86+
87+
class _CustomThumbShape extends SfThumbShape {
88+
_CustomThumbShape({required this.type, required this.value});
89+
final SliderType type;
90+
final double value;
91+
92+
@override
93+
void paint(PaintingContext context, Offset center,
94+
{required RenderBox parentBox,
95+
required RenderBox? child,
96+
required SfSliderThemeData themeData,
97+
SfRangeValues? currentValues,
98+
dynamic currentValue,
99+
required Paint? paint,
100+
required Animation<double> enableAnimation,
101+
required TextDirection textDirection,
102+
required SfThumb? thumb}) {
103+
final Paint thumbPaint = Paint();
104+
final Rect? rect;
105+
final Offset trackCenter =
106+
Offset(parentBox.size.width / 2, parentBox.size.height / 2);
107+
final halfTrackHeight = themeData.activeTrackHeight / 2;
108+
final x = center.dx;
109+
final y = center.dy;
110+
if (type == SliderType.vertical) {
111+
rect = Rect.fromLTRB(
112+
x - halfTrackHeight,
113+
y,
114+
x + halfTrackHeight,
115+
parentBox.size.height / 2,
116+
);
117+
} else {
118+
rect = Rect.fromLTRB(
119+
x,
120+
y - halfTrackHeight,
121+
trackCenter.dx,
122+
y + halfTrackHeight,
123+
);
124+
}
125+
126+
thumbPaint
127+
..color =
128+
value < 0 ? const Color(0XFFC62828) : Colors.orange.withOpacity(0.850)
129+
..style = PaintingStyle.fill
130+
..strokeWidth = themeData.activeTrackHeight;
131+
132+
context.canvas.drawRect(rect, thumbPaint);
133+
super.paint(context, center,
134+
parentBox: parentBox,
135+
child: child,
136+
themeData: themeData,
137+
paint: thumbPaint,
138+
enableAnimation: enableAnimation,
139+
textDirection: textDirection,
140+
thumb: thumb);
141+
}
142+
}
143+
144+
enum SliderType { vertical, horizontal }

0 commit comments

Comments
 (0)