NumberPicker
NumberPicker copied to clipboard
how to onChange called when the user stops changing the value
onChange called when the user stops changing the value
I wanted the same thing. My workaround is to listen to UserScrollNotification and only call onChanged when the scroll direction is "idle". Solution below uses flutter hooks but could also be done with a stateful widget.
MyNumberPicker.dart
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:numberpicker/numberpicker.dart';
class MyNumberPicker extends HookConsumerWidget {
/// Min value user can pick
final int minValue;
// ... all other fields
const MyNumberPicker({
Key? key,
required this.minValue,
// ... all other fields
}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final temp = useState(value);
return NotificationListener<UserScrollNotification>(
onNotification: (notification) {
if (notification.direction == ScrollDirection.idle) {
onChanged(temp.value);
}
return true;
},
child: NumberPicker(
value: temp.value,
onChanged: (value) => temp.value = value,
minValue: minValue,
// ... pass through all other fields
),
);
}
}