Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RahulEncodework opened this issue Feb 7, 2023 · 1 comment
Open

Comments

@RahulEncodework
Copy link

onChange called when the user stops changing the value

@RahulEncodework RahulEncodework changed the title onchange end property is required how to onChange called when the user stops changing the value Feb 7, 2023
@gmkado
Copy link

gmkado commented Jul 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants