NumberPicker icon indicating copy to clipboard operation
NumberPicker copied to clipboard

how to onChange called when the user stops changing the value

Open RahulEncodework opened this issue 3 years ago • 1 comments

onChange called when the user stops changing the value

RahulEncodework avatar Feb 07 '23 07:02 RahulEncodework

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

gmkado avatar Jul 16 '23 18:07 gmkado