rrousselGit / state_notifier

ValueNotifier, but outside Flutter and with some extra perks
MIT License
311 stars 28 forks source link

[Support] State Notifier Consumer 💙🇵🇭 #3

Closed ianjaspersantos closed 4 years ago

ianjaspersantos commented 4 years ago

Currently, the implementation of StateNotifierBuilder widget requires stateNotifier property which will be coming from the Consumer widget or (from the context extension if possible).

return StateNotifierProvider<CounterStateNotifier, CounterState>(
  create: (BuildContext context) => CounterStateNotifier(CounterState(0)),
  child: Consumer<CounterStateNotifier>(
    builder: (BuildContext context, CounterStateNotifier counterStateNotifier, Widget child) {
      return StateNotifierBuilder<CounterState>(
        stateNotifier: counterStateNotifier,
        builder: (BuildContext context, CounterState counterState, Widget child) {
          counterStateNotifier.increment();
          counterStateNotifier.decrement();
          counterState.value;

          return Scaffold();
        },
      );
    },
  ),
);

While it's good and working like a charm, I'd like to know if you have a plan adding support for the following syntax. :)

return StateNotifierProvider<CounterStateNotifier, CounterState>(
  create: (BuildContext context) => CounterStateNotifier(CounterState(0)),
  child: StateNotifierConsumer<CounterStateNotifier, CounterState>(
    builder: (BuildContext context, CounterStateNotifier counterStateNotifier, CounterState counterState, Widget child) {
      counterStateNotifier.increment();
      counterStateNotifier.decrement();
      counterState.value;

      return Scaffold();
    },
  ),
);
rrousselGit commented 4 years ago

The official Consumer of provider works

ianjaspersantos commented 4 years ago

Final output! Thanks hero! 💙💙💙

return StateNotifierProvider<CounterStateNotifier, CounterState>(
  create: (BuildContext context) => CounterStateNotifier(CounterState(0)),
  child: Consumer2<CounterStateNotifier, CounterState>(
    builder: (BuildContext context, CounterStateNotifier counterStateNotifier, CounterState counterState, Widget child) {
      counterStateNotifier.increment();
      counterStateNotifier.decrement();
      counterState.value;

      return Scaffold();
    },
  ),
);

PS: I would still love to see if you will out of the box support this kind of syntax, cause it will be a lovely match to StateNotifierProvider.

class StateNotifierConsumer<A extends StateNotifier<B>, B> extends StatelessWidget {}
rrousselGit commented 4 years ago

I do not see any reason to do such thing, as StateNotifierConsumer == Consumer

With Dart upcoming typedefs for classes, this becomes trivial:

typedef StateNotifierConsumer<A extends StateNotifier<B>, B> = Consumer2<A, B>;
ianjaspersantos commented 4 years ago

Honestly you're right! There's no reason to do such thing. It's just a matter of (my) naming preferences. Hopefully, I can see typedefs for classes in action very soon. 💙💙💙

rrousselGit commented 4 years ago

You can do, as of today, the following :

mixin _Noop {} 

class StateNotifierConsumer<A extends StateNotifier<B>, B> = Consumer2<A, B> with _Noop;
ianjaspersantos commented 4 years ago

Best part about flutter community while still learning 💙

You can do, as of today, the following :

mixin _Noop {} 

class StateNotifierConsumer<A extends StateNotifier<B>, B> = Consumer2<A, B> with _Noop;