I used a GestureDetector
and AnimatedBuilder
to get a slider. I want to trigger a function whenever the slider reaches the end and then vanish it in an animation, which I can achieve by using visibility, but how would I know that when my slider has reached the end? Please help me out.
Here is the code -
class SwipeButton extends StatefulWidget {
final ValueChanged<double>? valueChanged;
final String? text;
final Function? callBack;
SwipeButton({this.valueChanged, this.text, this.callBack});
@override
SwipeButtonState createState() {
return new SwipeButtonState();
}
}
class SwipeButtonState extends State<SwipeButton> {
ValueNotifier<double> valueListener = ValueNotifier(.0);
@override
void initState() {
valueListener.addListener(notifyParent);
super.initState();
}
void notifyParent() {
if (widget.valueChanged != null) {
widget.valueChanged!(valueListener.value);
}
}
@override
Widget build(BuildContext context) {
return Container(
color: colorPrimary,
height: 40.0,
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Stack(
children: [
Center(
child: Text(
"${widget.text}",
style: TextStyle(
color: Colors.white,
fontSize: 17,
),
),
),
Builder(
builder: (context) {
final handle = GestureDetector(
onHorizontalDragUpdate: (details) {
valueListener.value = (valueListener.value +
details.delta.dx / context.size!.width)
.clamp(.0, 1.0);
print("${details.delta.dx} ${context.size!.width}");
if(details.delta.dx==context.size!.width) {
print("Reached");
//Call a function
}
},
onPanEnd: (details) {
print('\n \n Ended');
},
onPanUpdate: (details) {},
child: Container(
height: 25.0,
width: 25.0,
color: Colors.white,
child: const Center(
child: Icon(
Icons.arrow_forward,
color: Colors.orange,
size: 12,
),
),
),
);
return AnimatedBuilder(
animation: valueListener,
builder: (context, child) {
return Align(
alignment: Alignment(valueListener.value * 2 - 1, 0),
child: child,
);
},
child: handle,
);
},
),
],
),
);
}
}