stargazing-dino / async_button_builder

A helper builder to create loading buttons in Flutter
https://pub.dev/packages/async_button_builder
MIT License
29 stars 9 forks source link

RFC: What about onLongPress, onDoubleTap? #21

Open stargazing-dino opened 3 years ago

stargazing-dino commented 3 years ago

I could add more parameters, but then the function signature would change on the builder which is breaking. I could also make a class to hold all the various types of taps but that's also breaking:

AsyncButtonBuilder(
  child: Text('Click Me'),
  onPressed: () async {
    await Future.delayed(Duration(seconds: 1));
  },
  onLongPressed: () async {
    await Future.delayed(Duration(seconds: 1));
  }
  builder: (context, child, callback, _) {
    return TextButton(
      child: child,
      onPressed: callback.onPressed,
      onLongPressed: callback.onLongPressed
    );
  },
),

Dunno.

Another non-breaking option would to make a factory version with all the different types of presses. Not really the cleanest solution though.

ayushin commented 3 years ago

Hi, nice library! I wish we found it a week ago, we've just published something very similar (https://pub.dev/packages/progress_builder) but we did take a slightly different approach.

Perhaps we can exchange ideas.

I was thinking about supporting multiple actions as well, but I guess then it would make more sense to use a Cubit or Bloc, as you get more complexity you may not want to have inside your UI.

Nice work though!

stargazing-dino commented 3 years ago

Thanks ! Of course, I'd be happy to share ideas.

Concerning needing a more complicated state management, I feel as though this package shouldn't obstruct anyone from doing that on their side. This package so far is only concerned in moving UI around so I personally don't feel as I need anything more than setState under the hood.

As for the issue, from inkwell there are a lot of pointer events to possibly account for:

GestureTapCallback? onTap,
GestureTapCallback? onDoubleTap,
GestureLongPressCallback? onLongPress,
GestureTapDownCallback? onTapDown,
GestureTapCancelCallback? onTapCancel,
ValueChanged<bool>? onHighlightChanged,
ValueChanged<bool>? onHover,

I wonder where I draw the line. Personally would not consider anything after onTapDown. This reminded me however that we might be able to do something similar with nested builders to get the same functionality:

AsyncButtonBuilder(
  child: Text('Click Me'),
  onPressed: () async {
    // for presses
    await Future.delayed(Duration(seconds: 1));
  },
  builder: (context, child, onPressed, _) {
    return AsyncButtonBuilder(
      child: child,
      onPressed: () async {
        // for long pressed
        await Future.delayed(Duration(seconds: 1));
      },
      builder: (context, child, longPressed, _) {
        return TextButton(
          child: child,
          onPressed: onPressed,
          onLongPress: longPressed,
        );
      },
    );
  },
),

I would rename onPressed to callback to be generic but that looks reasonable. I'm still favoring adding new properties though instead of nesting which inevitably leads to a MultiProvider kind of situation and I don't think that's possibly for function types. Talking about builders I just had the realization this whole package could be a nifty hook as well.

final buttonHandler = useAsyncButton(myCallback, child);

// Then add these to the UI
// buttonHandler.child
// buttonHandler.callback