MarcinusX / NumberPicker

BSD 2-Clause "Simplified" License
305 stars 142 forks source link

how to onChange called when the user stops changing the value #139

Open RahulEncodework opened 1 year ago

RahulEncodework commented 1 year ago

onChange called when the user stops changing the value

gmkado commented 1 year ago

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

```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( 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 ), ); } } ```