fluttercommunity / rx_command

RxCommand - Reactive event handler wrapper class inspired by ReactiveUI. Maintainer @escamoteur
https://pub.dev/packages/rx_command
MIT License
134 stars 19 forks source link

RxCommandListener pass functions after initialization feature enquiry #25

Closed MillerAdulu closed 5 years ago

MillerAdulu commented 5 years ago

I am appreciative of the good work on this. Thank you a lot.

I am working on an application which I managed to find a workaround for. Currently, it's not possible to pass functions to some of the RxCommandListerner without going through the constructor as shown in the code sample below. My workaround is that I set a boolean variable in the class which is modified by the RxCommandListener which I then use elsewhere.

import 'package:flutter/material.dart';
import 'package:rx_command/rx_command_listener.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  RxCommandListener _listener1;
  ScrollController _scrollController = ScrollController();

  bool busy;
  bool hasMoreResults;

  @override
  void initState() {
    super.initState();

    _listener1 = RxCommandListener(
      myCoolRxCommandViaServiceLocator,
      onNotBusy: () => this.busy = false,
      onIsBusy: () => this.busy = false,
    );

    _scrollController.addListener(() {
      double maxScroll = _scrollController.position.maxScrollExtent;
      double currentScroll = _scrollController.position.pixels;
      double delta = MediaQuery.of(context).size.height * .3;

      if (maxScroll - currentScroll < delta) {
        if (hasMoreResults && busy == false) print("Do some pretty cool stuff");
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listener1?.dispose();
    _scrollController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Is it possible to have something of this sort where you can pass functions into the listener after it has already been instantiated? If not, what would be recommended?

import 'package:flutter/material.dart';
import 'package:rx_command/rx_command_listener.dart';

class Home2 extends StatefulWidget {
  @override
  _Home2State createState() => _Home2State();
}

class _Home2State extends State<Home2> {
  RxCommandListener _listener1;
  ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();

    _listener1 = RxCommandListener(myCoolRxCommandViaServiceLocator);

    _scrollController.addListener(() {
      double maxScroll = _scrollController.position.maxScrollExtent;
      double currentScroll = _scrollController.position.pixels;
      double delta = MediaQuery.of(context).size.height * .3;

      if (maxScroll - currentScroll < delta) {
        _listener1.onNotBusy(() {
          print("Do some pretty cool stuff");
        });
        _listener1.onIsBusy(() {
          print("Hold up for cool stuff to end");
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _listener1?.dispose();
    _scrollController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Thanks.